Java data structure and algorithm parsing (eight)--stretching tree __ Storage

Source: Internet
Author: User
Tags comparable
Java data structure and algorithm parsing (eight)--stretching tree Introduction to Stretching tree

The stretching tree (splay) is a special two-fork lookup tree.
Its special meaning is that, in addition to itself is a binary lookup tree, it also has a feature: When a node is accessed, the extension tree will be rotated to make the node root. The advantage of doing this is that the next time you want to access the node, you will be able to access the node quickly. characteristic

1. Compared with ordinary two-fork lookup trees, there is no case, any operation of the spread O (log2n) of the complexity, time performance is better
2. Compared with general balanced binary trees such as red black tree, AVL tree, maintain less node extra information, better spatial performance and lower programming complexity
3. In many cases, the subsequent query has a significant correlation with the previous query for the lookup operation. So each query operation will be found to rotate the node to the root node of the tree, so that the next query operation can be completed quickly
4. Can be completed on the range of query, modify, delete operations, you can achieve the line-tree and tree-like array of all functional rotation

Extension Tree Implementation O (log2n) magnitude of the split-level complexity relies on each time the extension tree is queried, modified, deleted, and then rotated to splay (x, root), which rotates the node x to the root of the tree.
There are six types of rotation of the extension tree, and three if you remove the duplicate of the Mirror: Zig (Zag), Zig-zig (Zag-zag), Zig-zag (Zag-zig).

1 spin in a bottom-up manner

1.1 Zig rotation

As shown in the figure, the parent node of the X node is the left child node of Y,x y, and the Y node is the root. You can make X the root node of the extension tree by just having a right rotation (zig action) on the X node, making it the parent of Y.

1.2 Zig-zig rotation

As shown in the figure above, the parent node of the X node y,y the parent node Z, and the three are on a chain of glyphs. At this point, the Y-node and Z-nodes are zig rotated, then the x and Y nodes are zig rotated, and then the right image is shown, and X becomes the ancestor node of Y and Z.

1.3 Zig-zag Rotation

As shown in the figure above, the parent node of the X node y,y the parent node Z, and the three are on the chain of the font. At this point, the X and Y nodes are zig rotated, then the x and Y nodes are zag rotated, and then the right image is shown, and X becomes the ancestor node of Y and Z.

2 rotate from top to bottom
This method does not require a node to store a reference to its parent node. When we search a node x down the tree, move the node and its subtree on the search path. Build two temporary trees--Sho right tree. A tree without a removed node is called a middle tree.

(1) The current node x is the root of the middle tree
(2) Left tree L hold 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 tree L and the right tree R are all empty. Three kinds of rotation operation:
2.1 Zig Rotation

As shown in the figure, the X node's child node Y is the node we are looking for, then only need to do a right rotation of the Y node (Zig operation), make it the parent of X, you can make y as the root node of the extension tree. Y is the root of the tree in which the X node moves to the right tree R, and obviously the nodes on the right tree are larger than the node you are looking for.

2.2 Zig-zig Rotation

As shown in the figure above, the left child node of the x node Y,y Z, the three are on a font chain, and the node to look for is in a subtree with the root of the z node. At this point, the X and Y nodes are zig, then the z and y are zig, so that z becomes the root of the middle tree, and Y and its hung are loaded onto the right tree R.

2.3 Zig-zag Rotation

As shown in the figure above, the left child node of the X node y,y the right child node Z, the three are on the font chain, and the element that needs to be found is located on a subtree with Z root. At this point, the X and Y nodes are zig rotated, and the X and its right hung are loaded onto the right tree R, at which point y becomes the root node of the tree, and then the z and Y nodes are zag rotated so that z becomes the root node of the tree.

2.4 Merging

Finally, when a node is found or an empty node is encountered, the left, middle, and right trees need to be merged. As shown in the figure, the left hung is loaded to the bottom left of the middle tree (which satisfies the traversal order requirement), and the right hung is loaded to the far right of the tree in the bottom (which satisfies the traversal order requirement). the realization of stretching tree 1. Node

public class Splaytree<t extends comparable<t>> {
    private splaytreenode<t> mroot;    Root node public

