Heaps, heapsort

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

Formula for left child of 'k'

2k

Formula for right child of 'k'

2k + 1

Shape property of Binary Heap Benefit?

A binary heap is a complete binary tree: all levels of the tree, except maybe the last level are fully filled. If a level is not filled, then the nodes are filled from left to right. It can represent an n-element key tree using exactly n elements of the array

How are heaps implemented? Why? How?

A binary tree represented as an array To not waste space by storing the pointers. 2 Pointers per node can easily outweigh the size of the keys We store the data as an array of keys, and use the position of the keys to satisfy the role of the pointers

Binary Heap

A binary tree that: 1) is a complete binary tree 2) follows the heap property

Disadvantages of storing a binary tree without pointers, as an array?

All missing internal nodes still take up space in our structure Loss of flexibility in that we cannot move subtrees round by changing a single pointer, we must move each element in the subtree

Time complexity of extracting a minimum element from a heap?

Bubbling down takes O(logn) because each swap takes constant time but it will reach a leaf after floor(lg n) bubble down steps

How are heaps constructed?

By incrementally inserting a new element into the left-most open spot in the array to maintain the desired balanced shape of the tree Then to bubble up the new key to its proper position in the hierarchy to maintain the heap property

Time complexity of heap construction?

Each insertion takes at most O(log n) time because each swap takes constant time and the height of an n-element heap is floor(lg n) And the initial heap of 'n' elements needs n insertions. So, O(n log n) time

Formula for parent of 'k'

Floor( k / 2 ) (int) k / 2

Although other sorting algorithms are faster, what is heapsort good for?

Good for sorting data that sits in the computer's main memory

Heapify / Bubbling down operation

Having a dissatisfied element at the root and swapping it with its children until it satisfies a heap property

What are binary heaps used for?

Implementing priority queues

What would 'heapsort' algorithm do? Time complexity?

Initialize a heap with an element Iteratively extract the minimum into a sorted array O(n log n) for the n-element extraction of the minimum element

Why can't we use arrays for binary search trees?

Loss of flexibility of moving a subtree around by changing a single pointer

Min-heap property

Nodes are greater than or equal to their parent Nodes are smaller than or equal to their children

Max-heap property

Nodes are less than or equal to their parent Nodes are greater than or equal to their children

Can we efficiently search (binary search) for a key in a heap?

Nope We cannot use binary search because a heap is not a binary search tree We don't know anything about the relative order of the n/2 leaf elements. So we can't avoid doing a linear search through them

Does it matter to the time complexity that you can construct the heap in linear time?

Nope since the construction time does not dominate the complexity of heapsort, it will not improve its worst-case performance

Heapsort is nothing but an implementation of ___________ with ________? Why? Time complexities?

Selection sort the right data structure Selection sort repeatedly extracts the smallest remaining element from the unordered set, then the smallest item is swapped with the ith item. Priority queues have the same operations of removing a particular item from the array and finding the smallest item. If we use a priority queue, finding the minimum element is O(1) and deleting it is O(logn) Speeding up selection sort from O(n^2) to O(n log n)

Heap property

The key stored in each node is either greater than or equal or less than or equal to the keys in the node's children

How do you extract the minimum element from a heap?

The minimum element would be in the first position of the array. Removing that element leaves a hole. The hole is filled by moving the right-most leaf (n-th position) into the first position That element needs to be bubbled down until it dominates all its children

What is partial order for heaps?

The term describing how the heap is maintained. It is weaker than sorted order (so it can be efficient to maintain) yet stronger than random order (so the minimum element can be quickly identified)

What would happen if you don't enforce the complete binary tree property by packing everything to the left?

We would need 2^n array to store n-elements instead of an n sized array

Is heapsort an in-place sorting algorithm?

Yes Because it uses no extra memory than the array containing the elements to be sorted


Set pelajaran terkait

Economics - 8th - Chapter 6 - Section 1 - Combining Supply and Demand

View Set

Health and Accident Insurance Ch. 1

View Set

American Revolution Battles Chart

View Set

Comptia Network+ Class Review Questions

View Set

Easy/Medium Missed Questions on Archer

View Set

Soc. 150 Exam 2 Ethnic solidarity for economic survival

View Set

AC 300 Chp 1 Smartbook Questions

View Set

Simulated Exam Review - maybe L&H

View Set