View Single Post
  #4 (permalink)  
Old 08-03-2007, 10:08 PM
HelloWorld's Avatar
HelloWorld HelloWorld is online now
PT Admin
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: In front of computer...
Posts: 1,118
iTrader: (0)
HelloWorld is a jewel in the roughHelloWorld is a jewel in the roughHelloWorld is a jewel in the rough
Icon11 BinarySearchTree Implementation - PseudoCode

I found the solution for insert after a little bit of thinking and design in on paper. Although I'm not exactly sure whether this is the correct, but it works and seems correct to me

here's the pseudo code for it:

Code:
/* assuming that root is the constructor of BinarySearchTree
that inherits from the BinaryTree or however you implement it.
Keep in mind that root is a BinaryNode */
If Node < Root
     While Root.GetLeft != Null
             If Node < Root.GetLeft
                     // Set the Left Node of Root
             Else If Node > Root.GetLeft
                     // Set the Right Node of Root
     Root = Root.GetLeft
     End While
Else if Node > Root
     While Root.GetRight != Null
             If Node < Root.GetRight
                    // Set the Left Node of Root
             Else If Node > Root.GetRight
                   // Set the Right Node of Root
      Root = Root.GetRight
      End While
EndIf
End
Tell me what you think?

Edit: After I tested this, I think there's something wrong at this line
Code:
Root = Root.GetRight

and
Code:
Root = Root.GetLeft

But I need to somehow change the root in a real time so that it can compare when it's getRight or getLeft though

__________________
PHP Code:
System.out.println("Hello World!"); 

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!

Last edited by HelloWorld : 08-04-2007 at 12:02 AM.
Reply With Quote