Advertisement
SVXX

Binomial Heap Operations

Feb 24th, 2014
466
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. BINOMIAL-HEAP-MERGE merges the root lists of H1 and H2 into a single linked list
  2. that is sorted by degree into monotonically increasing order. Similar to MERGE of MERGESORT.
  3.  
  4. BINOMIAL-LINK(y, z)
  5. 1 p[y] ← z
  6. 2 sibling[y] ← child[z]
  7. 3 child[z] ← y
  8. 4 degree[z] ← degree[z] + 1
  9.  
  10. BINOMIAL-HEAP-UNION(H1, H2)
  11. 1 H ← MAKE-BINOMIAL-HEAP()
  12. 2 head[H] ← BINOMIAL-HEAP-MERGE(H1, H2)
  13. 3 free the objects H1 and H2 but not the lists they point to
  14. 4 if head[H] = NIL
  15. 5 then return H
  16. 6 prev-x ← NIL
  17. 7 x ← head[H]
  18. 8 next-x ← sibling[x]
  19. 9 while next-x ≠ NIL
  20. 10 do if (degree[x] ≠ degree[next-x]) or (sibling[next-x] ≠ NIL and degree[sibling[next-x]] = degree[x])
  21. 11 then prev-x ← x //Cases 1 and 2
  22. 12 x ← next-x // Cases 1 and 2
  23. 13 else if key[x] ≤ key[next-x]
  24. 14 then sibling[x] ← sibling[next-x] // Case 3
  25. 15 BINOMIAL-LINK(next-x, x) // Case 3
  26. 16 else if prev-x = NIL // Case 4
  27. 17 then head[H] ← next-x ▹ Case 4
  28. 18 else sibling[prev-x] ← next-x ▹ Case 4
  29. 19 BINOMIAL-LINK(x, next-x) ▹ Case 4
  30. 20 x ← next-x ▹ Case 4
  31. 21 next-x ← sibling[x]
  32. 22 return H
  33.  
  34. BINOMIAL-HEAP-INSERT(H, x)
  35. 1 H' ← MAKE-BINOMIAL-HEAP()
  36. 2 p[x] ← NIL
  37. 3 child[x] ← NIL
  38. 4 sibling[x] ← NIL
  39. 5 degree[x] ← 0
  40. 6 head[H'] ← x
  41. 7 H ← BINOMIAL-HEAP-UNION(H, H')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement