Search-figure Wenxiang Jie stretching tree SplayTree and figure Wenxiang tree splaytree

Source: Internet
Author: User

Search-figure Wenxiang Jie stretching tree SplayTree and figure Wenxiang tree splaytree


Stretch tree

The Splay Tree, also known as the split Tree, is a binary sorting Tree created by Daniel Sleator and Robert Tarjan, which improves it.


Assume that you want to perform a series of search operations on a binary search tree.To make the entire search time shorterHigh query frequencyThose entries should always be near the root of the tree.So I thought of designing a simple method,Refactor the tree after each search, and move the searched entries to a location near the root of the tree.. Splaytree came into being. Splaytree is a self-adjusting binary search tree. It moves the node to the root of the tree along the path from a node to the root of the tree through a series of rotations.

(We used to learn the optimal binary tree in the course "Data Structure". It was a static tree. We calculated the probability of each query in advance and placed the high probability query near the root of the tree, not practical .)


The stretch tree can be inserted, searched, and deleted in O (logn.Its advantage is that it does not need to record the redundant information used for the balancing tree.. The general operation on the stretch tree is based on the stretch operation.

The following describes the basic operations of the stretch tree:

After finding a node, you need to "stretch" the node and move it to the root of the tree. There are two methods for stretching:Bottom-upAndTop-down.


Bottom-up

The so-called bottom-up is like the AVL Tree"Left-hand","Right hand", After multiple operations, the node is rotated to the root of the tree, but in this way, you need to save the pointer of each node in the search path for rotation.

(Since we will talk about the more convenient and practical top-down method below, we will not detail the bottom-up rotation method here. If you are interested, You can google it)


Top-down

When we search for a node X down the tree, we remove the node and Its subtree in the search path. We construct two temporary trees-left and right.

A tree composed of unremoved nodes is called a middle tree. During the stretching operation:

1. The current node X is the root of the middle tree.

2. The left Tree L stores nodes smaller than X.

3. the right tree R stores nodes greater than X.

At the beginning, X is the root of the tree T, and left and right trees L and R are empty.

(Keyword:Side access and dismember)

(We use a 2-node instance to illustrate the Stretching Process of the SplayTree. This is the clearest illustration in history. You can see it clearly. ^_^)



Now, after reading this example, you must have a clear understanding of the SplayTree stretching operation. As for code implementation, you can search for a large amount of space on the Internet. Here, we will not paste it as a waste of space. If you are interested, please Google it.


Delete operation

The delete operation of the stretch tree is very simple. It is to first access the target node, move the node to the root of the tree, then delete the root node, and connect the left and right subtree together.


Interval operations of the stretch tree

In practical application, the ordinal traversal of the stretch tree is the series we maintain, which leads to a problem,How to express a certain range in the stretch tree?

For example, if we want to extract the range [a, B], we will convert the node corresponding to the number before a to the root of the tree, and the node corresponding to the next node of B to the right of the root of the tree, then the left subtree on the right of the root corresponds to the interval [a, B]. The reason is very simple. After the node corresponding to the number before a is transferred to the root, the number after a and a is on the right subtree of the root, then, the node corresponding to a node next to B is transferred to the right of the root of the tree,Then [a, B] is the subtree shown in B..



Using the interval operation, we can implement some functions of the Line Segment tree, such as answering questions about the interval (maximum value, minimum value, etc ).

Compared with line tree, the stretch tree is more powerful. It can solve the problems that cannot be solved by the following two line tree segments:

(1) Insert some numbers behind. The method is as follows: first, use the number to be inserted to construct an extension tree, then convert a to the root, and then convert the node corresponding to the number after a to the right of the root node, finally, the new subtree is mounted to the left child node of the right child node of the root.

(2) Delete the number in the range [a, B. Extract the range [a, B] and delete it directly.



Advantages

1. Low time complexity. O (log n) is applied to all basic operations of the stretching tree. The tree data structure is undoubtedly excellent.

2. Low space requirements. Different from the red/black tree, the color of each node needs to be recorded, and the AVL tree needs to record the balance factor. The stretching tree does not need to record any information to maintain the balance of the tree.

3. Simple algorithms and easy programming. The basic operations of the stretch tree are based on the Splay operation, and the Splay operation only needs to rotate according to the current node position.

Although the stretch tree algorithm is similar to the AVL Tree in terms of time complexity and sometimes it is slower than the AVL Tree, the programming complexity of the stretch tree is much lower than that of the AVL Tree.


Disadvantages

1. They need more local adjustments, especially during the search. (Many other data structures only need to be adjusted during the update period, but not during the search period)

2. One of a series of search operations may take a long time, which may be a deficiency in real-time applications.

3. It may become a chain. This situation may occur after n elements are accessed in a non-descending order. However, the worst case of evenly distributed data is the log-level O (logn)



Example

You need to read a number each time, and find the one with the smallest difference from the number entered above.

We can easily think of the O (n2) algorithm: Read a number each time, and then search for the number entered above once to find the minimum difference with the current number, which is recorded in the summary result T. If n is large, this algorithm is too inefficient. If you use the line segment tree to record the number of records that have been read, you need to record a 2 m large array, with a slightly higher space complexity. Although the red-black tree and the balanced binary tree are both excellent in time efficiency and space complexity, the high programming complexity is daunting. So we thought of the stretch tree algorithm.

Further analysis of this question involves three operations for an ordered set: insert, forward, and successor. For these three operations, the time complexity of the stretch tree is very good, so we design the following algorithm:

At the beginning, the tree S is empty, and the sum of T is zero. Each time a number of p is read, Insert (p, S) is executed, and p is inserted into the extension tree S. At this time, p is also adjusted to the root node of the stretching tree. In this case, the rightmost and leftmost points in the left subtree of p are obtained. These two points are the forward direction and the successor of p in an ordered set. Then obtain the minimum difference value and add the final result T.

The complexity of basic operations on the stretch tree is O (log n), so the time complexity of the entire algorithm is O (nlog n ).


[Reference]

Http://wenku.baidu.com/link? Url = Jgmmmelk6RHMDta6N0ypBiOV6XwA4uzUfQjHXHUFbFYwCxDCSXMtCr8bnQNmLBs73aVAZ4H55u8wVmyiNEmMPF6pJnNxG3K4VIXool-vwzi

Http://zh.wikipedia.org/wiki/






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.