Advanced heaps Flashcards
Mergable heap methods (5 basic +2 extra)
Make-Heap, Insert, Minimum, Extract-Min, Union, Decrease-Key, Delete
Binomial tree features (B(k)) (4 features)
- Built out of two connected B(k-1) trees.
- Has 2powk nodes.
- Height of k.
- At level I there are exactly (k i) nodes.
Binomial heap definition
- A group of binomial trees that maintain the heap property.
- At most one binomial tree of a certain degree h.
Binomial heap implementation
- All the binomial trees roots point to the next one, where the first is the tree with the lowest degree, and last the highest.
Binomial heap similarity
Binomial heaps are very similar to the binary counting system. The number of nodes directly corresponds to the binary counting system with correlation to k. Merging two heaps is same to adding to binary numbers.
Binomial heap facts to know (2 facts)
- the right tree of each node is a root of a tree of one level lower.
- 0β€x.ππππππβ€x.π.ππππππβ, the degree of a son node is always at least one less than the father node.
Binomial heap union steps
- Merge two BH to be one sorted linked list that includes all binomial trees.
- Merge every two trees of the same degree until there is a maximum of one treefor a certain degree.
Tree merging cases (4)
- Current degree is different than next one (π₯.ππππππβ πππ₯π‘βπ₯.ππππππ) -> increment pointers.
- x is first of a 3 same degree trees (π₯.ππππππ=πππ₯π‘βπ₯.ππππππ=πππ₯π‘βπ₯.π ππππππ.ππππππ) -> continue.
- x is first of two same degree keys (π₯.ππππππ=πππ₯π‘βπ₯.ππππππβ πππ₯π‘βπ₯.π ππππππ.ππππππ). and π.πππβ€ππππβπ.πππ -> merge them with x as root.
- x is first of two same degree keys (π₯.ππππππ=πππ₯π‘βπ₯.ππππππβ πππ₯π‘βπ₯.π ππππππ.ππππππ). and π.πππ>ππππβπ.πππ -> merge them with next-x as root.
Binomial-Heap-Insert
Insert new Binomial tree and then merge
Binomial-Heap-Extract-Min
Find smallest root (to return). Then for each child left add it as its own tree (in opposite order so that the sort is preserved) and merge all.
Binomial-Heap-Decrease-Key
Increase the key and then bubble it up
Binomial-Heap-Delete
Decrease key to -inf and remove minimum.
Fibonacci heap (idea)
A linked list of trees. The pointer to the heap is the smallest root node. Each node has a degree field and a mark to specify if it has lost a child.
Fibonacci heap (insert, min, union)
When inserting a new element, add the element as a new node. The minimum is pointed to by the heap so itβs easy to get the value. For union, just combine the two root lists but make sure to update the minimum pointer. All done in O(1).
Fibonacci heap (extract min)
Take all the children nodes and add them to the root list. Then start merging trees with the same degree. The amortized cost is O(logn)