Introduction to algorithms-Chapter 2-item heap

Source: Internet
Author: User
1. Concept 1. merged heap

(1) operations that can be merged and heap should be supported

Make-heap () insert (H, x) Minimum (h) EXTRACT-MIN (h) Union (H1, H2) (2) two items heap is a kind of merged heap 2. binary Tree (1) Definition of Binary Tree BK is a recursive ordered tree. B0 contains only one node BK (k> 0) composed of two tree B | K-1, one of which serves as the properties of BK of the other tree. A total of 2 ^ K Node B. the height of the tree is KC. at depth I, there are exactly C (I, K) nodes D. the degree of the tree is K, which is greater than the degree of any other node; and, if the child of the root is numbered from left to right as a K-1, a K-1 ,......, 0. Child I is the root of the subtree Bi (3) the structure of the binary tree. The left child uses the sibling method to represent the binary tree (4) the example of the binary tree 3. definitions and properties of two heap Items (1) Two heap Items (2) structure of two heap Items (3) operations provided by two heap Items (2) code binomial_heap.h
# Include <iostream> using namespace STD; // two-item heap node Structure struct node {int key; // keyword int data; // satellite data node * P; // pointer to the parent node, parent or left brother node * child; // pointer to left child node * sibling; // pointer to right brother int degree; // degree // initialize node (int n, node * nil): Key (N), P (NiL), Child (NiL), sibling (NiL), degree (0) {}}; // two-way Heap Structure class binomial_heap {public: node * head; node * nil; // constructor binomial_heap () {nil = new node (-1, nil);} binomial_heap (node * nil) {Ni L = nil;} // 19.2 void make_binomial_heap (); node * handle (); void binomial_link (node * y, node * z); node * binomial_heap_merge (binomial_heap * H1, binomial_heap * H2); void binomial_heap_union (binomial_heap * H2); void binomial_heap_insert (node * X); node * Merge (); void Merge (node * X, int K ); void binomial_heap_delete (node * X) ;}; // construct an empty two-way heap void bi Nomial_heap: make_binomial_heap () {// initialize object head = nil;} // find the smallest keyword node * binomial_heap: binomial_heap_minimum () {// The minimum keyword must be in node * x = head, * Y = nil; int min = 0x7fffffff on the root node of a Two-item tree; // traverse the root node of each two-tree while (X! = Nil) {// find the minimum value if (X-> key <min) {min = x-> key; y = x ;}x = x-> sibling ;} return y;} // connect the tree with node y as the root to the tree with node Z as the root so that Z becomes the parent node void binomial_heap: binomial_link (node * y, node * z) {// just modify the pointer y-> P = z; y-> sibling = z-> child; Z-> child = y; // increase degree Z-> degree ++ ;} // combine the root tables of H1 and H2 into a monotonically ascending order of degrees. // combine the monotonically linked list of non-leading nodes and return the merged header, node * binomial_heap: binomial_heap_merge (binomial_heap * H1, binomial_heap * H2) {node * L1 = h1-> head, * L2 = H2-> head, * ret = nil, * c = ret, * temp; while (L1! = Nil & L2! = Nil) {If (L1-> degree <= L2-> degree) temp = L1; elsetemp = L2; If (ret = nil) {ret = temp; C = ret;} else {C-> sibling = temp; C = temp;} If (L1 = temp) L1 = L1-> sibling; else L2 = L2-> sibling;} If (L1! = Nil) {If (ret = nil) ret = L1; elsec-> sibling = L1;} else {If (ret = nil) ret = L2; elsec-> sibling = L2;} Delete H2; return ret;} // merge the two hews into void binomial_heap: binomial_heap_union (binomial_heap * H2) {// H is the merging node used to output binomial_heap * H = new binomial_heap (NiL); H-> make_binomial_heap (); binomial_heap * H1 = this; // combine the root tables of H1 and H2 into a list in a monotonically ascending order of degrees, and place them in H-> head = binomial_heap_merge (H1, H2 ); // free the objects H1 and H2 Bu T not the lists they point to // If H is empty, return if (H-> head = nil) return directly; // connect the root of the equal degree, until each degree has at most one root. // X points to the root currently checked, and Prev-X points to the first root of X, next-X points to the next root node x = H-> head, * prev_x = nil, * next_x = x-> sibling; // determine whether to connect the two elements based on the degrees between x and next-X. While (next_x! = Nil) {// Case 1: the degree is not equal if (X-> degree! = Next_x-> degree | // condition 2: X indicates the first (next_x-> sibling! = Nil & next_x-> sibling-> degree = x-> degree) {// point the pointer to the next position, prev_x = x; X = next_x ;} // Case 3: X-> the key is small. Connect next-X to X, remove else if (X-> key <= next_x-> key) from the root table {// remove next-XX-> sibling = next_x-> sibling; // binomial_link (next_x, x) of the left child who makes next-X;} // case 4: The Key keyword is small, X is connected to else on next-X {// remove X from the root table if (prev_x = nil) // X is the first root in the root table. H-> head = next_x; else // X is not the first root in the root table. prev_x-> sibling = next_x; // make X the leftmost child of next-x binomial_l Ink (x, next_x); // update X to enter the next iteration x = next_x;} next_x = x-> sibling;} head = H-> head ;} // insert node X into heap H void binomial_heap: binomial_heap_insert (node * X) {// construct a temporary two-way heap HH, only one node xbinomial_heap * hh = new binomial_heap; hh-> make_binomial_heap (); X-> P = nil; X-> child = nil; X-> sibling = nil; x-> degree = 0; hh-> head = x; // merge H and HH and release hhbinomial_heap_union (HH );} // extract the node * binomial_heap: binomial_heap_extract_min () with the minimum keyword () {// The minimum keyword must be at node * x = head, * Y = nil, * ret; int min; If (x = nil) on the root node of a Two-item tree) {// cout <"empty" <Endl; return nil;} min = x-> key; // 1. find the root X with the minimum key in the root list of H, // traverse the root node of each two trees. to delete this node, you also need to know the first root node of X while (X-> sibling! = Nil) {// find the minimum value if (X-> sibling-> key <min) {min = x-> sibling-> key; y = x ;} X = x-> sibling;} ret = x; // 1.and remove X from the root list of H // delete a node in two cases, the node is the first tree in the two items heap. After the node is deleted, the child of the node is saved to node * temp = NULL; If (y = nil) {x = head; temp = x-> child; head = x-> sibling;} // The node is not the first tree in the Two-item heap. else {x = Y-> sibling; y-> sibling = x-> sibling; temp = x-> child;} // 2. // If the node to be deleted is the root of the Two-item tree T, after the node is deleted, t becomes a two-item heap binomial_heap * hh = new binomi Al_heap (NiL); hh-> make_binomial_heap (); // 3. reverse the order of the linked list of X' childern, setting the P field of each child to nil, and set head [HH] to point to the head of the resulting list // under normal circumstances, the degree of tree in the two items heap is small to large. At this time, the degree of the tree in HH is from large to the row, so we need to make a reverse node * P; while (temp! = Nil) {P = temp-> sibling; temp-> sibling = HH-> head; hh-> head = temp; temp-> P = nil; temp = P ;} // 4. // The original two-item heap H deletes the two-item tree T and becomes the new two-item heap H, the two-item tree T deletes the root node and becomes the new two-item heap HH // merges H and HH into binomial_heap_union (HH); Return X ;} // reduce the key word of a node X in the Two-item heap h to a new value kvoid binomial_heap: binomial_heap_decrease_key (node * X, int K) {// error if (k> X-> key) {cout <"new key is greater than current key" <Endl; return ;} // reduce a keyword in the same way as the minimum binary heap, so that the keyword bubbles up In the heap X-> key = K; node * Y = X, * z = Y-> P; while (Z! = Nil & Y-> key <z-> key) {swap (Y-> key, Z-> key); swap (Y-> data, z-> data); y = z; Z = Y-> P ;}// delete a keyword void binomial_heap: binomial_heap_delete (node * X) {// change the value to the minimum value and raise it to the top of the heap: binomial_heap_decrease_key (x,-0x7fffffff); // Delete the heap top element binomial_heap_extract_min ();}
Main. cpp
# Include <iostream> using namespace STD; # include "binomial_heap.h" int main () {char ch; int N; // generate an empty binary heap binomial_heap * H = new binomial_heap; H-> make_binomial_heap (); // various tests while (CIN> CH) {Switch (CH) {Case 'I': // insert an element {CIN> N; node * x = new node (n, H-> nil); H-> binomial_heap_insert (X ); break;} case 'M': // returns the minimum value {node * ret = H-> binomial_heap_minimum (); If (ret = H-> nil) cout <"empty" <Endl; elsecout <ret-> key <Endl; break;} case 'K': // change the value of a keyword, make it smaller {// because there is no search function, you can only test node * ret = H-> binomial_heap_minimum () for the minimum node; If (ret = H-> nil) cout <"empty" <Endl; else {CIN> N; H-> binomial_heap_decrease_key (Ret, n);} break;} case 'E ': // extract the minimum keyword value and delete {H-> binomial_heap_extract_min (); break;} case 'D' from the heap ': // delete a node {node * ret = H-> binomial_heap_minimum (); If (ret = H-> nil) cout <"empty" <Endl; elseh-> binomial_heap_delete (RET); break ;}} return 0 ;}

Iii. Exercise 19.1 two-item tree and two-item heap
19.1-1x is not the root, then degree [sibling [x] <degree [x] X is the root, then degree [sibling [x]> degree [x] 19.1-2degree [p [x]> degree [x]

19.2 two heap operations

19.2-1

You can directly view the Code with pseudo code.

// Combine the root tables of H1 and H2 into a monotonically ascending order of degrees. // combine the monotonically linked list of non-leading nodes and return the merged header, node * binomial_heap: binomial_heap_merge (binomial_heap * H1, binomial_heap * H2) {node * L1 = h1-> head, * L2 = h2-> head, * ret = nil, * c = ret, * temp; while (L1! = Nil & L2! = Nil) {If (L1-> degree <= L2-> degree) temp = L1; elsetemp = L2; If (ret = nil) {ret = temp; C = ret;} else {C-> sibling = temp; C = temp;} If (L1 = temp) L1 = L1-> sibling; else L2 = L2-> sibling;} If (L1! = Nil) {If (ret = nil) ret = L1; elsec-> sibling = L1;} else {If (ret = nil) ret = L2; elsec-> sibling = L2;} Delete H2; return ret ;}

19.2-2

19.2-3

19.2-5

If you can set the value of the keyword to positive infinity, binomial-heap-minimum cannot distinguish the two heap null and the minimum keyword infinity. You only need to differentiate them in the return.

BINOMIAL-HEAP-MINIMUM(H)1    y <- NIL2    x <- head[H]3    min <- 0x7fffffff4    while x != NIL5        do if key[x] < min6                then min <- key[x]7                         y <- x8              x <- sibling[x]9    if min = 0x7fffffff and head[H] != NIL10       then return head[H]11   return y

19.2-6

-0x7fffffff is not required. It can be smaller than the minimum value.

BINOMIAL-HEAP-DELETE(H)1    y <- BINOMIAL-HEAP-MINIMUM(H)2    BINOMIAL-HEAP-DECREASE-KEY(H, x, key[y]-1)3    BINOMIAL-HEAP-EXTRACT-MIN(H)

19.2-7

Maps a two-item heap h to a binary number x. The corresponding mode x = func (H) is:

If the root degree of a binary tree in H is K, K of X is set to 1.

(1) make a two-item heap h1 have X1 = func (H1), insert a node on H1 and change it to H2, with X2 = func (H2 ), then X2 = X1 + 1

(2) merge two heap H1, H2, H1, and H2 into two heap H3, with X1 = func (H1), X2 = func (H2), X3 = func (H3), then X1 + X2 = X3

19.2-8

To be resolved

 

Iv. Thinking questions 19-1-2-4-4 heap ideas 19-2 using the two-heap Minimum Spanning Tree Algorithm see Introduction to Algorithms
19-2 use the Minimum Spanning Tree Algorithm of the Two-item heap

 

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.