Redis Research-3.4 Why use a redis jump table

Source: Internet
Author: User

For several days did not put up the notes, one is busy these days in a number of bids and project papers, Haha, there is also a drawing is also more time-consuming, if there are good drawing suggestions or tools, trouble recommended, thank you. Nonsense not much to say, directly into today's two topics: two fork search tree, balanced binary tree.

1. Two-fork Search tree (BST)

The definition of a binary search tree is simple: it is a two-fork tree and satisfies two conditions: if there is a left child, the left child's keyword must not be greater than the parent node's keyword, if there is a right child, then the right child's keyword is not less than the parent node's keywords. At the same time, the left and right sub-trees are two-fork search tree.

Bst_1

The results of the middle sequence traversal are: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

Bst_2

The results of the middle sequence traversal are: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

Now there's a scene, join me to find a keyword in this tree 11, if not, then add to this tree, the query steps are:

1. For bst_1, the root node is 6, because 11 is greater than 6, then find the right child of the root node (because the right child of the binary search tree is not less than the parent node);

2. The right child's value is 9, less than 11, continue to check his right child;

3. The right child's value is 10, less than 11, continue to check his right child;

4. Because there is no right child, you need to add it as the right child.

For Bst_2, we just have to keep looking for the right child. Let's look at a binary search tree:

BST3

The results of the middle sequence traversal are: 1, 2, 3, 4, 5, 6, 7, 9, 10

Now I'm going to find a node where the keyword is a node of 8, and if it's not in the tree, add it to the tree as a node:

1. Root node is 6, less than 8, find right child;

2. The right child's value is 9 greater than 8, looking for his left child;

3. The left child's value is 7 less than 8, looking for his right child;

4. Because there is no right child, so add it as 7 right child.

From the above several processes we found a rule: in the dynamic search process, we do not need to reconstruct the structure, just on the leaf node to operate, very convenient.

What if you want to remove a node?

There are several things to consider when deleting a node:

1. The node is a leaf node, so it does not have to be reconstructed;

2. The node only Zuozi or right subtree, then the left subtree or the right subtree root, to delete the node on the parent node can be;

3. What if the node has both?

We look at it one by one:

1. The node is a leaf node.

We want to delete the node in BST3 7, then follow the query steps to find this node, found that he has no left subtree, there is no right subtree, then directly deleted, and the node's parent node of the left child or right child (depending on whether the node is left child or right child) can be emptied.

2. The node has only left dial hand trees.

We want to delete node 4 in BST3, then follow the query steps to find this node, found that he only left dial hand tree, so as long as the node is deleted, and the left child of the node is placed in the location of the node to change.

3. The node has only the right subtree:

We want to delete node 5 in BST2, then we find this node according to the query steps and find that he has only a child tree, so as long as the node is deleted and the right kid of the node is placed in the node's position.

4. The node has a left dial hand tree and a right subtree.

We are now going to delete the root node in the BST1, then the middle sequence traverses the left subtree of the node, and the node before that node is the predecessor of the node to be deleted.

For example, the middle sequence traversal of the left subtree of the above root node is: 1, 2, 3, 5, then, I want to delete the precursor of node 6, replace the predecessor of the node to be deleted, and set the left and right point.

, let's look at a BST now.

Now I want to delete node 9, follow the above steps, 9 have left and right children, so first do 9 of the left subtree of the middle sequence traversal, find 9 precursor is 8.5, and then to replace. The end result is:

, say so much, or on the code bar, this time with Java code:

