Heaps Flashcards

1
Q

True or false, when we remove the min/max of a tree, we don’t need to maintain the completeness of the tree.

A

False. We need to find the new min and set that as the new root

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

The parent of a node at index i is at index ________ (using the floor that results from integer division).

A

(i - 1) // 2 (i.e. floor)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the five step process for removing a node

A
  1. 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

After we remove the min/max node from a heap, the we first replace that the root with _____

A

the last node. In an array, this is the last element.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

We can efficiently IMPLEMENT the complete binary tree representation of a heap using a/an _______. Can any binary tree be represented using this?

A

array or dynamic array. Yes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Whats the pseudocode while loop for percolating a node down for when you remove a node from the heap?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

In a priority queue, whats the first element that gets dequeued?

A

The element with the highest priority

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Whats the fundamental idea of the priority queue ADT

A

The priority queue is an ADT that associates a priority value with each element.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What happens if we use an array to implement a binary tree but the tree isn’t complete

A

If the binary tree isn’t complete, then the array will be less space efficient because there will be gaps in the array

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What data structure is typically used to implement a priority queue?

A

A heap

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Pseudocode for removing minimum value from an array based heap

A
  1. 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Does a heap maintain BST property where the left child is smaller and right child is bigger?

A

No. It just has to be a complete tree.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What are the two requirements for a minimizing heap data structure (or min heap)

A

Must be a COMPLETE binary tree EVERY NODE’S value must be less than or equal to the values of its children

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

In a min or max heap, which node is always the lowest or max value?

A

The root of the tree

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Where is the lowest priority value located in a min heap?

A

The root node

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What operation do you use for adding a node? Percolate down or up the tree?

A

Up

17
Q

The _______ is an ADT that associates a priority value with each element. A ________ data structure is a complete binary tree in which every node’s value is less than or equal to the values of its children. A ___________ is typically implemented using a data structure called a ________.

A

priority queue min heap priority queue heap

18
Q

Whats the n-step process for removing the root node from a heap?

A
  1. Remove the root node 2. Replace the root node with with whatever the last element in the heap 3. Fix the heap by percolating that node down
19
Q

In an array-implemented heap, where is the last element and the “next open spot” in the heap

A

Last element is literally last element in the array, and next open spot is the index that follows after the last element in the array

20
Q

Using an array to implement a binary tree that is not complete is bad because

A

It can leave big gaps in the array, wasting space.

21
Q

When we add a new value to a min heap, we place it into the ________ and then percolate it ____ until its priority value is _____ than both of its children.

A

“next open spot”, up, less

22
Q

True or false, in an array representation of a heap, the order of the nodes in the array follows the breadth first search results

A

True.

23
Q

Pseudocode for inserting into an array based heap

A
  • Put new element at the end of the array.
  • Compute the inserted element’s parent index ((i - 1) / 2).
  • Compare the value of the inserted element with the value of its parent.
  • If the value of the parent is greater than the value of the inserted element, swap the elements in the array and repeat from step 2 else stop.
    • Do not repeat if the element has reached the beginning of the array.
24
Q

Whats the pseducode for percolating down the replacement node after removing the root node from a heap?

A

while priority > smallest child:

swap with smallest child

25
Q

Which operation do you use for removing a node? Percolate down or up the tree?

A

Down

26
Q

Whats the four step pseudocode process for inserting an element into the heap

A
  1. Put new element at the end of the array. 2. Find the index of the parent of the inserted element ((i - 1) / 2) 3. Compare the value of the inserted element with the value of its parent. 4. If the value of the parent is greater than the value of the inserted element, swap the elements in the array and repeat from step 2. Loop stops when #4 is false or the inserted element ends up at the beginning of the array
27
Q

The last node at the deepest level in the heap is located where in the array?

A

The very last element of the array.

28
Q

When removing a node from a MIN heap, what’s the node that gets removed?

A

Since the min heap is a tree, the root node is the lowest value in a min heap. The root node is the only node that ever gets removed in a min heap (and max heap)

29
Q

A min (or max) heap is maintained through the addition and removal of nodes via ________, which move nodes up and down the tree according to their priority values.

A

percolations

30
Q

In an array representation of a heap, the left and right children of a node at index i are stored at indices _________ and , respectively.

A

left: 2 * (i + 1), right: 2 * (i + 2)

31
Q

Whats the pseudocode while loop for percolating a node up when you ADD a node to the heap

A

while new priority value < parent’s priority value: swap new node with parent

32
Q

In an array representation of a heap, where is the root node stored?

A

Index 0

33
Q

When we add a new value to a heap, we always first add it at the ____ which is located ______

A

“next open spot”, which is in the last level to the right of the furthest left node