0

I have an issue where my items in my binary tree are being inserted incorrectly. I'm inserting strings in each node. I think I might be doing something wrong because it seems like I always end up with the wrong tree. i.e.

A, B, C

I should have

   B
  /  \
 A    C

but somehow I end up with like:

   C
  / \
 B   A

or something different depending in which order I insert in my tree.

This is my tree class:

This is my insert method and insert helper method. Can you take a look and see what I'm doing wrong? Thanks in advance.

void BinarySortTree::insert(string key)
{
    if(root != NULL)
    {
        insert(key, root);
    }
    else
    {
        root = new TreeNode;
        root->item = key;
        root->left = NULL;
        root->right = NULL;
    }
}

void BinarySortTree::insert(string key, TreeNode *node)
{
    bool done = false;

    while(!done)
    {
        if(key.compare(node->item) < 0)
        {
            if(node->left != NULL)
            {
                node = node->left;
            }
            else
            {
                node->left = new TreeNode;
                node->left->item = key;
                node->left->left = NULL;
                node->left->right = NULL;
                done = true;
            }
        }
        else if(key.compare(node->item) > 0)
        {
            if(node->right != NULL)
            {
                node = node->right;
            }
            else
            {
                node->right = new TreeNode;
                node->right->item = key;
                node->right->left = NULL;
                node->right->right = NULL;
                done = true;
            }
        }
        else if(key.compare(node->item) == 0)
        {
            done = true;
        }
    }

}
hippietrail
  • 15,848
  • 18
  • 99
  • 158
  • Your algorithm is not recursive, please correct your question tags. And I advice you to re-read the way and method that insertion goes. – Tamer Shlash Mar 13 '11 at 19:21
  • 2
    @Mr.TAMER: The *implementation* that you see above is certainly not recursive. As for the *algorithm* itself... it is not that simple. It can be formally called recursive under a wide definition of algorithmic recursion. In fact, from the algorithmic point of view, any cycle can be interpreted as a degenerate form of recursion. – AnT stands with Russia Mar 13 '11 at 19:47
  • @AndreyT: Thanks, I didn't know... Lesson learned :) – Tamer Shlash Mar 13 '11 at 19:50
  • I compiled you code, exactly what order did you insert those letters? – sj755 Mar 13 '11 at 19:55
  • 1
    I don't think there's anything wrong with the methods you listed. If you insert the values in sequence "A, B, C", you should get a non-balanced tree (like Gajet's answer). I suggest you check or post here the other methods you have, especially inorder travel and the pretty printing method. – Antti Mar 13 '11 at 19:56

1 Answers1

1

it's becouse when you never change what was periviusly inserted, for example if you insert C at first the next item you insert(for example b) never can take place in root so if you insert "C","B","A" in this order you will have a tree shaped like :

    C
   /
  B
 /
A

you have to check every if the current node has to be changed! and reinsert you key couse your root may change at every insert! and the tree you draw would never generate the only pissible forms of trees your code generate are these for given inputs :

 ABC        ACB      BAC        CBA     CAB
                     BCA        
 A          A         B            C       C            
  \          \       / \          /       /      
   B          C     A   C        B       A            
    \        /                  /         \ 
     C      B                  A           B
Ali1S232
  • 3,373
  • 2
  • 27
  • 46
  • 1
    You're describing self-balancing binary search trees. It looks like he's trying to implement a simple binary search tree without balancing, which degenerate to a linear list in the worst case as you describe. – Antti Mar 13 '11 at 19:52
  • what do I need to do to make it balanced? I only need to add, no need to delete. –  Mar 13 '11 at 22:18
  • For a balanced binary tree, you'll need e.g. a [red-black tree](http://en.wikipedia.org/wiki/Red-black_tree) or an [AVL tree](http://en.wikipedia.org/wiki/AVL_tree). Implementing those is a bit tricky, though, and you'll probably first want to solve your original problem. – Antti Mar 14 '11 at 17:53