Results 1 to 7 of 7
Thread: BST Inorder Traversal problem
- 12-24-2010, 02:06 AM #1
Member
- Join Date
- Dec 2010
- Posts
- 3
- Rep Power
- 0
BST Inorder Traversal problem
Hi guys,
Let me first say that I'm new to this forum, I've read some topic here with great interest but now that I've stumbled on a problem I thought you guys might be able to help me. :)
I've created a program which implements binary trees. I'm required to output the elements of all nodes in a (sub)tree in a natural order. I immediately thought of using Inorder Traversal. The, recursive method I want to use is this:
Java Code:public static void printInOrder(BSTNode top) { if (top != null) { printInOrder(top.left); System.out.println(top.element); printInOrder(top.right) } }
However, the trick is that I don't want to print the values but I want to store them in a string and output them at the end.
Something like this:
I can't seem to make a simple recursive method which does this :/Java Code:public static String printInOrder(BSTNode top) { ? ? ? Return aString }
Maybe you guys can help me?
- 12-24-2010, 06:11 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
So keep them in a global position.
- 12-24-2010, 08:21 AM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,407
- Blog Entries
- 7
- Rep Power
- 17
Pass a second argument to the (recursive) method, say, a StringBuffer and append each data element to it. When your method has finished the Buffer contains all data in inorder traversal; something like this will do it:
Note that the method returns the same StringBuffer for convenience.Java Code:public static StriingBuffer printintInOrder(BSTNode top, StringBuffer sb) { if (top != null) { printInOrder(top.left, sb); sb.append(top.element); printInOrder(top.right, sb) } return sb; }
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 12-24-2010, 03:02 PM #4
Member
- Join Date
- Dec 2010
- Posts
- 3
- Rep Power
- 0
Yeah that's the easiest solution, but global variables are evil and are not a very elegant solution to a problem.
Hi Jos, ty for your input.
I also thought about a solution like this however this way the code won't produce the right sequence but will just terminate with the value at the root :(
Let's take this BST for example:
I obviously want it to return the sequence A,B,C,D,E,F,GJava Code:D /\ B F /\ /\ A C E G
The way the algorithm works:
So if we add a return statement after the if-statement it will just do that after it hits a leaf-node..Java Code:inOrderTraversal(D); inOrderTraversal(B); inOrderTraversal(A); inOrderTraversal(NULL); print(A) inOrderTraversal(NULL); print(B) . . .
I think i might have to rewrite the algorithm for it to work, but since I'm already not that great with visualising recursion that gonna be bothersome..
I had an idea of maybe writing in something similiar to this:
But I have no idea how to implement the additional conditions (stop if you hit a reference to a null-node) in this kind of form. :(Java Code:public static String inOrder(BSTNode aNode, String sb) { return sb + inOrder(aNode.left, "") + " " + aNode.element.toString() + inOrder(aNode, ""); }
Maybe you guys can have another look at it, thnx in advance :)Last edited by Kirby <(^^)>; 12-24-2010 at 03:18 PM.
- 12-24-2010, 03:17 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,407
- Blog Entries
- 7
- Rep Power
- 17
- 12-24-2010, 03:36 PM #6
Member
- Join Date
- Dec 2010
- Posts
- 3
- Rep Power
- 0
- 12-25-2010, 12:52 AM #7
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Similar Threads
-
Array traversal issues
By sondratheloser in forum New To JavaReplies: 3Last Post: 08-13-2012, 12:49 AM -
Focus Traversal Demo in SWT
By Java Tip in forum SWTReplies: 0Last Post: 07-07-2008, 04:37 PM -
Bidirectional Traversal with ListIterator
By Java Tip in forum java.langReplies: 0Last Post: 04-16-2008, 10:37 PM -
JTable Focus Traversal
By helios_lie in forum AWT / SwingReplies: 1Last Post: 12-20-2007, 10:27 AM -
Preorder Traversal problem...
By tuckker in forum New To JavaReplies: 3Last Post: 12-04-2007, 06:06 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks