Heaps Flashcards
What is a ‘Priority Queue’ and what operations does it support?
An abstract data type that is similar to a regular queue, with the difference being that every item has a priority associated with it.
Additional Operations: Insert(x), deleteMin(x)
What are the runtimes for Linked lists, Balanced BSTs, and Heaps that implement priority queues?
Linked list
1. Insert(x): Theta(1)
2. deleteMin(): Theta(N)
Balanced BST
1. Insert(x): Theta(lnN)
2. deleteMin(): Theta(lnN)
Heaps
1. Insert(x): Theta(1)
2. deleteMin(): Theta(N)
3. Sorting: nlnN
What is a ‘heap’ data structure, and what are its traversal equations?
A heap is essentially a ‘complete’ tree stored in ‘level order’ inside an array.
Traversal Equations:
leftChild(i) = 2i
rightChild(i) = 2i+1
parent(i) = [i/2]
What are the two types of heaps.
Min-Heap ( >= x): The values of all the nodes in the subtree rooted in n are >= x.
Max-Heap ( <= x): The values of all the nodes in the subtree rooted in n are < = x.
What type of tree is stored in a heap?
A complete tree
What order do heaps store trees?
Level-order
What are the heap operation runtimes?
height: ln N
insert(x): O(lg N)
deleteMin(): O(lg N)
getMin(): Theta (1)
How are heaps built?
From the bottom up.
- Begin with an unordered array.
- Make sure that all subtrees in the last two layers are heaps.
- Move up layer by layer.
percolate Down(i) assumes that both subtrees under i are already heaps?