Ch 5: Heaps Flashcards

1
Q

what is a max-heap?

A

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

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

what is the root node?

A

a node with the maximum key in the entire tree, and therefore has no parents

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

what is insert?

A

an operation that inserts the node into the tree’s last level then swaps node with the parent until no max-heap property violation occurs

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

how is a level added and when is a new level added during insert?

A

a level is filled left to right before adding another level so that the tree’s height is always the minimum possible

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

what is percolating?

A

an upward movement of a node in a max-heap

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

what is remove?

A

an operation that removes the root node 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

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

what is the height of a max-heap?

A

logN

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

what is a min-heap?

A

similar to a max-heap but a node’s key is less than or equal to its children’s keys

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

how are heaps typically implemented?

A

arrays

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

what is used to refer to the nodes?

A

indices, not pointers since it is an array and not linked list

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

what is the formula to find the parent index from the child index?

A

lower of (i-1)/2

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

what is the formula to find the child index/indices from the parent index

A

(2i)+1 / (21i)+2

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

what is heap sort?

A
  • a sorting algorithm that takes advantage of a max-heap’s properties by repeatedly removing the max and building a sorted array in reverse order
  • sorts from least to greatest
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

what is heapify?

A

an operation that is used to turn an array into a heap

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

what is the formula to find the largest internal node index?

A

lower of N/2 -1

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

explain how heapsort works

A
  1. heapify array into a max-heap and initializing an index value to the size of the array minus 1
  2. swap last item w root and lower size by 1 (removes last item)
  3. percolate top node down to satisfy heap ordering property
  4. repeat until end index is 0
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

explain the heapsort algorithm

A
  • uses 2 loops to sort an array
  • first loop heapifies the array using MaxHeapPercolateDown
  • second loop removes the maximum value, stores that value at the end index, and decrements the end index, until the end index is 0
18
Q

what is a priority queue?

A

a queue where each item has a priority and items with higher priority are closer to the front of the queue than items with lower priority

19
Q

what is enqueue for a priority queue?

A

an operation that inserts an item such that the item is closer to the front than all items of lower priority, and closer to the end than all items of equal or higher priority

20
Q

what is dequeue for a priority queue?

A

an operation that removes and returns the item at the front of the queue, which has the highest priority

21
Q

is the priority the same for every priority queue?

A

no, it can be changed for each queue, sometimes it compares the data, sometimes it compares the priority value, sometimes order is least-greatest or greatest-least

22
Q

what is peek for a priority queue?

A

an operation that returns the highest priority item, without removing the item from the front of the queue

23
Q

what is EnqueueWithPriority for a priority queue?

A

an enqueue operation that includes an argument for the enqueued item’s priority

24
Q

what is the common way to implement priority queues?

A

heaps

25
Q

what is sorting?

A

the process of converting a list of elements into ascending or descending order

26
Q

what is shell sort?

A

a sorting algorithm that treats the input as a collection of interleaved lists, and sorts each list individually with a variant of the insertion sort algorithm

27
Q

what is a gap value?

A

a positive integer representing the distance between elements in an interleaved list

28
Q

how are gap values used in shell sort?

A

shell sort uses gap values to determine the number of interleaved lists

29
Q

how are interleaved lists created?

A
  1. starting at the first index, that node is part of list 1
  2. the next node for that list is the current node’s index + gap value
  3. continue until end of list is reached
  4. then go back to first index and move over by 1, repeat list creation until all nodes are separated
30
Q

does shell sort work if the interleaved lists do not have the same amount of elements?

A

yes, some lists may have less elements than the others but shell sort still works

31
Q

what is quicksort?

A

a sorting algorithm that repeatedly partitions the input into low and high parts(each part is unsorted) and then recursively sorts each of those parts

32
Q

what is a pivot?

A

any value within the array being sorted, commonly the value of the middle array element

33
Q

how is a pivot used in quicksort?

A

to partition the input, quicksort chooses a pivot to divide the data into low and high parts

34
Q

what is merge sort?

A

a sorting algorithm that divides a list into two halves recursively sorts each half, then merges the sorted halves to produce a sorted list, the recursive partitioning continues until a list of 1 element is reached, as a list of 1 element is already sorted

35
Q

what is radix sort?

A

a sorting algorithm specifically for an array of integers

36
Q

what is a bucket

A

a collection of integer values that all share a particular digit value

37
Q

how do buckets relate to radix sort?

A

the algorithm makes use of buckets and is a type of bucket sort

38
Q

what is a fast sorting algorithm?

A

a sorting algorithm that has an average runtime complexity of O(NlogN) or better

39
Q

what is an element comparison sorting algorithm?

A

a sorting algorithm that operates on an array of elements that can be compared to each other

40
Q

explain insert algorithm for heaps

A

An insert 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. The upward movement of a node in a max-heap is called percolating.

41
Q

explain remove algorithm for heaps

A

A remove 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. Because upon completion that node will occupy another node’s location (which was swapped upwards), the tree height remains the minimum possible.