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
