Guest User

Dequeue

a guest
Nov 5th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.13 KB | None | 0 0
  1. HNode* Heap::dequeue() {
  2.     HNode* tree_root = tree[1];
  3.  
  4.     int index = tree.size() - 1;
  5.     HNode* last = tree[index];
  6.     tree.pop_back();
  7.  
  8.     if (index > 0) {
  9.         tree[1] = last;
  10.         // fixheap
  11.         HNode* root = tree[1];
  12.         int last_index = tree.size() - 1;
  13.         int other_index = 1;
  14.         bool more = true;
  15.         while (more) {
  16.             int child_index = 2 * other_index;
  17.             if (child_index <= last_index) {
  18.                 HNode* child = tree[2*index];
  19.                 if ((2*index + 1 <= last_index) && tree[2*index+1]->weight < child->weight) {
  20.                     child_index = 2 * other_index + 1;
  21.                     child = tree[2*other_index+1];
  22.                 }
  23.                 if (child->weight < root->weight) {
  24.                     tree[other_index] = child;
  25.                     child = tree[2*other_index+1];
  26.                 }
  27.                 else {
  28.                     more = false;
  29.                 }
  30.             }
  31.             else {
  32.                 more = false;
  33.             }
  34.         }
  35.         tree[other_index] = root;
  36.     }
  37.     count--;
  38.     return tree_root;
  39. }
Advertisement
Add Comment
Please, Sign In to add comment