954,549 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Printing Binary Tree

I'm having trouble formatting a binary tree. In my program, the tree format should print every output as such:

35
30
29
25
24
20
15

This is the code that I have thus far.

public void treeFormatTraversal(){//begin
		int level = 0;
		if(root !=null)
			treeFormatHelper(root, level);
		else
			System.out.println("\nTree is Empty\n");
	}
	private void treeFormatHelper(TreeNode<T> tree, Integer indentSpaces){
		
		if(tree==null)
			return;
		treeFormatHelper(tree.rightNode,indentSpaces+1);
		for(int x=1;x<indentSpaces;x++)
			System.out.print("\t");
		System.out.printf("%s",tree.data);
		treeFormatHelper(tree.leftNode,indentSpaces+1);
	}


Any help is appreciated on how to implement the display format.

Thanks

Iamthecheese
Newbie Poster
15 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
 

------35
---30
------29
25
------24
---20
------15

I meant like this without the dashes. :)

Iamthecheese
Newbie Poster
15 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: