Heap and Priority Queue
Priority Tree are often used to the implementation of algorithms. It can improve the performance of these algorithms including Backtracking, Sorting and Graph algorithms.
Binary Heap
Complete Binary Tree, Perfect Binary Tree
Put: put the item to the next position in the complete binary tree and make it heap ordered by moving it from bottom to the root. O (log n)
Remove: remove the root element and move the last element in complete binary tree to the root and make it heap ordered by moving it form root to bottom. O (log n)
Build Heap from Array: move every internal node to the bottom to make the heap ordered. O (n)
Leftist Heap
Leftist Tree: path length of the right subtree is no more than left one for every subtree
Merge: using recursion, the tree with the bigger root merge with the right subtree of another tree. And then, if the path length of right subtree is more than left one, exchange them. O (log n1+log n2)
Put: Merge the original tree with a new leftist heap containing only the new node O (log n)
Remove: remove the root and merge the two children of the root O (log n)
Binomial Queue
Binomial Tree: B0={R}, Bk={R, Bk-1, Bk-2….B0}
The number of nodes at level l in the binomial tree of order k is given by the binomial coefficient (k, l)
Binomial Queue: A forest of binomial tree with heap order
Remove tree: best O (1), worst O (log n)
Add tree: O (1)
Find minimum: O (log n)
Merge: O (log (m + n))
Put: Merge the original binomial queue with a new one containing the only node O (log n)
Remove: remove the root of the minimum binomial tree and then add all its children to the binomial queue
Set
Array Set
Bit-Vector Set
Multiset
Partition
Implement a partition using a forest (using array)
The nodes in the partition tree have three fields – items, parent and rank
Find: find the root of the partition tree
Join: join two partition tree
Collapsing find: 1 find the root 2 assign all the nodes on the finding path to the root
Union by size: attach the small tree to the root node of the larger one.
The height of partition tree is guaranteed to be O (log n)
Union by rank (height): attach the tree with lower height to the one with higher height
The height of partition tree is guaranteed to be O (log n)
Application: processing the Equivalence Relation (reflexive, symmetric, transitive)