3

I've been trying to implement a binary search tree in C++ for fun. My problem is that I'm having trouble with my insert function. Below is what I have so far:

class TreeNode{

public: 
    int data;          
    TreeNode *left;    
    TreeNode *right;  
    void Insert(int value, TreeNode *x);
    void TreeNode::Print(TreeNode *x);
    TreeNode();
};


TreeNode::TreeNode(){

left = NULL;
right = NULL;

}

.

void TreeNode::Insert(int value, TreeNode *x){

    if(x->left == NULL && x->right == NULL){
           TreeNode *tree = new TreeNode();                        
           tree->datavalue;                               
           x->left = tree;                                  
    }

    else if(x->left == NULL && x->right != NULL){

           TreeNode *tree = new TreeNode();                
           tree->data = value;                          
           x->left = tree;                                 
    }

    else if(x->left != NULL && x->right == NULL){

           TreeNode *tree = new TreeNode();                      
           tree->data = value;                            
           x->right = tree;                          
    }

    else if(x->left != NULL && x->right != NULL){

        ??????

    }

}
Vadim Kotov
  • 7,103
  • 8
  • 44
  • 57
  • what problem are you having exactly ? – h4ck3d Dec 02 '12 at 09:05
  • 2
    Are you trying to have any organization to your tree? It looks like you're just placing the elements anywhere they'll go, rather than using mathematical logic to find a place for it. – JustinDanielson Dec 02 '12 at 09:06
  • I think you want to implement a Binary Search Tree . – h4ck3d Dec 02 '12 at 09:07
  • 1
    You should write your search function before you do insert. In order to do an insert, you would follow logic very similar to search to find the correct place for the node. – JustinDanielson Dec 02 '12 at 09:09
  • You're going in the wrong direction, unless this insert is a helper function for Add – JustinDanielson Dec 02 '12 at 10:23
  • Your insert function should be static if this is really what you want to do. Your current implementation ignores the fact that "this" is a pointer to a root or subroot of the tree. – JustinDanielson Dec 03 '12 at 01:22

2 Answers2

4

You should not insert blindly, follow the logic below. If x.value is less than the root value, attempt to insert to the left. If x.value is >= root.value, go to the right. Repeat this logic until you hit a NULL value. That will be the proper place to insert your element.

You could flip the logic, I just chose left on < because less than kinda makes an arrow to the left. <- :P

TreeNode *root = this;
TreeNode *parent = root;
//Traverse the tree, go left if x.val < , else go right(>=)
while(root) {
    parent = root;
    root = (root->value > x.value) ? root->left : root->right;
}
if(parent->value > x->value) 
    parent->left = x;
else
    parent->right = x;

If you don't care about ordering, do a depth first search with a Queue.

queue<TreeNode*> nodes;
nodes.push(this);
while(1)
{
    if(!nodes.front()->left)
    {
        nodes.front()->left = x;
        return;
    } 
    else if (!nodes.front()->right)
    {
        nodes.front()->right = x;
        return;
    }
    nodes.push(nodes.front()->left);
    nodes.push(nodes.front()->right);
    nodes.pop();
}
JustinDanielson
  • 3,055
  • 1
  • 16
  • 26
  • This is only required if you need binary *search* trees. That is not a requirement here. You can insert blindly. – axiom Dec 02 '12 at 09:20
  • 3
    First line of the OP's question is "I've been trying to implement a binary search tree in C++ for fun." but the logic he posted in his code contradicts him. – JustinDanielson Dec 02 '12 at 09:22
0

If you want to insert when both left and right child are NOT null , then you can insert the item by recursively moving to the left-most node of the tree. And since you are just inserting values in no particular order , it will be difficult to keep track. Rather implement a Binary Search Tree in that case.

else if(x->left != NULL && x->right != NULL){
         Insert(value,x->left);
         x-> data  = value;
         x->left = x->right = NULL;
}

And most importantly , insert a BASE case to exit from recursion.

h4ck3d
  • 5,584
  • 15
  • 44
  • 73