View Single Post
  #2 (permalink)  
Old 08-03-2007, 07:25 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
Here's some pseudocode for an insert.. Binary trees are very difficult without recursion, so if you're trying to do it with a while or for loop, then you're in for an interesting time.

Code:
Begin Method: Insert(Tree, NewNode)
    if ( NewNode.data < Tree.data ) Then
        If ( Tree.left is NULL ) Then
            Tree.left = NewNode
        Else
            Insert( Tree.left, NewNode)
        End If

    Else if ( NewNode.data > Tree.data ) Then
        If ( Tree.right is NULL ) Then
            Tree.right = NewNode
        Else
            Insert( Tree.right, NewNode)
        End If
    End If
End Method 


Begin Method: Grow(Data)
    NewNode = New Node(Data)
    If ( Root is NULL ) Then
        Root = NewNode
    Else
        Insert( Root, NewNode )
    End If
End Method

__________________

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-03-2007)