Binary Search Tree, Tabs in preorder traversal |
Binary Search Tree, Tabs in preorder traversal |
Goo |
Mar 6 2009, 07:12 AM
Post
#1
|
Newbie Group: Members Posts: 13 Joined: 5-October 08 Member No.: 6,818 |
Hi
I'm using a Binary Search Tree, and outputting it with Pre-order traversal. My Pre-order traversal code: public void printPreorder() { System.out.println("Preorder"); printPreorder(root, 0); } private void printPreorder(BinaryNode t, int n) { for(int i = 0; i < n; i++) { System.out.print("\t"); } if (t != null) { System.out.println(t.element.toString( ) ); printPreorder(t.left, n+1); printPreorder(t.right, n+1); } } The traversal code is working correct but there is something wrong with my tabs. The output looks like this: indicated *tab* where it displays a tab 5 *tab* 2 *tab**tab* 1 *tab**tab**tab**tab**tab**tab**tab* 3 *tab**tab**tab**tab**tab* 4 *tab**tab**tab**tab**tab**tab**tab**tab* 6 *tab**tab**tab* 7 but must actually be: 5 *tab* 2 *tab**tab* 1 *tab**tab* 3 *tab**tab**tab* 4 *tab**tab* 7 *tab* 6 Can someone please help me with my tabs? This post has been edited by Goo: Mar 6 2009, 07:15 AM |
Lo-Fi Version | Time is now: 28th March 2024 - 05:17 PM |