11, "Heap" oblique heap

Source: Internet
Author: User

introduction of the oblique heap

The inclined heap (Skew heap) is also called the Adaptive Heap (self-adjusting heap), which is a variant of the left-leaning heap. As with the left-leaning heap, it is often used to implement a priority queue. The time complexity of its merge operation is also O (LG N).

Compared to the left-leaning heap, the nodes of the oblique heap do not have a "0 distance" attribute. In addition, they have different combinations of oblique heaps. The combined operation algorithm of the oblique heap is as follows:
(1) If an empty oblique heap is merged with a non-empty inclined heap, a non-empty oblique heap is returned.
(2) If two oblique heaps are non-null, then compare two root nodes and take the root node of the smaller heap as the new root node. Merge the "right child" and "larger heap" of the root node of the smaller heap.
(3) After merging, swap the new heap root node for the left child and right child.
Step (03) is the key to the difference between the combination of the oblique and left-leaning piles, if it is left-leaning heap, then the combination to compare the size of the child's 0 distance, if the right child's 0 distance > The child's 0 distance away, then the exchange of children; Finally, the 0 distance from the root is set.

analysis of inclined piles

1. Basic definition

1Template <classT>2 classskewnode{3      Public:4T key;//keyword (key value)5Skewnode *left;//left child6Skewnode *right;//Right Child7 8Skewnode (T value, Skewnode *l, Skewnode *r):9 key (value), left (L), right (R) {}Ten};

Skewnode is the node class corresponding to the skew heap.

1Template <classT>2 classSkewheap {3     Private:4Skewnode<t> *mroot;//root node5 6      Public:7 skewheap ();8~skewheap ();9 Ten         //forward sequence traversal "oblique heap" One         voidpreorder (); A         //middle order Traverse "oblique heap" -         voidinorder (); -         //Sequential traversal of "oblique heap" the         voidpostorder (); -  -          //merges the skew heap of other into this.  -         voidMerge (skewheap<t>*Other ); +         //Insert nodes (key is node key value) into the oblique heap -         voidInsert (T key); +         //Delete nodes (key is node key value) A         voidremove (); at  -         //destroying the oblique heap -         voiddestroy (); -  -         //Print a slanted heap -         voidprint (); in     Private: -  to         //forward sequence traversal "oblique heap" +         voidPreorder (skewnode<t>* heap)Const; -         //middle order Traverse "oblique heap" the         voidInorder (skewnode<t>* heap)Const; *         //Sequential traversal of "oblique heap" $         voidPostorder (skewnode<t>* heap)Const;Panax Notoginseng  -         //swap node x and node Y the         voidSwapNode (skewnode<t> *&x, skewnode<t> *&y); +         //merging "oblique heap X" and "Oblique heap y" Askewnode<t>* merge (skewnode<t>* &x, skewnode<t>* &y); the  +         //destroying the oblique heap -         voidDestroy (Skewnode<t>* &heap); $  $         //Print a slanted heap -         voidPrint (skewnode<t>* heap, T key,intdirection); -};

Skewheap is a skew heap class that contains the root node of the oblique heap and the operation of the skew heap.

2. Merging

1 /*2 * Merge "oblique heap X" and "Oblique heap y"3  */4Template <classT>5skewnode<t>* Skewheap<t>::merge (skewnode<t>* &x, skewnode<t>* &y)6 {7     if(x = =NULL)8         returny;9     if(Y = =NULL)Ten         returnx; One  A     //when merging x and Y, X is used as the root of the merged tree; -     //The operation here is guaranteed: Key for x < y -     if(X->key > y->key) the SwapNode (x, y); -  -     //Merge the right child of X with Y, -     //The merging of X's left and right children, without the need to consider their NPL like a leftist heap.  +skewnode<t> *tmp = Merge (x->Right , y); -X->right = x->Left ; +X->left =tmp; A  at     returnx; - } -  - /* - * Merge the skew heap of other into this.  -  */ inTemplate <classT> - voidSkewheap<t>::merge (skewheap<t>*Other ) to { +Mroot = Merge (Mroot, other->mroot); -}

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

1 /* 2 * Create a new node with key value and insert it into the oblique heap .3  *4 * Parameter Description:5 * Root node of heap slope6 * Key value of the node inserted by key7 * return value:8 * Root node9  */TenTemplate <classT> One voidSkewheap<t>:: Insert (T key) A { -Skewnode<t> *node;//new Node -  the     //new Node -node =NewSkewnode<t>(key, NULL, NULL); -     if(node==NULL) -     { +cout <<"error:create node failed!"<<Endl; -         return ; +     } A  atMroot =merge (mroot, node); -}

The action of Insert (key) is to create a new node with key value and add it to the current skew heap.

4. Delete

1 /* 2 * Delete nodes.3  */4Template <classT>5 voidSkewheap<t>:: Remove ()6 {7     if(Mroot = =NULL)8         returnNULL;9 Tenskewnode<t> *l = mroot->Left ; Oneskewnode<t> *r = mroot->Right ; A  -     //Delete root node - Delete mroot; the     //the new tree after the merging of the left and right sub-trees -Mroot =merge (L, R); -}

Remove () removes the smallest node from the oblique heap.

11, "Heap" oblique heap

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.