Binary search tree

It is a simple programme to make a list of numbers. I'm using a class numbers, and a class for the list. The problem is root changes whenever i add another element. (tree is the name of my list class)


void tree::add(number *num)
{


if (root == NULL) { // empty tree
root = num;
return;
}


number *curr = root;

cout << "ROOT CHANGED TO : " << root->num << endl;
number *prev; // find insertion point

while (curr!=NULL) { // find a correct leaf to insert at
prev = curr;
if (num->num > curr->num) {
curr = curr->right;
cout << "NUM " << num->num <<endl;}

else{
curr = curr->left;
cout << "NUM " << num->num <<endl;}

}

if (num->num > prev->num)
prev->right = num; // insert node as a right child of the leaf
else
prev->left = num; // insert node to left
}



Topic archived. No new replies allowed.