Thought
The concept of a binary tree does not say, how to draw two fork tree, like textbooks.
In the control of the picture, it is in a plain text environment.
The entire control can be viewed as a Cartesian coordinate system.
By setting the x Y coordinate for the node of the tree
-When you calculate x, you will have 2^x elements per line, starting with Root, and the left and right nodes of the next line are located on the left and opposite sides of the parent node.
When you calculate y, you need to use the hierarchy level property of the tree that you set up when you first create the tree.
The result is a two-fork tree can be drawn on the console.
node definition of a tree
/** * * @auther Wuwang * @createTime 2015-6-17 pm 11:17:04 * * PackageTree.binarytree;ImportLombok. Data;/** * * * * * * @author Peaches * *@Data Public class BinaryTree { PrivateNode Root;/ * Two fork Tree construction method * / Public BinaryTree() {root =NULL; }/ * Insert method * / Public void Insert(intdata) {root = insert (root, data,NULL); } PublicNodeInsert(Node node,intData, Node nodeparent) {if(node = =NULL) {node =NewNode (data); Node.parent = nodeparent; Node.level = countlevel (node); }Else{if(Data <= Node.data) {node.left = insert (node.left, data, node);//Recursive insert here, only to form //The tree is actually a two-fork sort tree. }Else{node.right = insert (node.right, data, node); } }returnNode }/ * Build a binary tree * / PublicBinaryTreeBuildtree(int.. a) { for(intDATA:A) {insert (data); }return This; }/ * Below is the middle sequence traversal AH * / Public void Printtree() {printtree (root);/* SYSTEM.OUT.PRINTLN (); */} Public void Printtree(Node node) {if(node = =NULL) {return; } printtree (Node.left); System.out.println (Node.data +""); Printtree (Node.right); } Public int Countlevel(Node node) {returnnode = =NULL?0: (1+ countlevel (node.parent)); }/ * Define a static inner class, Node * / @Data Public Static class Node {Node parent;//Parent nodeNode left;//Left nodeNode right;//Right node intX//For drawing, position in drawing, horizontal axis intY//For drawing, position in drawing, ordinateInteger data;//Node value intLevel//From root to the level of this node PublicNodegetParent() {return NULL;//ToString () ignores parent nodes, avoids Lombok generated code that recursively overflows} Node (Integer newdata) {left =NULL; right =NULL; data = NewData; } }}
Defines a tool class for exporting binary tree nodes
/** * * @auther Wuwang * @createTime 2015-6-17 pm 11:31:52 * * PackageTree.binarytree;ImportJava.util.Arrays;/** * * * * * * @author Peaches * * Public class binarytreeutils { Private Static intNodenum;Private Static intLineLength;Private Static intMaxlevel;Private StaticString[][] strings;/** * Draw binary Tree * * @param binaryTree * @createTime 2015-6-19 Morning 7:55:57 * * Public Static void Drawbinarytree(BinaryTree BinaryTree) {nodenum = Countnode (Binarytree.getroot ()); LineLength = Nodenum *2; Beforedrawbinarytree (Binarytree.getroot (), nodepostion.root); strings =NewString[maxlevel +1][linelength *2]; for(inti =0; i < strings.length; i++) { for(intj =0; J < Strings[i].length; J + +) {Strings[i][j] =" "; }} System.out.println ("Nodenum:"+ Nodenum); Drawbinarytree (Binarytree.getroot ()); System.out.println ("------------------------------------"); for(string[] s:strings) {System.out.println (arrays.tostring (s). ReplaceAll ("[\\]\\[\\,]{1}"," ")); } }/** * Before plotting a binary tree, calculate the position of all nodes in the binary tree in the XY coordinate system * * @param node * @param nodepostion * @ Createtime 2015-6-19 Morning 7:53:59 * * Private Static void Beforedrawbinarytree(Binarytree.node Node, nodepostion nodepostion) {if(node = =NULL) {return; } countxy (node, nodepostion);if(Node.left = =NULL) {Node.left =NewBinarytree.node (NULL); node.left.parent = node; Node.left.level = Node.level +1; Countxy (Node.left, nodepostion.left); }Else{Beforedrawbinarytree (node.left, nodepostion.left); }if(Node.right = =NULL) {Node.right =NewBinarytree.node (NULL); node.right.parent = node; Node.right.level = Node.level +1; Countxy (Node.right, nodepostion.right); }Else{Beforedrawbinarytree (node.right, nodepostion.right); } }/** * Draws a binary tree, outputting all nodes of the two-fork tree to a two-dimensional array. A two-dimensional array that represents the terminal for line-by-row output * * @param node * @createTime 2015-6-19 a.m. 7:55:01 */ Private Static void Drawbinarytree(Binarytree.node Node) {if(node = =NULL) {return; } strings[node.y][node.x] = string.valueof (node.data); Drawbinarytree (Node.left); Drawbinarytree (Node.right); }/** * Calculate the number of binary tree nodes * * @param node * @return * @createTime 2015-6-17 pm 11 : 45:33 */ Private Static int Countnode(Binarytree.node Node) {if(node = =NULL) {return 0; }return 1+ Countnode (node.left) + Countnode (node.right); }/** * Calculates the coordinates of the binary tree node in the XY coordinate system * * @param node * @param nodepostion * @createTime /c3> 2015-6-19 Morning 7:53:30 * * Private Static void Countxy(Binarytree.node Node, nodepostion nodepostion) {intx =0;inty =0;Switch(nodepostion) { CaseLeft:x = (int) (Node.parent.x-linelength/(Math.pow (2, Node.level) +2)); y = node.level; Break; CaseRight:x = (int) (Node.parent.x + linelength/(Math.pow (2, Node.level) +2)); y = node.level; Break; CaseRoot:x = linelength/2; y = node.level; Break; } node.x = x; Node.y = y;if(Maxlevel < Node.level) {maxlevel = Node.level; } }/** * Node position, left, right, root * * @author Peaches */ Public Static enumNodepostion {left, right, ROOT}}
Call
/** * * @auther Wuwang * @createTime 2015-6-17 pm 11:17:45 * * PackageTree.binarytree;/** * * * * * * @author Peaches * * Public class treemain { Public Static void Main(string[] args) {BinaryTree BinaryTree =NewBinaryTree (). Buildtree (0,8, at,Ten, -, A, -,1,2,3, the,675,826,7,0,8, at,Ten, -, A, -,1,2,3, the,675,826,7); System.out.println (BinaryTree); Binarytreeutils.drawbinarytree (BinaryTree); }}
Draw a two-fork tree from the console using a Cartesian coordinate system