Package H1;import Java.lang.thread.state;import Java.util.stack;public class Tree {public static void main (string[] args {Tree tree = new Tree (); Tree.insert (8); Tree.insert (5); tree.insert; Tree.insert (4); Tree.insert (6); Tree.insert (9 ); Tree.insert (n); Tree.postorder (Tree.root); System.out.println (); tree. Posttraverse (tree.root);} Private Node Root;public boolean find (int key) {Boolean flag = true; Node current = Root;while (Current.getidata ()! = key) {if (Key < Current.getidata ()) {current = Current.getleftchild (); } else {current = Current.getrightchild ();} if (current = = null) {flag = False;break;}} return flag;} /** * For the insertion number of nodes operation * * @param iData */public void Insert (int iData) {//Make a new nodenode NewNode = new node (); newnode.se Tidata (IData); if (root = null) {root = NewNode;} else {Node current = root; Node Parent;while (true) {parent = Current;if (IData < Current.getidata ()) {//Go leftcurrent = Current.getleftchild () ; if (current = = null) {parent.setleftchild (newNode); return;}} else {//Go rightcurrent = Current.getrightchild (); if (current = = null) {parent.setrightchild (newNode); return;}}}} public void Delete (int key) {}/** * is used to traverse the data node when accessing the * * @param node */public void Visit (node node) {System.out.print (node). Getidata () + "");} /** * First Order traversal, recursive implementation (root node, left subtree, right subtree,) * * @param node */public void Preorder (node node) {if (node = = null) {return;} Visit (node); if (node.getleftchild () = null) {preorder (Node.getleftchild ());} if (node.getrightchild () = null) {preorder (Node.getrightchild ());}} First-order traversal non-recursive implementation public void Pretraverse (node node) {stack<node> Stack = new stack<node> (); Stack.push (node);//First Put the root node into the stack while (!stack.isempty ()) {//Summary: What is the difference between pop and peek in the stack? Pop: Removes and returns the element at the top of the stack//peek: Only the element that returns the top of the stack is node Node2 = (node) stack.pop (); visit (node2); if (Node2.getrightchild ()! = null) { Stack.push (Node2.getrightchild ());} if (node2.getleftchild () = null) {Stack.push (Node2.getleftchild ());}}} Middle sequence traversal non-recursive implementation, first always find the leftmost child,//And then the right subtree into the stack public void Intraverse (node node) {stack<node> StACK = new stack<> (); Node p = root;while (P! = NULL | |!stack.isempty ()) {if (P! = null) {Stack.push (p);p = P.getleftchild ();} else {p = stack. Pop (); visit (p);p = P.getrightchild ();}}} Post-traversal non-recursive implementation public void Posttraverse (node node) {stack<node> Stack = new stack<> (); Node p = root; Node pre = P;while (P! = NULL | |!stack.isempty ()) {///first also find the leftmost child of the tree if (P! = null) {Stack.push (p);p = P.getleftchild ();} El Se {if (stack.isempty ()) {return;} p = Stack.peek (); Gets the element at the top of the stack, but peek is not removed! if (p.getrightchild () = null && p.getrightchild ()! = Pre) {p = P.getrightchild ();} else {p = stack.pop (); Visit (P) ;p re = p;p = null;}}} /** * Middle Sequence traversal (recursive implementation): Left dial hand tree, data, right subtree * * @param node */public void inorder (node node) {if (node = = null) {return;} if (node.getleftchild () = null) {inorder (Node.getleftchild ());} Visit (node); if (node.getrightchild () = null) {inorder (Node.getrightchild ());}} /** * post-sequential traversal, recursive implementation (left subtree, right subtree, root node) * * @param node */public void Postorder (node node) {if (node = = null) {return;} IF (node.getleftchild () = null) {Postorder (Node.getleftchild ());} if (node.getrightchild () = null) {Postorder (Node.getrightchild ());} Visit (node);} public int getminvalue () {int min = 0; Node current = Root;while (Current.getleftchild ()! = null) {current = Current.getleftchild ();} return Current.getidata ();} public int getmaxvalue () {int min = 0; Node current = Root;while (Current.getrightchild ()! = null) {current = Current.getrightchild ();} return Current.getidata ();} /** * When deleting a node, to classify the discussion: ①: When the node is a leaf node, is directly deleted; ②: When the node has a child, ③: When the node has 3 children * * @param key * @return */public Boolean re Move (int key) {Boolean flag = True;return flag;}}
Java implementation two Fork sort tree