    class Splaytreenode<t extends comparable<t>> {
        T key;                Keyword (key value)
        splaytreenode<t> left;    Left child
        splaytreenode<t> right;    Right child public

        Splaytreenode () {
            this.left = null;
            This.right = null;
        }

        Public Splaytreenode (T key, splaytreenode<t> left, splaytreenode<t> right) {
            this.key = key;
            This.left = left;
            This.right = Right;}}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19-20

Splaytree is the extension tree, and the splaytreenode is the extension tree node. Here, I define splaytreenode as the inner class of the splaytree. The extension tree splaytree contains the root node mroot of the extension tree. Splaytreenode consists of several constituent elements:
(1) key– is the keyword that is used to sort the nodes of the extension tree.
(2) left– is the left child.
(3) right– is the right child. 2. Rotate

 * * * The node for the rotation key is the root node and the root node is returned.
 * Note: * (a): There is a "key-valued node" in the extension tree.
 * Rotate the node with key value to the root node.
 * (b): There is no "key-valued node" in the extension tree, and key < Tree.key.
 * B-1 the precursor node of "key-valued node" to the root node when the predecessor node of "Key-valued node" is present.
 * B-2 the predecessor node of the "key-valued node" means that the key is smaller than any key value in the tree, then the smallest node is rotated to the root node.
 * (c): There is no "key-valued node" in the extension tree, and key > Tree.key.
 * C-1 the successor node of the "key-valued node" to the root node if the successor node of the "key-valued node" exists.
 * C-2 the successor node of the "key-valued node" does not exist, it means that the key is larger than any key value in the tree, then the maximum node is rotated to the root node. * Private splaytreenode<t> splay (splaytreenode<t> tree, T-key) {if (tree = = null) RE

        Turn tree;
        splaytreenode<t> N = new splaytreenode<t> ();
        splaytreenode<t> L = N;
        splaytreenode<t> r = N;

        Splaytreenode<t> C; for (;;)
            {int cmp = Key.compareto (Tree.key);                           if (CMP < 0) {if (Key.compareto (Tree.left.key) < 0) {c = tree.left; * * Rotate Right * *
                    Tree.left = C.right;
                    C.right = tree;
                    tree = C;
                if (Tree.left = null) break;                               } r.left = tree;
                /* Link Right */r = tree;
            tree = Tree.left;

                else if (cmp > 0) {if (tree.right = null) break;                          if (Key.compareto (Tree.right.key) > 0) {c = tree.right;
                    /* Rotate Left */tree.right = C.left;
                    C.left = tree;
                    tree = C;
                if (tree.right = null) break;                              } l.right = tree;
                /* Link Left */L = tree;
            tree = tree.right;
            } else {break; }} L.Right = Tree.left;
        /* Assemble * * r.left = tree.right;
        Tree.left = N.right;

        Tree.right = N.left;
    return to tree;
    public void splay (T key) {mroot = Splay (Mroot, key);
 }
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26-27--28 29---30 31--32 33 34 35 36 37 38-39 40 41 42 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 The 69 70

The function of the above code is to rotate the "key-valued node" to the root node and return the root node. Its treatment consists of:
(a): There is a "key-valued node" in the extension tree.
Rotate the "key-valued node" to the root node.
(b): There is no "key-valued node" in the extension tree, and key < Tree->key.
B-1) The predecessor node of "Key-valued node" is present, rotate the predecessor node of "node with key value" as the root node.
B-2) The predecessor node of "Key-valued node" exists, meaning that the key is smaller than any key value in the tree, then the smallest node is rotated to the root node.
(c): There is no "key-valued node" in the extension tree, and key > Tree->key.
C-1) The successor node of the node with key value is the root node, if the successor node of the node of key value is present.
C-2) The successor node of the "key-valued node" does not exist, meaning that the key is larger than any key value in the tree, then the maximum node is rotated to the root node.

Below is an example of a description of a.
Look for 10 in the extension tree below, including the "right" –> "right link" –> "combination" of these 3 steps.

01, right spin.
The "Rotate Right" section of the corresponding code

02, right Link
The "Link Right" section of the corresponding code

Related Article

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.