cs126 heaps

0.0(0)
studied byStudied by 0 people
GameKnowt Play
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/18

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

19 Terms

1
New cards

What is a heap?

  • A binary tree storing keys at its nodes and satisfying the following properties:

    • Heap order

    • Complete binary tree

2
New cards

Define complete binary tree.

  • For i = 0, …, h - 1: there are 2i nodes of depth i

  • At depth h, the internal nodes are to the left of external nodes

3
New cards

Define heap order.

For every internal node, its key is greater than or equal to the key of its parent.

4
New cards

How can a priority queue be implemented using a heap?

Each internal node of the heap stores a (key,element) item.

5
New cards

What is the height of a heap storing n entries?

⌊logn⌋

6
New cards

Describe how an item k is inserted into a heap.

  • Find the insertion node z (the new last node)

  • Store k at z

  • Restore the heap-order property (by upheap bubbling)

7
New cards

Describe how the smallest item is removed from a heap.

  • Replace the root key with the key of the last node w

  • Remove w

  • Restore the heap-order property (by downheap bubbling)

8
New cards

Describe the upheap algorithm.

  • Restores the heap-order property after a new element is added 

  • Swaps k (the new node) along an upward path from the insertion node

  • It terminates when the key k reaches:

    • The root

    • A node whose parent has a key smaller than or equal to k

9
New cards

What is the worst-case time cost of performing the upheap algorithm?

  • O(log n)

  • As a heap has height O(log n)

10
New cards

Describe the downheap algorithm.

  • It restores the heap-order after the smallest item is removed

  • Swaps the key k (the root) along a downward path from the root node

  • Terminates when k reaches:

    • A leaf

    • A node whose children are greater than or equal to k

11
New cards

What is the worst-case time cost of performing the upheap algorithm?

  • O(log n)

  • As a heap has height O(log n)

12
New cards

Describe how a heap can be implemented using an array.

  • For a heap with n keys, an array of length n is used

  • For the node at index i:

    • Left child is at 2i + 1

    • Right child is at 2i + 2

    • Parent node is at index (i - 1)/2

13
New cards

What is a heap sort?

A sort when all elements are inserted in a heap, then removeMin() is called until all elements are removed (sorted).

14
New cards

What is the worst-case time cost of performing a heap sort?

O(nlogn)

15
New cards

What is the worst-case time cost of performing the priority queue method size() / isEmpty() / min() on a heap?

O(1)

16
New cards

What is the worst-case time cost of performing the priority queue method insert() on a heap?

O(n)

17
New cards

What is the worst-case time cost of performing the priority queue method removeMin() on a heap?

18
New cards

Describe the heapify algorithm.

  • Starting at index i = (n - 1)/2

  • Call downHeap() until i = 0, decrementing i at each step

19
New cards

heapify function proof?? what is its purpose? used in heap sort?