Splay Stretch Tree & Template __function

Source: Internet
Author: User

Stretch Tree Operation detailed http://wenku.baidu.com/view/cc211f126edb6f1aff001f16.html?from=rec&pos=4&weight=9& Lastweight=9&count=5

The magical splay http://wenku.baidu.com/view/a202e27931b765ce05081416.html

BST Extension and extension tree (splay) Day pass http://wenku.baidu.com/view/f3fbe687b9d528ea81c779b6.html?from=rec&pos=4& Weight=10&lastweight=10&count=5

I'm a split line **************************. ***************************************************************************************

First, Introduction:
Stretching a tree, or an adaptive lookup tree, is a simple and efficient data structure for storing ordered sets. The extension tree is essentially a binary lookup tree. Allows to find, insert, delete, delete minimum, delete max, split, merge and many other operations, these operations have a time complexity of O (Logn). Because stretching trees can adapt to the requirements sequence, their performance is better in practical applications.
The extension tree supports all two-fork tree operations. The extension tree does not guarantee an O (LOGN) time complexity in the worst-case scenario. The time complexity boundary of the stretching tree is equally shared. Although a single operation can be time-consuming, the time complexity can be guaranteed to be O (Logn) for an arbitrary sequence of operations.

Ii. self-adjustment and split-divide analysis:
Some limitations of the balanced lookup tree:
1, the balance lookup tree each node needs to save additional information.
2, difficult to implement, so the insertion and deletion operations are highly complex, and is a potential error point.
3, for simple input, performance has not improved.
A balanced lookup tree can consider places where performance is high:
1. The time complexity of the balance lookup tree in the worst, average, and worst cases is essentially the same.
2, access to a node, if the second visit time is less than the first visit, will be very good thing.
3, 90-10 rule. In practice, 90% of the visits occur on 10% of the data.
4, handled well that 90% of the situation is very good.
Three, divide the time boundary:
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 nodes that are frequently accessed move in the direction of the root. Although this introduces additional operations, the frequently accessed nodes are moved to the location near the root, so we can access them quickly for this part of the node. According to the 90-10 rules above, this can improve performance.
To achieve the above goal, we need to use a strategy--rotate to the root (rotate-to-root). Specifically implemented as follows:
The rotation is divided into left-handed and right-handed, and the two are symmetrical. Icon:

For the sake of narration, the right rotation of the above figure is called X around Y right, and the left is called Y around X left.
The following figure shows the rotation of node 3 to the root:

Figure 1
First node 3 around 2 left, then 3 around node 4 right.
Note: The data you are looking for must conform to the above 90-10 rule, otherwise the performance does not rise or fall.
Four, basic from the bottom up stretch tree:
Using stretching (splaying) technique, we can get the time complexity of the logarithmic splitting boundary.
In the rotation, can be divided into three kinds of situations:
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 x to the root using the method shown in the following illustration:

Figure 2
This is the same as an ordinary single rotation.
2, Zig-zag situation.
In this case, X has a parent node p and a grandparent node g (the parent node of P). X is the right child node, p is the Zoozi node, or vice versa. This is a double spin.
First the x revolves around the p, and then the x rotates around the G-right.
As shown in the figure:

Figure Three
3, Zig-zig situation.
This is different from the previous rotation. In this case, both X and P are Zoozi nodes or right child nodes.
First, the p rotates around the G right, and then the X revolves around P right.
As shown in the figure:

Figure Four
Here is the pseudocode for splay:
P (x): Gets the parent node of x, G (x): The Grandparent node for X (=p (P (x))).
Function Buttom-up-splay:
Todo
If x is the Zoozi node of P (x) Then
If G (X) is an empty Then
X around P (x) Right
Else If P (x) is the Zoozi node of g (X)
P (x) round g (X) Right
X around p (x) right
Else
X around p (x) right
X around P (x) left (p (x) differs from the preceding sentence, which is the original g (x))
Endif
Else If X is the right child node of P (x) Then
If G (X) is an empty Then
X around P (x) left rotation
Else If P (x) is the right child node of G (X)
P (x) around g (x) left
X around P (x) left rotation
Else
X around P (x) left rotation
X around p (x) right (p (x) is different from the previous one, which is the original g (x))
Endif
Endif
while (P (X)!= NULL)
Endfunction
Careful analysis of Zig-zag, you can find that in fact, Zig-zag is two times zig. So the code above can simplify:
Function Buttom-up-splay:
Todo
If x is the Zoozi node of P (x) Then
If P (x) is a Zoozi node of g (X)
P (x) round g (X) Right
Endif
X around p (x) right
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
Endif
X around P (x) left rotation
Endif
while (P (X)!= NULL)
Endfunction
Here is an example of rotating node C to the root.

Figure Five
V. Basic Stretch Tree operations:
1. Insert:
When a node is inserted, the extension operation executes. Therefore, the newly inserted node is on the root.
2, find:
If the lookup succeeds (found), the node being looked up becomes the new root of the tree due to the stretching operation.
If the lookup fails (none), then the node before the lookup encounters null becomes the new root. That is, if the node in the lookup is in the tree, then the node on the root is the nearest node from the node.
3, find the largest minimum:
Perform a stretch after the lookup.
4, delete the largest minimum:
A) Delete minimum:
The first action to find the smallest is performed.
At this point, the node to be deleted is on the root. According to the characteristics of the binary lookup tree, the root does not have a left child node.
Use the right child node of the root as the new root, removing the old root containing the minimum value.
b) Delete Maximum:
The first action to find the most is performed.
Deletes the root and takes the Zoozi node of the deleted root as the new root.
5, Delete:
Move the node you want to delete to the root.
Deletes 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 does not have a right subtree.
Make R the right subtree of the root of L.
The following illustration:

Figure Six
Six, from the top down the stretch tree:
In a bottom-up stretch tree, we need to find the parent and grandparent nodes of a node, so this extension tree is difficult to implement. Therefore, we can build a top-down stretching tree.
When we search for a node x along the tree, we move the node and its subtree on the search path. We build two temporary trees--Sho right tree. A tree without a removed node is called a middle tree. During the stretching operation:
1, the current node x is the root of the middle tree.
2, Zo Shu L save less than x node.
3, right tree R to save nodes greater than X.
At first, X is the root of the tree T, and the left and right trees and r are empty. The same as the previous bottom-up, from top to bottom also divided into three kinds of situations:
1, Zig:

Figure Seven
As shown above, when you search for x, the node you are looking for

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.