Week 8 Flashcards
What is a heap?
A heap is a tree based data structure. Mainly used for priority queues and order statistics.
What are the different types of heaps?
Binary heaps
Binomial heaps
Fibonnacci heaps
What is a binary heap?
A complete binary tree, which is a binary tree in which all the levels are completely filled except possibly the lowest one which is filled from the left.
How can a binary heap be implemented as a Max heap?
The largest element is always the root node.
The value of each nodeis greater than the value of its children.
The tree is complete.
The tree is perfectly balanced.
The leaf nodes in the last level are all pushed to the left.
Height is O(log n)
How can we implement a heap as a min heap?
Similar to max except the value of each node is less than the value of its children.
The root contains the smallest element.
Are heaps perfectly ordered?
No, heaps are not perfectly ordered.
Order is only maintained through linear lines of descent between parent and children only.
How can heaps be implemented in arrays?
Elements are stored from top to bottom and from left to right at each level.
The root node is always at index = 0.
In an array heap, at where is the position of the left child and the right child if the node index is i?
What is the position of a parent of a node at index = 1?
The position of the left child is 2i+1
The position of the right child is 2i+2
The position of the parent of the node is (i-1)/2
How can a heap be implemented in a priority Queue?
Enqueue procedure:
add the new element to the end of the heap.
If necessary restore the heap property by swapping the element with its parent until the proper position is found or we are at the root.
Dequeue procedure
Remove the root element (highest priority)
Replace it with the last leaf node.
If necessary restore the heap property by swapping the rooy with its larger child, (reheaping downwards).
What is the complexity of top up heap creation in the worst case?
O(nlogn)
What is the worst case complexity for bottom up heap creation.
O(n) is the worst case.
What are the steps for top down heap creation.
Start with empty heap
Sequentially enqueue new elements
What are the steps for bottom up heap creation.
- Start with the last non-leaf node (n/2)-1
- Set index to this position
- If necessary restore the heap property by swapping with the largest child.
- Repeat until proper place found or becomes a leaf node.
- Repeat step 2 after decrementing index. Stop once the root has been processed.
What is heapsort? What is the procedure?
Is an inplace sort of an array.
Procedure:
Reorganize the array into a Max heap
Bottom up method is quickest
for (i = n-1; i>0; iā)
Swap root with element i
Restore heap property for elements 0 to i-1
In heapsort, what is the worst case, average case and best case complexity.
Is O(nlogn) in worst and average cases
Is O(n) in the best case for an array containing identical elements.