Splay Stretching Tree Learning notes

Source: Internet
Author: User

Splay Stretching tree

Have a splay entry must see article --csdn link

Classic citation

Space Efficiency: O (n)Time Efficiency: O (log n) insert, find, deleteCreator: Daniel sleator and Robert Tarjan
Pros: Each query adjusts the structure of the tree so that entries with high query frequency are closer to the root.
Tree Rotation
 The rotation of a tree is the basis of splay, and for binary search trees, the rotation of the tree does not destroy the structure of the search tree.   splaying splaying is the basic operation in splay tree, in order to get the queried entries closer to the root, splay tree uses the rotation of the trees, while ensuring that the properties of the binary sort tree are not changed. the operation of the splaying is affected by the following three factors:
    • Node x is the left child or right child of parent node p
    • Node P is not the root node, if not
    • Node P is the left child or right child of parent node G
There are also three basic operations:  Zig Step
when p is the root node, the zip step operation is performed. when X is the left child of P, the X is right-handed;when x is the right child of P, the X-L.   Zig-zig Stepwhen P is not the root node and x and P are the same as the left child or right child, the Zig-zig action is performed. when x and P are the same for the left child, the P and X are then clockwise.when x and P are the same right child, then p and X are left in turn.    Zig-zag Stepwhen P is not the root node, and X and p are different for the left child or right child, the Zig-zag operation is performed. when P is the left child and X is the right child, the X-L is then right-handed. when P is the right child and X is the left child, the X is right-handed and then left-handed.    Application splay tree can easily solve some interval problems, according to the different shape binary tree First order traversal results invariant characteristics, you can set the interval in order to build binary search tree. each bottom-up set of splay can be moved to the location of the root node, using this feature, you can easily use the lazy idea of the interval operation. for each node, the size is recorded, representing the number of nodes in the subtree, which makes it easy to find the K-small or K-elements in the interval. for a section to be processed [x, Y], first splay x-1 to root, and then splay y+1 to the right child of root, then the right child of Root's left child corresponds to the entire range of the child tree. In this way, most of the interval problems can be easily solved, the operation also applies to the addition or deletion of one or more entries, and the movement of the interval.

Self-study notes

Start Writing splay yourselftoday. The small partners around are mostly using subscripts and arrays to maintain the links of each node, but I still like the C + + pointer (? ω).

nodes exist in the form of a struct structure.

struct Node {    int  value;     *father;     *son[2];        Node (int0, node *f = NULL) {        = v;         = F;        son[0] = NULL;        son[1] = NULL;    }};

Among them, Son[0] represents the left son, Son[1] represents the right son.

Use a function to determine which son of the parent node the child node is .

bool Son (node *f, node *s) {    return f->son[1] = = s;}

The return value is the subscript of the son[] array, which is handy.

The most important thing is the rotation operation , unlike the common zig,zag rotation, I like to use a function to implement both functions, that is, rotate (x) represents the position x is rotated to its parent node.

InlinevoidRotate (node *t) {node*f = t->father; Node*g = f->father; BOOLA = Son (f, t), B =!A; F->son[a] = t->Son[b]; if(T->son[b]! =NULL) T->son[b]->father =F; T->SON[B] =F; F->father =T; T->father =G; if(g! =NULL) G->son[son (G, f)] =T; ElseRoot=t;}

The function will determine if x is on the left son of the parent node or the right son, and automatically spins or spins the left hand. It is important to pay attention to the hands of the son who changed his grandfather's knot, and the father's pointers to the nodes, so avoid sloppy leaks. At the same time also have to do a special sentence, put stop access to illegal address. Here with a pointer compared to the use of subscript one advantage is that if you accidentally access null is a null pointer, is also the subscript party commonly used 0 subscript, the pointer will be written must re, and subscript writing may not crash, it is not easy to find errors, resulting in some more complex and mentally retarded errors.

Then is the core function--splay function , it seems that some people called spaly appearance, but I did not research anything. Splay (x, y) is used to rotate the X node to one of the sons of the Y-node. In particular, splay (X,nil) represents the rotation of X to the location of the root node. The father of the root node is generally nil or 0.

 inline void  spaly (node *t, node *p) { while         (T->father!= p) {node  *f = T->father;        Node  *g = F->father;         if  (g == p) rotate (t);  else   { if  (Son (G, f) ^ son (f, t)) rotate (t), rotate (t);         else   rotate (f), rotate (t); }    }}

Two types of double-spin are notable here. If T (the node), F (Father node), G (Grandfather node) formed a one-way chain, that is, [right → right] or [Left → left] this way, then the Father node rotate operation, and then to the node rotate operation, otherwise, the node is two consecutive rotate operations. It is said that the single-spin Ben, double-spin O (logn), this sentence I did not research, the individual said do not want to do too much inquiry, after all, splay complexity of the original is quite metaphysical, and special Katan splay The question also did not hear. By the way, this double-spin operation and the double-spin of AVL is not so similar ah, although still not quite the same, well, it is not really like ╮ (╯-╰) ╭.

Next, we'll talk about insert operations . The insert operation is similar to the normal two-fork search tree, first finding the appropriate leaf node and then creating a new node on the empty son[] to put the value in. The difference is that the new node needs to be splay to the root node location, the complexity needs, do not ask why.

void Insert (int  val) {    if (root = NULL)        New  node ( Val, NULL);      for (Node *t = root; t; t = t->son[val >= t->value]) {        ifreturn;}         if (t->son[val >= t->value] = = NULL)            tnew  node (val, t);}    } 

Note that this insertion function implements a non-heavy collection.

The corresponding is the deletion operation , relatively complex. To delete an element, you need to find the node in the tree first, and then splay the node to the root position to begin the classification discussion. If the node has no left son (left subtree), direct the right son to the root of the position can be, otherwise you need to find ways to merge left and right sub-tree: In the left sub-species found the most up to the top of the node, to rotate it to the root of the son, at this time it must not have the right son, Because none of the elements in the left subtree of the root node is larger than it, then the right subtree of the root node is connected to the right son of the knot.

InlinevoidEraseintval) {Node*t =Root;  for(; t;) {        if(T->value = =val) Break; T= t->son[val > T->value]; }    if(t! =NULL)        {spaly (T, NULL); if(t->son[0] ==NULL) {Root= t->son[1]; if(Root! =NULL) Root->father =NULL; }        Else{node*p = t->son[0];  while(p->son[1] !=NULL) P= p->son[1]; Spaly (P, t); Root=p; Root->father =NULL; P->son[1] = t->son[1]; if(p->son[1] !=NULL) P->son[1]->father =p; }    }}

It's a lot more complicated than insert ().

The above is the framework of splay, is an essential part of splay, on this basis can join many new functions.

@Author: Yousiki

Splay Stretching Tree Learning notes

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.