Binary search Tree
Also called binary sort tree.
For each node in the tree X, the value of the item in all of its left subtrees is less than the item in the X node, and the value of the item in all right subtrees is greater than the item of X.
The basic operations that need to be implemented are:
1. include:
Determines whether an element is contained in a binary lookup tree, and returns True if it exists, otherwise false.
The following are the steps:
(1). Judging from the root node, the root node is empty and the direct end returns false;
(2). If it is equal, the direct end returns True, and if the value is greater than the root node, the right subtree of the root node continues to be compared to the left subtree of the root node, if the value of the node is smaller;
(3). Repeat step (2) until the equal element is found to return true or the binary tree traversal ends and is not found returns false.
2. Insert:
Note that when inserting, it is necessary to determine whether the element exists in a binary tree, that is, a comparison lookup operation is required, if it is present, it is not inserted, otherwise it is inserted.
The following are the steps:
(1). Judging from the root node, the root node is empty and is inserted directly;
(2). If the equality is terminated directly, no insertion is made, and if the value is greater than the root node, the right subtree of the root node is compared, and if the value of the small root node continues to be compared with the left subtree of the roots node;
(3). The Loop of Step (2) is continued until the corresponding node is empty, and the node corresponding to that element is inserted directly here.
3. Delete:
Note in the deletion of the need to determine whether the element exists in a binary tree, that is, the need for a comparison lookup operation, if not there is no related operation, directly return, otherwise delete the corresponding node, and then adjust the binary search tree node location relationship, return.
Here's how:
(1). Judging from the root node, the Joghen node is empty and the direct operation is not returned;
(2). If it is equal, delete the node directly, then make the related node adjustment, return to return; If the value is greater than the root node, then the right subtree of the root node will continue to be compared to the left subtree of the root node, if the value of the node is smaller;
(3). Continue the loop of step (2) until the equality condition occurs before step (2), or until the corresponding two fork tree is empty and no equal value appears, then no action is returned directly.
Note: The above mentioned to make the relevant node adjustment is this, the deletion node value is the node of E, you need to find the node as the root node of the smallest right subtree or the largest left subtree node to replace the node, that is, the node's predecessor or successor to replace (precursor: Is the nodes of the left subtree of the largest node; Successor: is the smallest node in the right subtree of the node).
4. Maximum:
Depending on the nature of the binary lookup tree: The item of the right subtree is larger than the value of the Zuozi item.
Traversing from the root node until the right subtree node appears, the value of the node is the maximum value of the two-fork find tree.
5. Minimum:
As with the above lookup maximum, we just find the value of the leftmost subtree node.
6. Traversal:
As with the traversal of the binary tree, it can carry forward sequence traversal, middle sequence traversal, post-order traversal, and hierarchical traversal.
Note that the result of the middle order traversal is an ascending sequence.
There are also operations such as finding precursor nodes and subsequent nodes.
Here is my Java implementation method.
Package com.phn.tree;/** * @author Pan Hainan * @Email [email protected] * @TODO binary search tree * @date July 27, 2015 */public class FOB Inarysearchtree<e extends Comparable<e>> {//two forks find the root node of the tree private fobinarytreenode<e> root; /** * TODO No parameter constructor, initialize binary sort tree */public Fobinarysearchtree () {this.root = null; }/** * @TODO The INPUT element e is included in the two-fork sort tree * @param e INPUT element * @return True or FALSE */public Boolean contains (E e) {return This.contains2 (e, root); }/** * @TODO binary sort tree contains the specific implementation of an element * @param e INPUT element * @param node tree root node * @return True or false */ Private Boolean contains2 (e E, fobinarytreenode<e> node) {if (node = = null) return false; else {int result = E.compareto (Node.gete ()); if (Result < 0) {return contains2 (E, Node.getleftchild ()); } else if (Result > 0) {return contains2 (E, Node.getrightchild ()); } else {return true; }}}/** * @TODO binary sort tree Find node information for INPUT element in tree * @param e INPUT element * @return node information for the input element in the tree */public FO Binarytreenode<e> Search (e e) {if (root = null) {throw new RuntimeException ("The Tree is empty! "); } fobinarytreenode<e> tempnode = root; while (Tempnode! = null) {int result = E.compareto (Tempnode.gete ()); if (result = = 0) {return tempnode; } else if (Result < 0) {Tempnode = Tempnode.getleftchild (); } else {tempnode = Tempnode.getrightchild (); }} return null; }/** * @TODO binary sort tree Insert element * @param e element to be inserted * @return true or FALSE */public boolean Add (E e) { if (root = null) {root = new fobinarytreenode<e> (); Root.sete (e); return true; } fobinarytreenode<e> TempinserTnode = root; fobinarytreenode<e> Insertparentnode = new fobinarytreenode<e> (); int result = 0; while (Tempinsertnode! = null) {Insertparentnode = Tempinsertnode; result = E.compareto (Tempinsertnode.gete ()); if (Result < 0) {Tempinsertnode = Tempinsertnode.getleftchild (); } else if (Result > 0) {tempinsertnode = Tempinsertnode.getrightchild (); } else {return true; }} fobinarytreenode<e> Insertnode = new fobinarytreenode<e> (); Insertnode.sete (e); Insertnode.setparent (Insertparentnode); int cmpresult = E.compareto (Insertparentnode.gete ()); if (Cmpresult > 0) {insertparentnode.setrightchild (Insertnode); } else {insertparentnode.setleftchild (insertnode); } return true; }/** * @TODO binary sort tree To delete the INPUT element in the number E * @param e The INPUT element that will be deleted * @return True or FALSE */public boolean remove (e e) {fobinarytreenode<e> RemoveNode = This.search (e); if (RemoveNode = = null) {return false; } fobinarytreenode<e> Preremovenode = This.findprenode (RemoveNode); if (Preremovenode! = null) {Removebyprenode (RemoveNode, Preremovenode); return true; } fobinarytreenode<e> Nextremovenode = This.findnextnode (RemoveNode); if (Nextremovenode! = null) {Removebynextnode (RemoveNode, Nextremovenode); return true; } if (Removenode.getparent (). Getleftchild () = = RemoveNode) {removenode.getparent (). Setleftchild (NULL); } else {removenode.getparent (). Setrightchild (NULL); } return true; }/** * @TODO Delete the nodes in the binary sort tree and replace them with the nodes of the RemoveNode * @param the node to be deleted * @param the predecessor node of the node that will be deleted. */Preremovenode. private void Removebyprenode (fobinarytreenode< E> RemoveNode, fobinarytreenode<e> preremovenode) {if (Preremovenode.getleftchild ()! = null) { Preremovenode.getleftchild (). SetParent (Preremovenode.getparent ()); Preremovenode.getparent (). Setrightchild (Preremovenode.getleftchild ()); } else {preremovenode.getparent (). Setrightchild (NULL); } preremovenode.setparent (Removenode.getparent ()); if (Removenode.getparent (). Getleftchild () = = RemoveNode) {removenode.getparent (). Setleftchild (Preremovenode); } else {removenode.getparent (). Setrightchild (Preremovenode); } preremovenode.setleftchild (Removenode.getleftchild ()); if (removenode.getleftchild () = null) {Removenode.getleftchild (). SetParent (Preremovenode); } preremovenode.setrightchild (Removenode.getrightchild ()); if (removenode.getrightchild () = null) {Removenode.getrightchild (). SetparENT (Preremovenode); }}/** * @TODO Delete the nodes in the binary sort tree and replace their successors * @param the node to be deleted * @param the successor of the node that will be deleted Nextremovenode */private void Removebynextnode (fobinarytreenode<e> removeNode, fobinarytreenode<e> Nextremov Enode) {if (nextremovenode.getrightchild () = null) {Nextremovenode.getrightchild () . SetParent (Nextremovenode.getparent ()); Nextremovenode.getparent (). Setleftchild (Nextremovenode.getrightchild ()); } else {nextremovenode.getparent (). Setleftchild (NULL); } nextremovenode.setparent (Removenode.getparent ()); if (Removenode.getparent (). Getleftchild () = = RemoveNode) {removenode.getparent (). Setleftchild (Nextremovenode); } else {removenode.getparent (). Setrightchild (Nextremovenode); } nextremovenode.setleftchild (Removenode.getleftchild ()); if (Removenode.getleftchild ()! = NULL) {Removenode.getleftchild (). SetParent (Nextremovenode); } nextremovenode.setrightchild (Removenode.getrightchild ()); if (removenode.getrightchild () = null) {Removenode.getrightchild (). SetParent (Nextremovenode); }}/** * @TODO get the precursor node of the input node * @param node Input node * @return The predecessor node of the input node */public Fobinarytreenode< ; e> Findprenode (fobinarytreenode<e> node) {if (node = = null) {throw new RuntimeException ("Incoming node is empty! "); } fobinarytreenode<e> Tempnode = Node.getleftchild (); if (Tempnode = = null) {//throw new RuntimeException ("Incoming node no precursor!") "); return null; } fobinarytreenode<e> Prenode = Tempnode; Tempnode = Tempnode.getrightchild (); while (Tempnode! = null) {Prenode = Tempnode; Tempnode = Tempnode.getrightchild (); } return Prenode; }/** * @TODO get the subsequent node of the input node * @pAram node Input node * @return The subsequent node of the input node */public fobinarytreenode<e> Findnextnode (fobinarytreenode<e> n ODE) {if (node = = null) {throw new RuntimeException ("Incoming node is empty! "); } fobinarytreenode<e> Tempnode = Node.getrightchild (); if (Tempnode = = null) {//throw new RuntimeException ("Incoming node no successor!") "); return null; } fobinarytreenode<e> nextnode = Tempnode; Tempnode = Tempnode.getleftchild (); while (Tempnode! = null) {nextnode = Tempnode; Tempnode = Tempnode.getleftchild (); } return nextnode; }/** * @TODO gets the largest element of the two-fork sort tree * @return The largest element of the two-fork sort tree */public E getmaxe () {return this.getmaxe (root ); }/** * @TODO get the implementation of the largest element of the input binary sort tree * @param node input binary sort tree root node * @return The largest element of the input binary sort tree */private E GE Tmaxe (fobinarytreenode<e> node) {if (node ==null) {return null; } while (Node.getrightchild ()!=null) {node = Node.getrightchild (); } return Node.gete (); }/** * @TODO gets the smallest element of the two-fork sort tree * @return The smallest element of the two-fork sort tree */public E Getmine () {return this.getmine (root ); }/** * @TODO get the implementation of the smallest element of the input binary sort tree * @param node input binary sort tree root node * @return The smallest element of the input binary sort tree */private E GE Tmine (fobinarytreenode<e> node) {if (node ==null) {return null; } while (Node.getleftchild ()!=null) {node = Node.getleftchild (); } return Node.gete (); }/** * @TODO pre-order traversal output binary sort tree elements * @return binary sort tree elements in string form */public string Preorderprint () {Stringbu Ffer sb = new StringBuffer ("["); This.preorder (root, SB); Sb.replace (Sb.length ()-2, Sb.length ()-1, "]"); return sb.tostring (); }/** * @TODO The concrete implementation of the elements in the binary sorting tree of the pre-sequence traversal * @param node two forks the root node of the sorting tree * @param sb to save the traversal results of the elements in the two-fork sorting tree in SB * @return two forks The string form of an element in the sort tree */ Private String Preorder (fobinarytreenode<e> node, StringBuffer sb) {if (node! = null) {Sb.app End (node + ","); Preorder (Node.getleftchild (), SB); Preorder (Node.getrightchild (), SB); } return sb.tostring (); }/** * @TODO middle sequence traversal output binary sort tree element * @return binary sort tree elements in string form */public string Inorderprint () {Stringbuf Fer sb = new StringBuffer ("["); This.inorder (root, SB); Sb.replace (Sb.length ()-2, Sb.length ()-1, "]"); return sb.tostring (); }/** * @TODO The concrete implementation of the elements in the binary sorting tree of the sequence traversal * @param node two forks the root node of the sorting tree * @param sb to save the traversal results of elements in the two-fork sorting tree in SB * @return two forks The string form of an element in the sort tree */private string Inorder (fobinarytreenode<e> node, StringBuffer sb) {if (node! = NULL) {inorder (Node.getleftchild (), SB); Sb.append (node + ","); Inorder (Node.getrightchild (), SB); } return sb.tostring (); }/** * @TODO post-order traverse output binary sorting tree elements * @return The string form of the elements in the binary sort Tree */public string Postorderprint () {stringbuffer sb = new StringBuffer ("["); This.postorder (root, SB); Sb.replace (Sb.length ()-2, Sb.length ()-1, "]"); return sb.tostring (); }/** * @TODO The concrete implementation of the elements in the binary sorting tree of the sequence traversal * @param node two forks the root node of the sorting tree * @param sb to save the traversal results of elements in the two-fork sorting tree in SB * @return two forks The string form of an element in the sort tree */private string Postorder (fobinarytreenode<e> node, StringBuffer sb) {if (node! = NULL ) {Postorder (Node.getleftchild (), SB); Postorder (Node.getrightchild (), SB); Sb.append (node + ","); } return sb.tostring (); }}
Two fork tree node class:
Package com.phn.tree;/** * @author Pan Hainan * @Email [email protected] * @TODO binary tree node * @date July 27, 2015 */public class FOB Inarytreenode<e extends comparable<e>>{private e e; Private fobinarytreenode<e> parent; Private fobinarytreenode<e> leftchild; Private fobinarytreenode<e> Rightchild; Public Fobinarytreenode () {super (); }/** * @param e * @param parent * @param leftchild * @param rightchild */Public Fobinarytreeno De (e E, fobinarytreenode<e> parent, fobinarytreenode<e> leftchild, fobinarytreenode<e> RightC Hild) {super (); THIS.E = e; This.parent = parent; This.leftchild = Leftchild; This.rightchild = Rightchild; } public e Gete () {return e; } public void Sete (e e) {this.e = e; } public fobinarytreenode<e> GetParent () {return parent; } public void SetParent (fobinarytreenode<e> paRent) {this.parent = parent; } public fobinarytreenode<e> Getleftchild () {return leftchild; } public void Setleftchild (fobinarytreenode<e> leftchild) {this.leftchild = Leftchild; } public fobinarytreenode<e> Getrightchild () {return rightchild; } public void Setrightchild (fobinarytreenode<e> rightchild) {this.rightchild = Rightchild; } @Override Public String toString () {return e.tostring (); }}
Test code:
Package com.phn.tree;/** * @author Pan Hainan * @Email [email protected] * @TODO * @date July 29, 2015 */public class Fobinary searchtreetest {public static void main (string[] args) {fobinarysearchtree<string> Fobst = new Fobinarys Earchtree<string> (); Fobst.add ("F"); Fobst.add ("E"); Fobst.add ("B"); Fobst.add ("a"); Fobst.add ("D"); Fobst.add ("C"); Fobst.add ("I"); Fobst.add ("G"); Fobst.add ("H"); Fobst.add ("J"); System.out.println (Fobst.preorderprint ()); System.out.println (Fobst.inorderprint ()); System.out.println (Fobst.postorderprint ()); System.out.println (Fobst.contains ("a")); System.out.println (Fobst.contains ("V")); System.out.println (Fobst.remove ("a")); System.out.println (Fobst.preorderprint ()); System.out.println (Fobst.inorderprint ()); System.out.println (Fobst.postorderprint ()); System.out.println (Fobst.remove ("E")); SystEm.out.println (Fobst.preorderprint ()); System.out.println (Fobst.inorderprint ()); System.out.println (Fobst.postorderprint ()); System.out.println (Fobst.remove ("K")); System.out.println (Fobst.preorderprint ()); System.out.println (Fobst.inorderprint ()); System.out.println (Fobst.postorderprint ()); System.out.println (Fobst.getmaxe ()); System.out.println (Fobst.getmine ()); }}
Test results:
Copyright NOTICE: This article for Bo Master original article, if you need to reprint please specify the source and attached link, thank you.
Java Data Structure-tree application-Two fork find tree