Heaps and Treaps - Chapter 7 Flashcards
A blank is a complete binary tree that maintains the simple property that a node’s key is greater than or equal to the node’s children’s keys.
max-heap
a max-heap’s blank always has the maximum key in the entire tree.
root
An blank into a max-heap starts by inserting the node in the tree’s last level, and then swapping the node with its parent until no max-heap property violation occurs. Inserts fill a level (left-to-right) before adding another level, so the tree’s height is always the minimum possible.
insert
The upward movement of a node in a max-heap is called blank.
percolating
A blank from a max-heap is always a removal of the root, and is done by replacing the root with the last level’s last node, and swapping that node with its greatest child until no max-heap property violation occurs
remove
what is the height of a max-heap?
floor(logN)
Given a max-heap with N nodes, what is the worst-case complexity of an insert, assuming an insert is dominated by the swaps?
O(logN)
Given a max-heap with levels 0, 1, 2, and 3, with the last level not full, after inserting a new node, what is the maximum possible swaps needed?
3
Given a max-heap with N nodes, what is the complexity for removing the root?
O(logN)
A blank is similar to a max-heap, but a node’s key is less than or equal to its children’s keys.
min-heap
A customer with a higher priority has a blank numerical value in the min-heap.
lower
Heaps are typically stored using blank. Given a tree representation of a heap, the heap’s array form is produced by traversing the tree’s levels from left to right and top to bottom. The root node is always the entry at index 0 in the array, the root’s left child is the entry at index 1, the root’s right child is the entry at index 2, and so on.
arrays
Because heaps are not implemented with node structures and parent/child pointers, traversing from a node to parent or child nodes requires referring to nodes by blank.
index
Node index 0 Parent index ? Child indices ?
None
1,2
Node index 1 Parent index ? Child indices ??
0
3,4
Node index 2 Parent index ? Child indices ?
0
5,6
Node index 3 Parent index ? Child indices ?
1
7,8
Node index 4 Parent index? Child indices?
1
9, 10
Node index 5 Parent index ? Child indices??
2
11, 12
Node index i Parent index ? Child indices??
floor((i-1)/2)
2 * i +1, 2* i + 2
MaxHeapPercolateUp(nodeIndex, heapArray) {
while (nodeIndex > 0) {
parentIndex = (nodeIndex - 1) / 2
if (heapArray[nodeIndex] <= heapArray[parentIndex])
return
else {
swap heapArray[nodeIndex] and heapArray[parentIndex]
nodeIndex = parentIndex
}
}
}
Max-heap percolate up algorithm.
MaxHeapPercolateDown(nodeIndex, heapArray, arraySize) {
childIndex = 2 * nodeIndex + 1
value = heapArray[nodeIndex]
while (childIndex < arraySize) {
// Find the max among the node and all the node’s children
maxValue = value
maxIndex = -1
for (i = 0; i < 2 && i + childIndex < arraySize; i++) {
if (heapArray[i + childIndex] > maxValue) {
maxValue = heapArray[i + childIndex]
maxIndex = i + childIndex
}
}
if (maxValue == value) { return } else { swap heapArray[nodeIndex] and heapArray[maxIndex] nodeIndex = maxIndex childIndex = 2 * nodeIndex + 1 } } }
Max-heap percolate down algorithm.
blank = (node_index - 1) // 2
parent_index
blank = 2 * node_index + 1
left_child_index
blank = 2 * node_index + 2
right_child_index
The MaxHeap methods blank and blank make use of two methods, percolate_up() and percolate_down().
insert() and remove()