Data structure of the extension tree detailed _c language

Source: Internet
Author: User

1. Overview

Binary lookup trees (Binary search tree, also known as the binary sort tree, that is, Binary kind trees) can support a variety of dynamic set operations, which can be used to express ordered sets, indexing, etc., so in practical applications, binary sorting tree is a very important data structure.

Considering the complexity of the algorithm, we know that the time complexity of the basic operations on the binary lookup tree (such as finding, inserting, etc.) is proportional to the height of the tree. For a complete binary tree with n nodes, the worst-case run time for these operations is O (log n). However, if the tree is degraded to a linear chain of n nodes (in this case, a single linked list) because of frequent deletions and insertions, then the worst-case run time for these operations is O (n). In order to overcome the above shortcomings, many binary lookup tree deformation appeared, such as red and black trees, AVL trees, treap trees and so on.

This article describes an improved data structure for a two-fork lookup tree-stretching tree (splay). Its main feature is that the tree is not guaranteed to be balanced, but the time complexity of the various operations is O (log n), so the binary lookup tree is also a kind of balanced binary tree from the complexity of the split. In addition, compared to other tree-like data structures (such as red-black tree, AVL tree, etc.), the spatial requirements and programming complexity of the extension tree are much smaller.

2. Basic operation

The starting point of the stretching tree is this: considering the principle of locality (the content that has just been accessed may still be accessed next time, the contents of the lookup number may be accessed next time), in order to make the entire search time smaller, those nodes with high frequency of investigation should often be located near the root. In this way, it's easy to think of the following scenario: After each lookup node, the tree is refactored, the node being searched is moved to the root, and the two-fork lookup tree is the extension tree. Each time the extension tree is manipulated, it rotates the accessed node to the root position by rotating it.

In order to rotate the currently accessed node to the root, we typically rotate the node from the bottom up until the node becomes the root of the tree. The ingenious thing about "rotation" is that without upsetting the data size relationship in the sequence (meaning that the sequence traversal result is full order), all basic operations are still O (log n).

The stretching tree has three kinds of rotation operation, namely single rotation, zigzag rotation and zigzag rotation. For the sake of explanation, we assume that the Father node of the currently accessed node is x,x y (if the Father node of x is present), and X's grandparent node is Z (if X's grandparent node exists).

(1) Single rotation

Node X's parent node y is the root node. At this point, if X is the left child of Y, we do a right spin operation, and if X is the right child of Y, we do a left-spin operation. After rotation, x becomes the root node of the binary lookup tree T, and the adjustment ends.

(2) A font rotation

Node X's parent node y is not the root node, Y's parent node is z, and X and Y are both the left children of their parent node or the right child of their parent node. At this point, we do a left or right rotation operation.

(3) Zigzag rotation

Node X's parent node y is not the root node, and Y's parent node is z,x and y in one is the left child of its parent node and the other is the right child of its parent node. At this time, we do a rotation or left-right rotation operation.

3. Stretching tree interval operation

In practical applications, the sequence traversal of the extension tree is the sequence we maintain, which leads to the question of how to represent an interval in the extension tree. For example, we want to extract the interval [a,b], then we will be a previous number of a node to the tree root, the B after a node corresponding to the node to the right side of the root, then the left subtree on the right side of the tree corresponds to the interval [a,b]. The reason is very simple, the corresponding node of a number in front of a is transferred to the root, the number after a and a is on the right subtree of the root, and then the node corresponding to the B is transferred to the right of the root, then the [a,b] interval is the subtree shown in B in the following figure.

Using interval operation we can realize some functions of the segment tree, such as answering the query of interval (maximum, minimum value, etc.). This can be achieved, in each node records about the node as the root of the subtree information, and then asked to extract the interval, and then directly read the relevant information subtree. You can also make an overall change to the interval, this also uses a delay marking technique similar to a line tree, that is, for each node, an extra record of one or more tokens indicates whether a subtree with the root of the node has been manipulated, and that this operation affects the information value of its child nodes, The tag is passed down appropriately when you rotate and some other actions.
The extension tree is more powerful than the line-segment tree, which solves the problem that the following two tree segments cannot solve:

