Heap & Priority Queue & Heap Sort

¡Supera tus tareas y exámenes ahora con Quizwiz!

What is the common use for heap?

- For building priority queues. - The heap is the data structure supporting heap sort. - Heaps are fast for when you often need to compute the minimum (or maximum) element of a collection. - Impressing your non-programmer friends.

How many kinds of heap?

- max-heap: parent nodes must always have a greater value than children. - min-heap: parent nodes must always have a value less than children.

What is the difference between binary search tree and heap?

- order of nodes: for bst, the left child always smaller than the node while right child always larger; In max-heap, children must be smaller; in min-heap, children must be larger. - memory: traditional trees take up additional memory for nodes objects and pointers to left/right childs. While heap only uses plain array of storage and no pointers. - binary search tree: binary search tree must be "balanced" so that most operations have O(log n) performance. For heap, we don't need the entire tree to sorted. We just need to have the heap property to fulfilled, and balancing isn't an issue. Because the way heap is structure is gurantee O(logn) performance. - searching: search a binary tree is really fast. Searching in heap is slow, the purpose of heap is to put the largest/smallet on the top for easy insert/delete.

What is shift up and down in heap?

- shift up: If the element is greater (max-heap) or smaller (min-heap) than its parent, it needs to be swapped with the parent. This makes it move up the tree. - shift down: If the element is smaller (max-heap) or greater (min-heap) than its children, it needs to move down the tree. This operation is also called "heapify".

What is insert in heap? How to insert? time complexity?

Adds the new element to the end of the heap and then uses shiftUp() to fix the heap. How? Take append the new value to the last of array, and then shift up if necessary - log(n)

What is heap?

Heap is a binary tree lives inside array. it doesn't use parent/child pointer.

What is peek complexity?

O(1)

What is the run time complexity of inserting into a heap?

O(log n)

What is the build heap complexity?

O(n)

What is the complexity for search in heap?

O(n), search the array.

What is remove in heap? How? time complexity?

Removes and returns the maximum value (max-heap) or the minimum value (min-heap). To fill up the hole that's left by removing the element, the very last element is moved to the root position and then shiftDown() fixes up the heap. - how? take the last item and put it on the first, and then shift down - log(n)

Does siftUp and siftDown has same time complexity?

The number of operations required for each operation is proportional to the distance the node may have to move. For siftDown, it is the distance from the bottom of the tree, so siftDown is expensive for nodes at the top of the tree. With siftUp, the work is proportional to the distance from the top of the tree, so siftUp is expensive for nodes at the bottom of the tree. Although both operations are O(log n) in the worst case, in a heap, only one node is at the top whereas half the nodes lie in the bottom layer. So it shouldn't be too surprising that if we have to apply an operation to every node, we would prefer siftDown over siftUp. Reference: https://aaronice.gitbooks.io/lintcode/content/data_structure/heapify.html

How to turn heap array back to a sorted heap?

Use heap sort.

How many level does a heap with n nodes have?

floor(log_2(n))

What is the formula for heap index?

if i is the index of node, parent(i) = floor((i-1)/2) left(i) = 2i + 1 right(i) = 2i + 2

What is the time complexity for shift up and down?

log(n)

What is the time complexity for heap sort?

nlogn

If we can build a heap in O(n), why doesn't that same logic work to make heap sort run in O(n) time rather than O(n log n)?

the work for heap sort is the sum of the two stages: O(n) time for buildHeap and O(n log n) to remove each node in order, so the complexity is O(n log n). You can prove (using some ideas from information theory) that for a comparison-based sort, O(n log n) is the best you could hope for anyway, so there's no reason to be disappointed by this or expect heap sort to achieve the O(n) time bound that buildHeap does. Reference: https://stackoverflow.com/questions/9755721/how-can-building-a-heap-be-on-time-complexity


Conjuntos de estudio relacionados

Chapter 4: Consumer and Producer Surplus

View Set

CHAPTER 18 LESSON 4: MONITORING BODY COMPOSITION

View Set

CH 3 AND 5 Managerial AC SmartBooks

View Set

Three types of Cartilage (UPDATED)

View Set

Chapter 30: Vital Signs (Vital Signs)

View Set