the basic structure of a tree
public class Treenode<t>{public treenode<t> leftnode;public treenode<t> rightnode;public T data; Public TreeNode (T data) {this.data = data;}}
1. Build a tree (in this article, using an expression tree as an example, build with a suffix expression, I've covered in the previous article how to convert a middle order to a subsequent expression)
Principle: Using stacks. Step:A. The current character is a character or a number, then a node is built directly, then into the stack
B. The current character is an operator, then two nodes are removed from the stack, and the two nodes are built as child nodes to build a tree into the stack
C. Finally, the only remaining nodes in the stack are returned, which is the tree to be built
Constructs a character descriptor, which is the mathematical expression public static treenode<character> Gettree (String obj) {//treenode<character> node2=new Treenode<character> (NULL); stack<treenode<character>> stack = new stack<treenode<character>> (); for (int i = 0; i < Obj.le Ngth (); i++) {Char temp = Obj.charat (i); if (Character.isletterordigit (temp))//If it is a letter or number, build a tree directly into the stack {Treenode<character > node = new treenode<character> (temp); Stack.push (node);} else//operator, remove two tree nodes from the stack and merge them into one node, with the operator {treenode<character> node = new treenode<character> (temp); There are operators with at least two elements of the stack Node.rightnode = Stack.pop (); node.leftnode = Stack.pop (); Stack.push (node);}} return Stack.pop ();}
2. Pre-order, sequence, orderprint a tree, there's nothing to say
/* Recursively print a tree first order, middle sequence, post order *///traversal of a tree, (root) public String Printtreepreoreder (treenode<t> node) {string obj = ""; if (node ! = null) {obj = node.data;obj + = Printtreepreoreder (node.leftnode); obj + = Printtreepreoreder (Node.rightnode);} return obj;} The middle sequence traverses a tree (left root right) public String Printtreeinfixoreder (treenode<t> node) {string obj = ""; if (node! = null) {if (node.left Node! = null) obj + = "("; obj + = Printtreeinfixoreder (node.leftnode); obj + = node.data;obj + = Printtreeinfixoreder ( Node.rightnode); if (node.rightnode! = null) obj + = ")";} return obj;} Subsequent traversal of a tree (left and right root) public String Printtreepostoreder (treenode<t> node) {string obj = ""; if (node! = null) {obj + = Printtre Epostoreder (node.leftnode); obj + = Printtreepostoreder (node.rightnode); obj + = Node.data;} return obj;}
3. Get the height of the tree (using recursion)
Principle: Height of the tree root = maximum child's height +1
Get a tree forehead height public int getheight () {if (this==null) return 0;if (This.leftnode = = NULL && This.rightnode = = null) retu RN 1;int lheight = This.leftNode.getHeight (); int rheight = This.rightNode.getHeight (); return lheight > Rheight? Lheight + 1:rheight + 1;}
4. Layer k of the print tree (recursive implementation)
Principle: Can be understood as the print root of the K-layer, that is, print the root of the child's k-1 layer, when k=0, can be directly printed out
Print the level layer of a tree public void Printtreeatlevel (treenode<t> node, int levels) {if (node = = NULL | | Level < 0) return;if (level = = 0)//reach the target layer System.out.print (node.data);p rinttreeatlevel (Node.leftnode, level-1);p rint (1);/// Print a space Printtreeatlevel (Node.rightnode, level-1);}
5. Print a tree by level
I have implemented two methods:
Method One:
Knowing the height of the tree, the height of each layer is printed in turn. But this approach, when printing any layer, will start at the root node, wasting time
To print a tree in the form of a tree, the public void Printtree () {int height = getheight () must be printed at the level, and for (int i = 0; i < height; i++) {Print (Height * 2-i);p Rinttreeatlevel (this, i); System.out.println ();}}
Method Two:
Implemented with two queues, queue 1 holds all the same leaf nodes, queue 2 holds the child node that is being accessed to save queue 1, and after queue 1 is accessed, queue 2 is added to queue 1 (this is an empty queue), and queue 2 is emptied
public void Printtreebyqueue (treenode<t> root) {queue<treenode<t>> queue1, queue2;queue1 = new Linkedlist<treenode<t>> (); queue2 = new linkedlist<treenode<t>> (); Queue1.add (root); Queue2.add (Root), while (!queue2.isempty ()) {queue2.clear (), while (!queue1.isempty ()) {treenode<t> node = Queue1.poll ();p rint (2); System.out.print (Node.data); if (node.leftnode! = null) Queue2.add (Node.leftnode); if (node.rightnode! = null) Queue2.add (Node.rightnode);} Queue1.addall (queue2); System.out.println ();}}
Tree-related operations of data structures (Java implementation) (III)