(1) Insert some numbers after a. The method is: first, you construct a stretch tree with the number you want to insert, then you go to the root, and the node that corresponds to a number after a is transferred to the right side of the root node, and finally the new hung to the Zoozi node of the root right child node.

(2) Delete the number within the interval [a,b]. First, the [a,b] interval is extracted and can be deleted directly.

4. Realize

The code is all from "Reference 2".

(1) Rotation operation

node is the node type where ch[0] represents the left node pointer, ch[1] represents the right node pointer//
 
pre representing the pointer to father/
 
/Rotate function for (left/right) rotation x->pre
 
void Rotate ( Node *x, int D)//rotation operation, d=0 represents left rotation, d=1 represents right-turn
 
{
 
 node *y = x->pre;
 
 Push_down (y), Push_down (x);
 
 Pass the Y-node's tag down (because Y is above), and then pass the X's tag down
 
 y->ch[! d] = x->ch[d];
 
 if (X->ch[d]!= Null) x->ch[d]->pre = y;
 
 X->pre = y->pre;
 
 if (y->pre!= Null)
 
 if (y->pre->ch[0] = = y) y->pre->ch[0] = x; else y->pre->ch[1] = x;
 
 X->ch[r] = y, Y->pre = x, Update (y); Maintain the Y node
 
 if (y = = root) root = x;//ROOT to represent the entire tree's roots node
 
}

(2) Splay operation

void splay (node *x, node *f)//splay operation, which indicates that the node x is transferred below the node F
 
{for
 
 (Push_down (x); X->pre!= F;)////At the outset, the X's mark is passed C3/>if (X->pre->pre = = f)//Parent node's father is F, execute single rotation
 
  if (x->pre->ch[0] = = x) Rotate (x, 1); Else Rotate (x, 0);
 
 else
 
 {
 
  node *y = x->pre, *z = y->pre;
 
  if (z->ch[0] = y)
 
   if (y->ch[0] = x)
 
    Rotate (y, 1), Rotate (x, 1);//zigzag rotation
 
   Else
 
    Rotate (x, 0), Rot Ate (x, 1); Zigzag rotation
 
  Else
 
   if (y->ch[1] = = x)
 
    Rotate (y, 0), Rotate (x, 0);//zigzag rotation
 
   Else
 
    Rotate (x, 1), Rota Te (x, 0); Zigzag rotation
 
 }
 
 Update (x);//finally Maintain x node
 
}


(3) To transfer the number of K to the required position

Finds the lower-order traversal of the K node and rotates it to node F below
 
void Select (int k, node *f)
 
{
 
 int tmp;
 
 Node *t;
 
 for (t = root;;)//starting from the root node
 
 {
 
  Push_down (t);///////////////////////////////////=
 
  t->ch[0]->size;///Get t Zuozi size />if (k = = tmp + 1) break; T is to find the node, exit the Loop
 
  if (k <= tmp)//k node on t left, go left
 
   t = t->ch[0];
 
  else//Otherwise on the right, and in the right subtree, this node is no longer k
 
   -= tmp + 1, t = t->ch[1];
 
 }
 
 Splay (T, f); Perform rotation
 
}

5. Application

(1) Sequence maintenance problems

Title: Maintain a sequence that supports the following kinds of operations:

1. Insert: Insert tot number after the posi number in the current series; If you insert in the first sequence, then the posi is 0.

2. Delete: Tot number is deleted continuously from the posi number of the current series.

3. Modification: Starting from the current series Posi number of consecutive tot number of unified revision to C.

4. Flip: Take out the number from the current number of the first posi numbers, tot, flip into the original position.

5. Sum: Calculates and outputs a continuous tot number from the posi number in the current sequence.
6. Sum maximal subsequence: To find out the current sequence and the largest segment of the sequence, and output maximum and.

(2) Lightweight Web server LIGHTTPD uses data structure splay tree.

6, Resources
(1) Yang Siyu "The basic operation and application of the extension tree"
(2) Crash "using extension tree to solve sequence maintenance problem"

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.