View Single Post
  #7 (permalink)  
Old 08-05-2007, 02:09 PM
Bench Bench is offline
Full Programmer
Join Date: Jul 2007
Location: UK
Posts: 113
iTrader: (0)
Bench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished road
Your inorder looks good. I can see that you insist on using a while loop for this.

Recursion causes the entire function to become one loop, what you seem to have done is broken it into half with 2 loops - therefore your function as it is will only traverse down one side of the loop or the other

try something like this (Without having your BinaryNode implementation, I made my own simplified version to test this one using a built-in type rather than a Double)
Code:
    public void Insert(BinaryNode node)
    {
        BinaryNode n = this.root;
        while( n != null )
        {
            if (node.data < n.data) 
            {
                if (n.left == null)
                {
                    n.left = node;
                    return;
                }
                else
                    n = n.left;
            }
            else if (node.data > n.data)
            {
                if (n.right == null)
                {
                    n.right = node;
                    return;
                }
                else
                    n = n.right;
            }
        }
    }

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
The Following User Says Thank You to Bench For This Useful Post:
HelloWorld (08-05-2007)