Package Com.tang.bst;import Java.util.arraylist;import Java.util.iterator;import java.util.list;public class BST< Key extends comparable<key>> {bstnode<key> root;public BST () {}/** * Insert a node * * @param data section Point Data * @return */public boolean insert (Key data) {if (null = = data) return false;if (null = = This.root) {this.root = new BS tnode<key> (data); this.root.parent = Null;this.root.leftchild = Null;this.root.rightchild = Null;return true;} else {return Insertbst (this.root, data);}} Private Boolean Insertbst (bstnode<key> node, Key data) {//left branch, right branch judgment int lorr = node.data.compareTo (data); if (Lorr = = 0) {return false;} else if (Lorr < 0) {//right subtree if (null = = Node.rightchild) {bstnode<key> rchild = new Bstnode&lt ; key> (data); rchild.parent = Node;node.rightchild = Rchild;return true;} else {return Insertbst (node.rightchild, data);}} else {//left dial hand tree if (null = = Node.leftchild) {bstnode<key> lchild = new bstnode<key> (data); lchild.parent =Node;node.leftchild = Lchild;return true;} else {return Insertbst (node.leftchild, data);}}} /** * In this tree, specify a node, and then begin a non-recursive way to traverse the two-tree with this node as the root node * * @param node * is designated as the root node * @return */public LIST&LT;BSTNODE&L T Key>> norecurseinordertraverse (Key data) {if (null = = This.root) return null; bstnode<key> beginroot = null;if (null = = data) {Beginroot = this.root;//if no node is specified as the root node, start with the root node of the tree} else {bstnode& Lt key> SNode = Searchbst (this.root, data); beginroot = SNode;} list<bstnode<key>> stack = new arraylist<bstnode<key>> (); list<bstnode<key>> retstack = new arraylist<bstnode<key>> (); if (null! = Beginroot) {Stack.add ( Beginroot);} while (!stack.isempty ()) {while (beginroot! = NULL && null! = beginroot.leftchild) {beginroot = Beginroot.leftchil D;stack.add (beginroot);} if (!stack.isempty ()) {bstnode<key> Tmpnode = Stack.get (Stack.size ()-1); Stack.remove (Stack.size ()-1); Retstack.add (Tmpnode); if (Tmpnode! = null &&Amp Null! = Tmpnode.rightchild) {beginroot = Tmpnode.rightchild;stack.add (Beginroot);}}} return retstack;} /** * Find the specified node, and if not, add him to the tree * * @param data * @return 0: Indicates that there is a new node in the original tree, 1: Indicates that there is an error in the query. */public boolean search (Key da TA) {if (null = = data) {return false;} if (null = = This.root) {return false;} Return Searchbst (this.root, data) = = null? False:true;} Private bstnode<key> Searchbst (bstnode<key> searhnode, Key data) {if (null = = data) {return null;} if (null = = Searhnode) {return null;} int Lorr = searhNode.data.compareTo (data), if (Lorr > 0) {//To go to the left subtree to find return Searchbst (searhnode.leftchild, data);} else I F (Lorr = = 0) {//has been found, go directly back to return Searhnode;} else {//To go to the right subtree to find return Searchbst (searhnode.rightchild, data);}} /** * Find the specified node, if not, insert node * * @param data * @return */public boolean searchorinsert (Key data) {return search (data) = = Fal Se? Insert (data): true;} /** * Delete the specified node * * @param data * @return */public boolean Delete (Key data) {if (null = = This.root | | null = = dATA) {return false;} bstnode<key> node = searchbst (this.root, data); if (null = = node) {return false;} bstnode<key> parent = node.parent; Bstnode<key> leftchild = node.leftchild;if (null = = Node.rightchild) {//Because there is no right child, so just reconnect her left child to be able if (null = = Pare NT) {//indicates that he is the root node if (null! = leftchild) {node.leftChild.parent = null;} else {this.root = null;}} else {Node.parent.leftChil d = leftchild;}} else if (null = = Node.leftchild) {//Because there is no left child, just reconnect her right child to be able if (parent = = NULL) {//Description He is the root node if (null! = Node.rightchild) { node.rightChild.parent = null;} else {this.root = null;}} else {node.parent.rightChild = Node.rightchild;}} else {//Both the left dial hand tree, and the right subtree//The left subtree of this node in the sequence, find the precursor of this node//This precursor is either a leaf node, or only the left node System.out.println (node.rightchild==null); List<bstnode<key>> Stack=norecurseinordertraverse (Node.leftChild.data); Bstnode<key> Prenode=stack.get (Stack.size ()-1); Bstnode<key> rightnode=node.rightchild;node.data=prenode.data;if (prenode.leftchild!=null) {node.leftChild= PrenoDe.leftchild;} if (prenode.parent!=null) {if (PreNode.parent.leftChild.data.compareTo (prenode.data) ==0) {PreNode.parent.leftChild =null;} else{prenode.parent.rightchild=null;//There is a problem here}}node.rightchild=rightnode; System.out.println (node.rightchild==null);} return true;} public static void Main (string[] args) {bst<integer> BST = new bst<integer> (); Bst.insert (new Integer (6)); BST . Insert (New Integer (5)), Bst.insert (New Integer (9)), Bst.insert (New Integer (2)), Bst.insert (New Integer (8)); Bst.insert (New Integer), Bst.insert (New Integer (1)), Bst.insert (New Integer (4)), Bst.insert (New Integer (3)); list<bstnode<integer>> stack = bst.norecurseinordertraverse (null); for (Iterator<bstnode<integer >> iterator = Stack.iterator (); Iterator.hasnext ();) {bstnode<integer> Bstnode = (bstnode<integer>) iterator.next (); System.out.print (Bstnode.data + "---");} Bst.delete (New Integer (5)); System.out.println (after "delete"); stack = Bst.norecurseinordertraverse (null); for (Iterator<bstnode<Integer>> iterator = Stack.iterator (); Iterator.hasnext ();) {bstnode<integer> Bstnode = (bstnode<integer>) iterator.next (); System.out.print (Bstnode.data + "---");}}}

Through the above study, we can see that the binary search tree is affected by the input sequence is very large, it is possible to form a linear table, the efficiency is very low. What's the whole thing? Watch the next section, and welcome to the qq:359311095 discussion

Redis Research-3.4 Why use a redis jump table

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.