Extension tree and its implementation

Source: Internet
Author: User

We have discussed that the search efficiency of a 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.

The starting point of the stretching tree is this: Taking into account the principle of locality (the content that has just been accessed may still be accessed the next time, the number of lookups may be accessed the next time), in order to make the whole search time smaller, those nodes with high frequency are often located near the root. In this way, it is easy to think of the following scenario: Every time the node is searched, the tree is reconstructed, the node being searched is moved to the root, and the self-tuning two-fork search tree is the stretching tree. Each time the stretching tree is manipulated, it rotates the accessed node to the root location by means of a rotation. In order to rotate the currently accessed node to the root, we usually rotate the node from the bottom up until the node becomes the root of the tree. The neat thing about "rotation" is that all the basic operations are still O (log n), without disrupting the data size relationship in the sequence (meaning that the middle order traversal results are all ordered).

There are three main kinds of rotation operations in the stretching tree, namely single rotation, one-zigzag rotation and zigzag rotation. For the sake of explanation, we assume that the currently accessed node is the parent node of x,x for Y (if the Father node of x exists), the grandfather node of X is Z (if the grandfather node of x exists). in the case of a target node, the stretching tree will continue to perform one of the following three operations until the target node becomes the root node (note that the Grandparent node refers to the 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.


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.


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.

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)

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 heavily accessed repeatedly. Stretching the tree allows this repetitive search to be done with high efficiency.

Extension tree and its implementation

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.