Heaps Flashcards
True or false, when we remove the min/max of a tree, we don’t need to maintain the completeness of the tree.
False. We need to find the new min and set that as the new root
The parent of a node at index i is at index ________ (using the floor that results from integer division).
(i - 1) // 2 (i.e. floor)
What is the five step process for removing a node
- Remember the value of the first element in the array (to be returned later). 2. Replace the value of the first element in the array with the value of the last element and remove the last element. 3. If the array is not empty (i.e., it started with more than one element), compute the indices of the children of the replacement element (2 * i + 1 and 2 * i + 2). ( If both of these elements fall beyond the bounds of the array, we can stop here.) 4. Compare the value of the replacement element with the minimum value of its two children (or possibly one child). 5. If the replacement element’s value is greater than its minimum child’s value, swap those two elements in the array, and repeat from step 3.
After we remove the min/max node from a heap, the we first replace that the root with _____
the last node. In an array, this is the last element.
We can efficiently IMPLEMENT the complete binary tree representation of a heap using a/an _______. Can any binary tree be represented using this?
array or dynamic array. Yes.
Whats the pseudocode while loop for percolating a node down for when you remove a node from the heap?
while priority > smallest child priority: swap with smallest child where priority is the new root node, and smallest child priority is the smallest child of that node
In a priority queue, whats the first element that gets dequeued?
The element with the highest priority
Whats the fundamental idea of the priority queue ADT
The priority queue is an ADT that associates a priority value with each element.
What happens if we use an array to implement a binary tree but the tree isn’t complete
If the binary tree isn’t complete, then the array will be less space efficient because there will be gaps in the array
What data structure is typically used to implement a priority queue?
A heap
Pseudocode for removing minimum value from an array based heap
- Remember the value of the first element in the array (to be returned later).
- Replace the value of the first element in the array with the value of the last element and remove the last element.
- If the array is not empty (i.e., it started with more than one element), compute the indices of the children of the replacement element (2 * i + 1 and 2 * i + 2).
- If both of these elements fall beyond the bounds of the array, we can stop here.
- Compare the value of the replacement element with the minimum value of its two children (or possibly one child).
- If the replacement element’s value is greater than its minimum child’s value, swap those two elements in the array, and repeat from step 3.
Does a heap maintain BST property where the left child is smaller and right child is bigger?
No. It just has to be a complete tree.
What are the two requirements for a minimizing heap data structure (or min heap)
Must be a COMPLETE binary tree EVERY NODE’S value must be less than or equal to the values of its children
In a min or max heap, which node is always the lowest or max value?
The root of the tree
Where is the lowest priority value located in a min heap?
The root node