![]() |
|
|
|
| ||||||
|
Welcome to the The ProgrammersTalk Community forums. You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today! If you have any problems with the registration process or your account login, please contact contact us. |
| Tags: binarysearchtree, help, implementation, java, programming |
![]() |
![]() | | LinkBack | Thread Tools | Display Modes | ![]() |
| |
| |||
| 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 |
| The Following User Says Thank You to Bench For This Useful Post: | ||
HelloWorld (08-03-2007) | ||
| ||||
| Quote:
Edit: Btw, I'm not trying to do Binary Tree, but Binary Search Trees. No wonder your psuedo code looks much simpler than I've thought ![]() Last edited by HelloWorld : 08-03-2007 at 06:59 PM. |
| ||||
| 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 ![]() 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 ![]() Last edited by HelloWorld : 08-03-2007 at 11:02 PM. |
| |||
| Quote:
the pseudocode I posted is for a Binary search tree (try it). Binary search trees are deceptively simple - based on the mathematical concept of a binary search. In fact, they're far more difficult to explain than to create. The best analogy I can think of is looking up a word in a dictionary, by starting in the middle, and eliminating half the remaining dictionary at a time - your "root node" is always whatever portion of the dictionary remains at each dictionary-traversal. Here's some traversal psuedocode for the binary search tree. Again, really simple, but it works a treat. Code: Begin Method: In_Order_Traversal( Root )
If( Root is not NULL )
In_Order_Traversal( Root.left )
Write( Root.data )
In_Order_Traversal( Root.right )
End If
End Method |
| The Following User Says Thank You to Bench For This Useful Post: | ||
HelloWorld (08-04-2007) | ||
| ||||
| Here's the last code that I got for "insert" implementation, please take a look and see if there's anything wrong. For some reason, I don't get ordered number when I did "inorder" to my BinarySearchTrees ![]() PHP Code: PHP Code: Last edited by HelloWorld : 08-05-2007 at 12:23 PM. |
| |||
| 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;
}
}
} |
| The Following User Says Thank You to Bench For This Useful Post: | ||
HelloWorld (08-05-2007) | ||
| ||||
| Bench, truly.. I'm kind of confused with recursion method, probably I need some practice to read recursion more ![]() can you please explain what happened to my inorder method? Let's say the inputs are: 5, 2, 9, 1, 3, 11 PHP Code: |
| |||
| Recursion causes multiple different calls of the same function to be placed on a stack frame. Each call to the function relies on the previous call beneath it on the stack frame for input, and returns to the previous call after its finished. In effect, each function call is like a new dependent object. if your inputs are in the order of 5, 2, 9, 1, 3, 11, then your BSTree will look like this (Sorry for the crude Text-art) Code: 5
_______/ \_______
2 9
_______/ \_______ \_______
1 3 11 The first function pushed on to the stack frame is inorder( "5" ) when inorder("5") runs, its first statement calls inorder("5".left) which is inorder("2") when inorder("2") runs, its first statement calls inorder("2".left) which is inorder("1") when inorder("1") runs, its first statement calls inorder("1".left) which is inorder(NULL) the stack frame now looks like Code: -top-
inorder(NULL)
inorder("1")
inorder("2")
inorder("5")
-bottom- At all times, the active function is the function at the top of the stack frame Next, inorder(NULL) returns. This removes it from the stack frame, leaving inorder("1") back at the top. Inorder("1") must take up from where it left off, so the next statement is Code: System.out.println("1"); Finally, inorder("1") is finished, and can be removed from the stack frame. the function at the top is now inorder("2"). it has already completed inorder("2".left), so it moves onto the next statement Code: System.out.println("2"); Code: -top-
inorder("3")
inorder("2")
inorder("5")
-bottom- Last edited by Bench : 08-05-2007 at 02:19 PM. |
| The Following User Says Thank You to Bench For This Useful Post: | ||
HelloWorld (08-05-2007) | ||
| ||||
| Thanx a lot Bench I'm now working on the remove() implementation for BinarySearchTrees, it's weird that I can't set anything unless if I'm using this.root directly instead of referenced it to n. I can see why, but if I update the value based on the this.root, it will remove the root right away I'm looking on solution into remove() implementation ![]() I tested this doesn't work: PHP Code: PHP Code: Last edited by HelloWorld : 08-05-2007 at 02:31 PM. |
![]() |
| Thread Tools | |
| Display Modes | |
| |