Tree print Binary Tree

Source: Internet
Author: User

Tree Structure printing binary tree, the difficulty lies in determining the gap between nodes. Because the content length of each node is not uniform, the processing method is to select the maximum length of each node as the standard, and other nodes are filled with space characters.

For a binary tree with a height of h, the bottom layer (leaf node), the gap between the first node and the start output is 2 ^ 0-1, and the gap between nodes is 2 ^ 1-1. The second to last layer. The gap between the first node and the start output is 2 ^ 1-1, and the gap between nodes is 2 ^ 2-1. That is, for layer k (from top to bottom, the root node is the first time), the first node is 2 ^ (h-k)-1 from the start output position, the gap between nodes is 2 ^ (h-k + 1)-1.

In this example, null ** indicates the left and right nodes of the leaf node. When you insert a node, enter null **. Then, you can complete the insert operation by entering the node content. In the code, the prefix, center, and suffix traversal are also implemented.

The code for the tree node is as follows:

public class TreeNode {public String s;public TreeNode left;public TreeNode right;public TreeNode(){this.s = "null" + Tree.nullNum;Tree.nullNum++;left = null;right = null;}}

The Tree Code is as follows:

Import java. util. arrayList; import java. util. list; public class Tree {public static int nullNum = 0; private TreeNode root; public Tree () {initTree ();} public void initTree () {nullNum = 0; root = new TreeNode ();} public String printTree () {String tree = ""; List <TreeNode> level = new ArrayList <TreeNode> (); list <TreeNode> wholeTree = new ArrayList <List <TreeNode> (); int maxLength = 0; level. add (root); while (Level. size ()> 0) {wholeTree. add (level); List <TreeNode> newLevel = new ArrayList <TreeNode> (); for (int I = 0; I <level. size (); I ++) {TreeNode node = level. get (I); if (node. s. length ()> maxLength) {maxLength = node. s. length ();} if (node. left! = Null & node. right! = Null) {newLevel. add (node. left); newLevel. add (node. right) ;}} level = newLevel;} int height = wholeTree. size (); for (int I = 0; I 

The code for the test class is as follows:

Mport java. util. principal; public class Test {public static void main (String [] args) {Tree tree = new Tree (); vertex in = new vertex (System. in); while (true) {tree. printTree (); System. out. println ("Enter the location to insert"); String position = in. nextLine (). trim (). replace ("\ n", ""); System. out. println ("Enter node content"); String s = in. nextLine (). trim (). replace ("\ n", ""); if (tree. insert (position, s) = false) {System. out. println ("the location you entered does not exist... "); break ;}}}}

The following figure shows the running effect:


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.