Introduction to the stretching tree
1. Source:http://www.cnblogs.com/skywang12345/p/3604238.html
The splay tree is a binary sort tree that can perform insert, find, and delete operations within O (log n). It was created by Daniel Sleator and Robert Tarjan.
(01) The stretching tree belongs to the binary search tree, that is, it has the same properties as the two-fork search tree: Assuming that X is any node in the tree, the x node contains the keyword key, and the key value of Node x is recorded as Key[x]. If Y is a node in the left subtree of x, then key[y] <= key[x], and if Y is a node of the right subtree of x, then key[y] >= key[x].
(02) In addition to the nature of the two-fork search tree, the extension tree also features a feature: When a node is accessed, the extension tree rotates to make the Node a tree root. The benefit of this is that the node will be accessed quickly the next time it is accessed.
Suppose you want to perform a series of lookup operations on a binary lookup tree. In order to make the whole search time smaller, those items with high frequency are often placed near the root of the tree. The idea is to design a simple method that reconstructs the tree after each search, moving the found items closer to the root. Stretching tree came into being, it is a self-adjusting form of the two-fork search tree, it will move from one node to the root of the path, through a series of rotations of the node moved to the root.
Compared to the binary lookup tree and the AVL tree, it is important to focus on the "rotation algorithm of the stretching tree" when learning to stretch the tree.
2. Source:http://www.cnblogs.com/vamei/archive/2013/03/24/2976545.html
The search efficiency of the tree is related to the depth of the tree. The depth of the binary search tree may be n, in which case the complexity of each search is n magnitude. The AVL tree is a log (n) of the complexity of a single search through the depth of the dynamic balance tree. Below we look at the splay tree, which is very efficient for M-time continuous search operations.
The stretching tree will perform some special operations on the tree after a search. The idea of these operations is somewhat similar to the AVL tree, which is to change the distribution of tree nodes and reduce the depth of the tree by rotating. But the stretching tree does not require the balance of AVL, the left and right subtree of any node can be any depth. Similar to a two-fork search tree, a single search for a stretch tree may also require n operations. But the stretching tree can guarantee that the complexity of M-time continuous search operations is Mlog (n) magnitude, not mn magnitude.
Specifically, after querying to the target node, the stretching tree continues one of the following three operations until the target node becomes the root node (note that the Grandparent node refers to the parent node's parent node)
1. Zig: When the target node is the left child or right child node of the root node, a single rotation is made, and the target node is adjusted to the location of the root node.
Zig
2. Zig-zag: When the target node, parent node, and grandfather node become "Zig-zag" configurations, make a double rotation and adjust the target node to the location of the grandparent node.
Zig-zag
3. Zig-zig: When the target node, parent node, and grandfather node become "Zig-zig" configurations, perform a Zig-zig operation to adjust the target node to the location of the grandparent node.
Zig-zig
Single rotation operation and double rotation operation are shown in the AVL tree . Here are the Zig-zig operations:
Zig-zig operation
In the stretch tree, the Zig-zig operation (basically) replaces the single rotation in the AVL tree. In general, if the tree above is unbalanced, then A and b subtrees are probably much deeper. Relative to the single rotation (think of the effect of single rotation), Zig-zig can put a, B subtree in a relatively high position, thereby reducing the total depth of the tree.
Here we demonstrate with a concrete example. We will search for node 2 from the tree:
Original
Zig-zag (double rotation)
Zig-zig
Zig (single rotation at root)
The first query above requires n operations. However, after a query, the 2 node becomes the root node, and the depth of the tree decreases greatly. Overall, most of the tree's node depths are reduced. Subsequent queries on the individual nodes will be more efficient.
Another benefit of stretching the tree is to place the most recently searched nodes in the most easily searchable root node. In many application environments, such as Web applications, some fixed content is repeatedly accessed (such as the MV of Jiangnan style). Stretching the tree allows this repetitive search to be done with high efficiency.
3. Source:http://www.cnblogs.com/kernel_hcy/archive/2010/03/17/1688360.html
First, Introduction:
Stretching trees, or adaptive search trees, is a simple and efficient data structure that holds an ordered set. The stretch tree is essentially a binary lookup tree. Allow to find, insert, delete, delete minimum, delete maximum, split, merge, and many other operations, the time complexity of these operations is O (LOGN). Because stretching trees can adapt to demand sequences, their performance is better in practical applications.
The stretch tree supports all two fork tree operations. The stretching tree does not guarantee that the worst-case time complexity is O (logn). The time complexity boundary of the stretching tree is equally. Although a single operation can be time-consuming, the temporal complexity can be guaranteed to be O (Logn) for an arbitrary sequence of operations.
Ii. self-adjustment and averaging analysis:
Some limitations of balancing the Find tree:
1. Balance the search tree each node needs to save additional information.
2, difficult to implement, so the insertion and deletion operation is high complexity, and is a potential error point.
3, for the simple input, performance and nothing improved.
Balancing the find tree can consider where to raise performance:
1. The time complexity of the balanced lookup tree in the worst, average, and worst case is essentially the same.
2, access to a node, if the second access time is less than the first visit, will be very good thing.
3, 90-10 Law. In real-world situations, 90% of access occurs on 10% of the data.
4, deal with that 90% of the situation is very good.
Three, the time boundary of averaging:
The time complexity of accessing a node in a binary tree is the depth of the node. Therefore, we can reconstruct the structure of the tree so that the frequently accessed nodes move in the direction of the tree root. Although this introduces additional operations, the frequently accessed nodes are moved closer to the root, so we can access them very quickly for this part of the node. This can improve performance, based on the 90-10 rule above.
To achieve the above goal, we need to use a strategy-rotation to the root (rotate-to-root). The specific implementation is as follows:
The rotation is divided into left and right, and the two are symmetrical. Icon:
For the convenience of narration, the right hand is called X around Y right, and the left is called Y around X left.
Shows the rotation of node 3 to the root:
Figure 1
First, the node 3 goes around 2 left, and then 3 is rotated around node 4.
Note: The data you are looking for must conform to the above 90-10 rules, otherwise the performance will not rise in reverse!!
Iv. Basic bottom-up stretching tree:
The time complexity of logarithmic averaging boundary can be obtained by applying stretching (splaying) technique.
There are three situations that can be divided when rotating:
1, zig situation.
X is a non-root node on the lookup path that we need to rotate.
If the parent of x is the root, then we rotate the x to the root using the method shown:
Figure 2
This is the same as a normal single rotation.
2, Zig-zag situation.
In this case, X has a parent node p and a grandparent node g (parent node of P). X is the right child node, p is the left Dial hand node, or vice versa. This is the double rotation.
First x rotates around p, and then x rotates right around G.
:
Might
3, Zig-zig situation.
This is different from the previous rotation. In this case, both X and P are left dial hand nodes or right child nodes.
First the p rotates around G and the x rotates around the p right.
:
Figure Four
Here is the pseudo-code for splay:
P (x): Gets the parent node of x, G (x): Gets the grandfather node of X (=p (P (x))).
Function Buttom-up-splay:
Do
If x is the left Dial hand node of P (x) Then
If G (X) is empty then
X Rotate Right by P (x)
Else If P (x) is the left Dial hand node of g (X)
P (x) right rotation around g (x)
x rotate Right by P (x)
Else
x rotate Right by P (x)
x p (x) left (p (x) and the above sentence are different, the original g (x))
Endif
Else If X is the right child node of P (x) Then
If G (X) is empty then
X around P (x) left-handed
Else If P (x) is the right child node of G (X)
P (x) around g (x) left-handed
X around P (x) left-handed
Else
X around P (x) left-handed
X (x) Right-hand rotation (p (x) and the above sentence, which is the original g (x))
Endif
Endif
while (P (X)! = NULL)
Endfunction
Careful analysis of Zig-zag, can be found, in fact, Zig-zag is two times zig. So the code above can be simplified:
Function Buttom-up-splay:
Do
If x is the left Dial hand node of P (x) Then
If P (x) is the left Dial hand node of g (X)
P (x) right rotation around g (x)
Endif
x rotate Right by P (x)
Else If X is the right child node of P (x) Then
If P (x) is the right child node of G (X)
P (x) around g (x) left-handed
Endif
X around P (x) left-handed
Endif
while (P (X)! = NULL)
Endfunction
Here is an example of rotating node C to the root.
Figure Five
V. Basic stretching tree operation:
1. Insert:
When a node is inserted, the stretch operation executes. Therefore, the newly inserted node is on the root.
2. Find:
If the lookup succeeds (found), the discovered node becomes the new root of the tree due to the stretching operation.
If the lookup fails (not), then the node before the lookup encounters null becomes the new root. That is, if the found node is in the tree, then the node at the root is the node closest to the node.
3. Find the maximum minimum:
Perform the stretching after the lookup.
4, delete the maximum minimum:
A) Delete minimum:
The least-found operation is performed first.
At this point, the node to be deleted is on the root. Depending on the characteristics of the binary lookup tree, the root has no left child nodes.
Use the right child node of the root as the new root and delete the old root that contains the minimum value.
b) Delete Max:
First, the most-searched operation is performed.
Delete the root and put the left dial hand node of the root being deleted as the new root.
5. Delete:
Move the node you want to delete to the root.
Delete the root, leaving two subtree L (left subtree) and R (right subtree).
Use Deletemax to find the largest node of L, at which point the root of L has no right subtree.
Make R a right subtree of the root of L.
As shown:
Figure Six
Six, top-down stretching tree:
In the bottom-up stretch tree, we need to find the parent and grandparent nodes of a node, so this stretch tree is difficult to implement. Therefore, we can build a top-down stretch tree.
As we search for a node x down the tree, we move the node and its subtree on the search path. We build two temporary trees-Sho right tree. A tree of nodes that are not moved is called the medium tree. During the stretching operation:
1, the current node x is the root of the tree.
2, Zo Shu L save nodes less than X.
3, right tree R save nodes greater than X.
At first, X is the root of the tree T, and the Left and right trees L and R are empty. And from the bottom up the same, top-down also divided into three kinds of situations:
1, Zig:
Figure Seven
For example, when you search for x, the node you are looking for is smaller than x, and y is rotated to the root of the tree. After rotation, x and its right subtree are moved to the right tree. Obviously, the nodes on the right tree are larger than the nodes you are looking for. Note that X is placed in the smallest position on the right tree, which means that x and its subtree are smaller than all nodes in the original right tree. This is because the more nodes that are moved to the right tree in front of the path, the larger the value. The reader can analyze the structure of the tree for a simple reason.
2, Zig-zig:
Figure Eight
In this case, the node you are looking for is in the subtree of Z, that is, the node you are looking for is smaller than x and Y. So move the X, Y and its right subtree to the right tree. The first is Y to the right of X, then z to the right of Y, then the right subtree of Z (right child node of Z) is moved to the right tree. Note the location of the mount point in the right tree.
3, Zig-zag:
Figure Nine
In this case, first turn y right to the root. This is the same as the zig situation. Then change to the shape shown on the right. Next, the z is left-handed, and Y and its left subtree are moved to the left tree. In this way, the situation is divided into two zig cases. This simplifies the programming, but increases the number of operations (equivalent to two zig cases).
Finally, after finding the node, merge the three trees.
Figure 10
The left and right sub-trees of the tree are connected to the right and left subtrees of the tree, respectively. Leave the left and right trees as the left and right subtree of x. The tree that is the root of the found node is the most re-formed.
The following is a pseudo-code:
Right connection: Connects the current root and its right subtree to the right tree. Left dial hand node as the new root.
Left join: Connects the current root and its Shoren to the left tree. The right child node as the new root.
T: The current root node.
Function Top-down-splay
Do
If X is less than T then
If X equals T left dial hand node Then
Right connection
ElseIf X is less than T left dial hand node Then
The left child node of T is right-handed around T
Right connection
Else x Left dial hand node that is greater than T
Right connection
Left connection
EndIf
ElseIf x greater than T then
IF X is equal to the right child node of T then
Left connection
ElseIf X is greater than the right child node of T then
The right child node of T is left-handed around T
Left connection
Else x is less than the right child node of T ' then
Left connection
Right connection
EndIf
EndIf
While! (Find X or encounter empty node)
Combine left and right tree
Endfunction
In the same way, the above three scenarios can also be simplified:
Function Top-down-splay
Do
If X is less than T then
If X is less than T left child then
The left child node of T is right-handed around T
EndIf
Right connection
Else If X is greater than T then
If X is greater than T right child then
The right child node of T is left-handed around T
EndIf
Left connection
EndIf
While! (Find X or encounter empty node)
Combine left and right tree
Endfuntion
Here is an example of finding node 19:
In the example, there is no node 19 in the tree, and finally, node 18 closest to the node is rotated to the root as the new root. Node 20 is also the closest node to node 19, but node 20 does not become the new root, which is related to the position of node 20 in the original tree.
This example is finding node C:
PHP algorithm "tree structure" stretching tree (1)-Basic concept