Hello everyone, I've been having problems running this program and i cant figure out the problem. its just a simple program to read a mixed file that has number 1-100000 and sort them in a binary tree.I'm not getting any errors the program is just getting stuck at debug mode and not running properly. just a note there is a second program that generates the file that this program is getting the data from, i didn't post it since it seems to be running fine and doing what its suppose to.
Header:
code:
Header:
Code:
class Node{
public: int key;
int sort_key;
Node *left;
Node *right;
Node(int key1,int key2){
this->sort_key=key1;
this->key=key2;
this->left=NULL;
this->right=NULL;
}
};
class BST{
public:
Node *root;
BST(){
root=NULL;
}
void insert(int sort_key, int key)
{
Node *x=root;
Node *y=NULL;
Node *newNode = new Node(sort_key,key);
if(this->root==NULL)
{
this->root=newNode;
return;
}
while(x!=NULL)
{
y=x;
if(sort_key<x->sort_key)
x=x->left;
else
x=x->right;
}
if(sort_key<y->sort_key)
y->left=newNode;
else
y->right=newNode;
}
int findDepth(Node *node){
if(node==NULL)
return -1;
int ldepth = findDepth(node->left);
int rdepth = findDepth(node->right);
if(ldepth>rdepth)
return ldepth+1 ;
else
return rdepth+1;
}
int findDepthofNode(int key){
Node *temp=this->root;
int depth=0;
while(temp->sort_key!=key)
{
if(key<temp->sort_key)
temp=temp->left;
else
temp=temp->right;
depth++;
}
return depth;
}
int numOfEndNodesWithSecondNum(Node *node, int num)
{
if(node==NULL)
return 0;
if(node->left==NULL && node->right==NULL && node->key==num)
return 1;
int x = numOfEndNodesWithSecondNum(node->left, num);
int y = numOfEndNodesWithSecondNum(node->right, num);
return x+y;
}
int numOfOneChildNodesWithSecondNum(Node *node, int num)
{
if(node==NULL)
return 0;
int x=numOfOneChildNodesWithSecondNum(node->left,num);
int y=numOfOneChildNodesWithSecondNum(node->right,num);
if( (node->left==NULL && node->right!=NULL && node->key==num) ||
(node->left!=NULL && node->right==NULL && node->key==num) )
return x+y+1;
else
return x+y;
}
int numOfTwoChildNodesWithSecondNum(Node *node, int num){
if(node==NULL)
return 0;
int x=numOfTwoChildNodesWithSecondNum(node->left,num);
int y=numOfTwoChildNodesWithSecondNum(node->right,num);
if( node->left!=NULL && node->right!=NULL && node->key==num)
return x+y+1;
else
return x+y;
}
};Code:
#include <sstream>
#include <string>
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <time.h>
#include <fstream>
#include "BST.h"
//#include "writefile.cpp"
using namespace std;
//extern int writefile();
int main()
{
int i =0;
string number;
int num_count=500;
int random;
BST bstree;
//srand(time(NULL));
int *inputnumbers = new int[num_count];
//writefile();
ifstream input("mixed.dat");
//cout<<"done";
string temp;
while ( getline(input ,number))
{
istringstream token(number);
while ( token>>temp) {
inputnumbers[i] = atoi(temp.c_str());
//cout<<setw(5)<<inputnumbers[i];
i++;
}
}
input.close();
cout<<"Data read from input file..."<<endl;
for(i=0;i<num_count;i++)
{
random = rand() % 100 + 1;
bstree.insert(inputnumbers[i],random);
}
cout<<"Binary tree created..."<<endl;
cout<<"Depth of deepest node in tree is : "<<bstree.findDepth(bstree.root)<<endl;
float avg_depth=0;
for(i=0;i<5000;i++)
{
random = rand() % num_count + 1;
avg_depth+=bstree.findDepthofNode(random);
}
avg_depth/=5000;
cout<<"Average depth for searching a key is : "<<avg_depth<<endl;
cout<<"Number of end nodes with value 50 is: "<<bstree.numOfEndNodesWithSecondNum(bstree.root,50)<<endl;
cout<<"Number of one child nodes with value 50 is: "<<bstree.numOfOneChildNodesWithSecondNum(bstree.root,50)<<endl;
cout<<"Number of two child nodes with value 50 is: "<<bstree.numOfTwoChildNodesWithSecondNum(bstree.root,50)<<endl;
}