How to print binary tree with spaces and slashs

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • game2d
    New Member
    • Apr 2013
    • 59

    How to print binary tree with spaces and slashs

    i know how to insert, print binary tree but how to print it like this? note that leafs and nodes will change and dont have to be same as this.

    Code:
    ex:
    
        6
       / \
     1    7
     /    / \
    0    5   8


    this is what i have, but this prints 6,1,0,7,8. i want to add space and slashs.
    Code:
    public void PreOrderTraversal(node root_node) {
    
    		if (root_node == null) {
    			 System.out.println("Binary Tree is empty!");
    		} else {
    			System.out.println(root_node.name + " " + root_node.age);
    			PreOrderTraversal(root_node.left_child);
    			PreOrderTraversal(root_node.right_child);
    		}
    	}
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    You need to know how many spaces are required for the most bottom child before you start printing the root node because you must print the root node after adding enough spaces to cover the leftmost child.

    Comment

    Working...