130 likes | 156 Views
Learn about binary heaps, heap properties, operations like insert, delete, increase/decrease key, merge, and implementations using arrays or pointers.
E N D
CISC220Fall 2009James Atlas Nov 9: Heaps
Remaining Course Topics • Heaps • Hashing • Graphs
Heaps • Specialized Tree • Often are Binary Heaps • Satisfy Heap Property • if B is a child node of A, then key(A) ≥ key(B) [in a Max-Heap] • if B is a child node of A, then key(A) key(B) [in a Min-Heap]
Heap operations • insert: • adding a new key to the heap • delete-max or delete-min: • removing the root node • increase-key or decrease-key: • updating a key within a max- or min-heap, respectively • merge: • joining two heaps to form a valid new heap containing all the elements of both.
Heap Insert • Add the element on the bottom level of the heap. • Compare the added element with its parent; if they are in the correct order, stop. • If not, swap the element with its parent and return to the previous step.
Heap Insert (example) 1 2 3
Heap Delete • Replace root with last element • Find max (or min) between root and children • Swap if child > root, recursively do find max on subtree now rooted at new child
Implementations • Array? • Pointers?