One or two fork sort tree definition
1. Definition of two-fork sorting tree
Binary sort tree, also known as the binary search tree, is called two-prong lookup (binary). It is defined as: a two-fork sort tree or an empty tree, or a two-fork tree that satisfies the following properties:
① if its left subtree is not empty, the values of all nodes on the left subtree are smaller than the value of the root node;
② if its right subtree is not empty, the values of all nodes on the right subtree are greater than the values of the root nodes;
③ the left and right sub-tree itself is a binary sorting tree.
These properties are called binary sort Tree Properties (BST property), so the binary sort tree is actually a two-fork tree satisfying the BST nature.
2. The nature of the two-fork sorting tree
By traversing the binary sort tree in the middle order, the resulting sequence of sequential traversal is an ascending sequential sequence.
3. Inserting a two-fork sort tree
Insert a new node in the binary sort tree, to ensure that the inserted two-fork tree still conforms to the definition of the binary sort tree.
Insert procedure:
If the binary sort tree is empty, the insertion node *s as the root node into the empty tree;
When non-empty, will be inserted node keyword S->key and tree root keyword t->key to compare, if S->key = T->key, then no need to insert, if s->key< T->key, then inserted into the root of the left subtree, if s- >key> T->key, it is inserted into the right subtree of the root. The insertion process in the subtree is the same as the insertion process in the tree, so proceed until the node *s is inserted into the two-fork sort tree as a new leaf, or until a node with the same keyword is found.
4. Finding a two-fork sorting tree
Assuming that the root node pointer of the binary sort tree is root and the given keyword value is K, the lookup algorithm can be described as:
① initial value: q = root;
② if K = Q-> Key, then the lookup succeeds, the algorithm ends;
③ otherwise, if K Q-> Key, and Q of the left subtree is not empty, then Q Zogen send Q, turn the step ②; otherwise, find the failure, the end algorithm;
④ otherwise, if K > Q-> Key, and the right subtree of Q is not empty, then Q is sent to the right subtree Q, the step ②; otherwise, the search fails, the algorithm ends.
5. Deletion of two-fork sorting tree
Assuming that the deleted node is *p, whose parents are *f, without losing generality, *p is the left child of *f, the following three kinds of situations are discussed:
⑴ If the node *p is a leaf node, simply modify the pointer of the parent node *f.
⑵ If the node *p only left dial hand tree pl or only the right sub-tree PR, then as long as the PL or PR becomes its parent node of the left sub-tree can be.
⑶ if the left and right subtrees of the *p are not empty, first find the *p (or successor) node *s (Note that *s is the most right node in the left subtree of *p, its right-link domain is empty, and then there are two ways to do it: ① the left subtree of the *p directly to the left strand of the *p's parent node *f, and the *p right subtree chain to * The middle sequence of P is *s on the right strand of the front node. ② the *p *s instead of *p (that is, copying the *s data into the *p), the *s left subtree is chained to the left (or right) chain of *s's parent node *q.
6, Binary Tree traversal
There are three ways to traverse a binary tree, as follows:
(1) The Pre-order traversal (DLR), first accesses the root node, then traverses the left subtree, and finally traverses the right sub-tree. Jenkgen-left-right.
(2) The middle sequence Traversal (LDR), first traversing the left subtree, then accessing the root node, and finally traversing the right subtree. Précis-writers left-root-right.
(3) Post-order traversal (LRD), first traverse the left subtree, and then traverse the right subtree, and finally access the root node. Précis-writers left-right-root.
Second, code writing
1. Definition of tree node class
Package com.lin;/** * Feature Summary: * * @author Linbingwen * @since August 29, 2015 */public class TreeNode {public Integer data;/* The parent node of the node */public TreeNode parent;/* The node's left child node */public TreeNode left;/* The right child node of the node */public TreeNode right;public TreeNode (Integer data) {this.data = data;} @Overridepublic String toString () {return "TreeNode [data=" + Data + "]";}}
2. The definition of binary sorting tree
Package com.lin;/** * Feature Summary: sort/Balance binary tree * * @author Linbingwen * @since August 29, 2015 */public class Searchtree {public T Reenode Root; Public long size;/** * Add node to tree * @author Linbingwen * @since August 29, 2015 * @param data * @return Boolean Insert successfully returns True */PUBL IC Boolean Addtreenode (Integer data) {if (null = = root) {root = new TreeNode (data); SYSTEM.OUT.PRINTLN ("Data successfully inserted into the Balanced binary tree"); return true; TreeNode TreeNode = new TreeNode,//data to be inserted TreeNode CurrentNode = root; TreeNode Parentnode;while (true) {parentnode = currentnode;//Save parent node//Insert data smaller than parent node if (Currentnode.data > data) {CURRENTN Ode = currentnode.left;//The left child node of the current parent node is empty if (null = = CurrentNode) {parentnode.left = Treenode;treenode.parent = parentnode; SYSTEM.OUT.PRINTLN ("Data successfully inserted into the two-fork lookup tree"); Size++;return true;} The inserted data is larger than the parent node, or else if (Currentnode.data < data) {CurrentNode = currentnode.right;//the right child node of the current parent node is empty if (null = = Currentnod E) {parentnode.right = Treenode;treenode.parent = parentnode; SYSTEM.OUT.PRINTLN ("Data successfully inserted into the two-fork lookup tree"); size++;return true;}} else {System.out.println ("input data is the same as node's data"); return false;}}} /** * Find Data * @author Linbingwen * @since August 29, 2015 * @param data * @return TreeNode */public TreeNode findtreenode (Inte Ger data) {if (null = = root) {return null;} TreeNode current = Root;while (current! = null) {if (Current.data > data) {current = Current.left;} else if (Current.data < data) {current = Current.right;} else {return current;}} return null;}}
There's only one way to add and find here for the time being.
3, front, middle and rear traversal
Package com.lin;import java.util.stack;/** * Feature Summary: * * @author Linbingwen * @since August 29, 2015 */public class Treeorder {/** * Recursive implementation of pre-order traversal * @author Linbingwen * @since August 29, 2015 * @param treeNode */public static void Preordermethodone (TreeNode TreeNode) {if (null! = TreeNode) {System.out.print (Treenode.data + ""); if (null! = TREENODE.L EFT) {Preordermethodone (treenode.left);} if (null! = Treenode.right) {preordermethodone (treenode.right);}}} /** * Loop for pre-order traversal * @author Linbingwen * @since August 29, 2015 * @param treeNode */public static void Preordermethodtwo (Treenod E TreeNode) {if (null! = TreeNode) {stack<treenode> Stack = new stack<treenode> (); Stack.push (TreeNode); while (!stack.isempty ()) {TreeNode Tempnode = Stack.pop (); System.out.print (Tempnode.data + "");//Right child node is not NULL, first put right child node in if (null! = Tempnode.right) {Stack.push (tempnode.right);} Put the right child node in the left child node, and next take if (null! = Tempnode.left) {Stack.push (tempnode.left);}}} /** * Recursive implementation of sequential traversal * @author Linbingwen * @since August 29, 2015 * @param treeNode */public static void Medordermethodone (TreeNode treeNode) {if (null! = TreeNode) {if (Null! = Treenode.left) {Medordermethodone (treenode.left);} System.out.print (Treenode.data + ""); if (null! = Treenode.right) {medordermethodone (treenode.right);}}} /** * Loop implementation of the sequence traversal * @author Linbingwen * @since August 29, 2015 * @param treeNode */public static void Medordermethodtwo (Treenod e treeNode) {stack<treenode> Stack = new stack<treenode> (); TreeNode current = TreeNode; while (current! = NULL | |!stack.isempty ()) {while (current! = null) {Stack.push (current); current = Current.left; } if (!stack.isempty ()) {current = Stack.pop (); System.out.print (current.data+ ""); current = Current.right; }}}/** * Recursive implementation of post-traversal * @author Linbingwen * @since August 29, 2015 * @param treenode */public static void Postordermethodone (TreeNode TreeNode) {if (null! = TreeNode) {if (null! = Treenode.left) {Postor Dermethodone (treenode.left);} if (null! = Treenode.right) {postordermethodone (treenode.right);} System.out.print (Treenode.data + "");}} /** * loop for post-traversal * @author Linbingwen * @since August 29, 2015 * @param treeNode */public static void Postordermethodtwo (Treeno De TreeNode) {if (null! = TreeNode) {stack<treenode> Stack = new stack<treenode> (); TreeNode current = TreeNode; TreeNode Rightnode = Null;while (current! = NULL | |!stack.isempty ()) {while (current! = null) { Stack.push (current); current = Current.left; } current = Stack.pop (); while (current! = null && (Current.right = = NULL | | Current.right = = Rightnode)) {System.out.print (Current.data + ""); Rightnode = current; if (Stack.isempty ()) {SysTem.out.println (); Return } current = Stack.pop (); } stack.push (current); current = Current.right; } }}}
4. How to use
Package com.lin;/** * Feature Summary: * * @author Linbingwen * @since August 29, 2015 */public class Searchtreetest {/** * @author Lin Bingwen * @since August 29, 2015 * @param args */public static void main (string[] args) {Searchtree tree = new Searchtree (); Tree.addtreenode (tree.addtreenode); tree.addtreenode; tree.addtreenode; Tree.addtreenode (10);; Tree.addtreenode (+); Tree.addtreenode (Tree.addtreenode); Tree.addtreenode (+); Tree.addtreenode (40); System.out.println ("==============================" + "start with recursive pre-order Traversal" + "=============================="); Treeorder.preordermethodone (Tree.root); System.out.println (); System.out.println ("==============================" + "start with the loop's pre-order traversal" + "=============================="); Treeorder.preordermethodtwo (Tree.root); System.out.println (); System.out.println ("==============================" + "start with recursive post-sequential traversal" + "=============================="); Treeorder.postordermethodone (Tree.root); System.out.println (); System.out.println ("==============================" + "use the cycle of the post-Calendar "+" ============================== "); Treeorder.postordermethodtwo (Tree.root); System.out.println (); System.out.println ("==============================" + "start with recursive middle order traversal" + "=============================="); Treeorder.medordermethodone (Tree.root); System.out.println (); System.out.println ("==============================" + "takes the middle sequence traversal of the loop to start" + "=============================="); Treeorder.medordermethodtwo (Tree.root);}}
Output Result:
Similarly, the search process is as follows:
TreeNode node = tree.findtreenode (100); SYSTEM.OUT.PRINTLN (node);
The result is correct.
Copyright NOTICE: This article for Bo Master Lin Bingwen Evankaka original article, reproduced please indicate the source Http://blog.csdn.net/evankaka
Java Two fork sort tree