Hi all!
I've just started working on my Binary Search Tree and already there's a problem that I can't seem to understand. Ok here's the problem, when I compile its ok but when I run it there's an error, i've managed to found where the problem is but I don't understand why..I will comment the part where there seems to be a runtime error.
#include<iostream>
using namespace std;
class treeNode{
public:
int item;
treeNode *left;
treeNode *right;
treeNode();
};
treeNode::treeNode(){
item=0;
left=right=NULL;
}
class binarySearchTree{
private:
int size;
public:
treeNode *root;
binarySearchTree();
void insert(int);
void del(int);
void search(int);
void display();
};
binarySearchTree::binarySearchTree(){
root=NULL;
}
void binarySearchTree::insert(int x){
treeNode *temp;
treeNode *add;
add = new treeNode;
add->item=x;
temp=root;
if(temp==NULL)
root=add;
else{
while(temp!=NULL){
if(temp->item>=add->item)
temp=temp->left;////////////It's here that I encounter the runtime error..why is this?
else
temp=temp->right;
}
temp=add;
}
}
void binarySearchTree::display(){
while(root!=NULL){
cout<<root->item<<endl;
root=root->left;
}
}
int main(){
binarySearchTree x;
x.insert(1598);
x.insert(160);
x.display();
return 0;
}