Java data structure and algorithm parsing (17)--Skew heap __ Storage

Source: Internet
Author: User
Tags comparable
Java data structure and algorithm parsing (17)--Skew heap Skew Heap Overview

The skew heap (Skew heap) is also called the Adaptive Heap (self-adjusting heap), which is a variant of the left skew heap. Like the left-leaning heap, it is also commonly used to implement precedence queues, and as an adaptive left-hand-skew heap, the time complexity of its merge operations is also O (LG N).
The difference between it and the left skew heap is:
(1) The node of the skew heap does not have the "0 distance" attribute, while the left skew heap has.
(2) The merge operation of the skew heap and the merge operation algorithm of the left-leaning heap are different.
merge operations for the skew heap

(1) If an empty skew heap is merged with a non-empty skew heap, returns a non-empty skew heap.
(2) If two oblique heaps are non-null, compare two root nodes, and take the root node of the smaller heap as the new root node. Merge the right children of the smaller heap's root node and the larger heap.
(3) After merging, swap the left and right children of the new heap root node.
The first (3) step is the key to the difference between the merge operations of the skew and left-leaning heaps, if it is left-leaning heap, then the merged to compare the 0 distance between the child's size, if the right child's 0 distance > The left child 0 distance, then exchange around the child; Finally, at the end of the set root 0 distance.

Since merging is done along the most right path, after merging, the maximum right path length of the new skew heap will inevitably increase, which can affect the efficiency of the next merge. So after merging, by swapping the left and right subtrees, the length of the entire tree's right-hand path is very small (this is the heuristic rule). However, the skew heap does not record the distance of the nodes and, at the time of operation, from the bottom up, along the merged path, the left and right subtrees are exchanged at each node. By constantly swapping the left and right subtree, the diagonal heap dumps the right-hand path to the left.

Recursive implementation Merging

1. Comparison of two heaps; Set p to be a heap of key values with a smaller root, Q is another heap, and R is the result heap after merge.
2. The root of the order R is P (with the minimum root key value), and the right subtree of R is the left subtree of p.
3. The result is that the left subtree of R is P's right subtree and Q merge.

Before merging:

After merging:

Non-recursive merge implementation

1. Cut down the right subtree of each heap (in recursive sense). This makes the right subtree of each tree of the resulting trees empty.
2. Arrange the trees in ascending order of the key values of the root.
3. Iterative merge two trees with the largest root key value:
1 The right subtree of a tree with the secondary root key value must be empty. The left subtree and the right subtree
2) Exchange. The left subtree of the tree is now empty.
The tree with the maximum root key value is the left subtree with the secondary large root key value tree.
Performance Comparison

code implementation of the skew heap 1. Basic definition

public class Skewheap<t extends comparable<t>>  {

    private skewnode<t> mroot;    Root node

    Private class Skewnode<t extends Comparable<t>> {
        T key;                Keyword (key value)
        skewnode<t> left;    Left child
        skewnode<t> right;    The right child is public

        Skewnode (T key, skewnode<t> left, skewnode<t> right-hand) {
            this.key = key;
            This.left = left;
            This.right = right;

        Public String toString () {return
            ' key: ' +key}}}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19-20

Skewnode is the node class corresponding to the skew heap.
Skewheap is a skew heap class that contains the root node of the skew heap and the operations of the skew heap. 2. The merger

 * * Merge "skew heap X" and "Skew heap y" * *
private skewnode<t> Merge (Skewnode<t> x, skewnode<t> y) {
    if ( x = = null) return y;
    if (y = = null) return x;

    When X and y are merged, X is used as the root of the merged tree;
    //The operation here is guaranteed: X Key < y key
    if (X.key.compareto (Y.key) > 0)
        {skewnode<t> TMP = x;
        x = y;
        y = tmp;
    }

    Combine X's right child with Y,
    //after merging to Exchange X's or left-side children without having to consider their NPL like a leftist heap.
    skewnode<t> tmp = merge (X.right, y);
    X.right = X.left;
    X.left = tmp;

    return x;
}

public void Merge (skewheap<t> other) {
    this.mroot = merge (This.mroot, other.mroot);
}
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

The merge (x, y) is an internal interface that merges the two skew heaps of x and Y and returns the root node of the resulting new heap.
The merge (other) is an external interface that merges other into the current heap. 3. Add

* 
 * Create a new node (key) and insert it into the skew heap
 *
 * Parameter description:
 *     key insertion node of the value/public
void Insert (T key) {
    skewnode<t> node = new skewnode<t> (key,null,null);

    If the new node fails, it is returned.
    if (node!= null)
        this.mroot = merge (this.mroot, node);
1 2 3 4 5 6 7 8 9 10 11 12-13

The action of Insert (key) is to create a new key-valued node and add it to the current skew heap. 4. Delete

* * 
 Delete root node
 * 
 * return value:
 * Returns the key value of the     deleted node
 /public
T Remove () {
    if (this.mroot = = NULL) return
        null;

    T key = This.mRoot.key;
    skewnode<t> L = this.mRoot.left;
    skewnode<t> r = this.mRoot.right;

    This.mroot = null;          Delete root node
    this.mroot = Merge (L, R);   Merge left and right subtree return

    key;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

The action of Remove () is to delete the smallest node of the skew heap. Complete Code

public class Skewheap<t extends comparable<t>> {private skewnode<t> mroot;                Root node private class Skewnode<t extends comparable<t>> {T key;    Keyword (key value) skewnode<t> left;    Left child skewnode<t> right;
            The right child is public skewnode (T key, skewnode<t> left, skewnode<t> right-hand) {This.key = key;
            This.left = left;
        This.right = right;
        Public String toString () {return "key:" +key;
    } public Skewheap () {mroot = null;
            } * * Forward traversal "skew heap"/private void Preorder (Skewnode<t> heap) {if (heap!= null) {
            System.out.print (heap.key+ "");
            Preorder (Heap.left);
        Preorder (heap.right);
    } public void Preorder () {preorder (mroot); } * * in sequence traversal "skew heap"/private void Inorder (Skewnode<t> heap) {if (heap!= null) {inorder (heap.left);
            System.out.print (heap.key+ "");
        Inorder (Heap.right);
    } public void Inorder () {inorder (mroot);
            } * * Subsequent traversal "skew heap"/private void Postorder (Skewnode<t> heap) {if (heap!= null) {
            Postorder (Heap.left);
            Postorder (Heap.right);
        System.out.print (heap.key+ "");
    } public void Postorder () {postorder (mroot); * * * Merge "skew heap X" and "Skew heap y"/private skewnode<t> Merge (skewnode<t&

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.