Topic #9: Heaps, Heapsort, Priority Queues

Ace your homework & exams now with Quizwiz!

What type of binary tree is a heap? What property must they satisfies?

Heap is a nearly complete binary trees that satisfy the heap property.

Run Build Max Heap on the data: keys: 1 2 3 4 5 indices: 1 2 3 4 5

5 4 3 1 2 Max-Heapify is first called on index 2 as idices 2 3 4 are already roots of max heaps. This swaps 2 and 5. Then it calls Max-Heapify at index 1, which propagates 1 down to the left most position.

Suppose you have a binary heap represented as an array using 1-based indexing (root is at index 1). An item is at index 31 in the array: what is the index of its right child?

63

Show the array representation of the max priority queue after HEAP-INCREASE-KEY of the item with key 1 to key 7, by entering integers for the keys: keys: 6 4 5 1 3 indices: 1 2 3 4 5

7 6 5 4 3

Insert the key 9 to this data: keys: 8 5 7 3 _ indices: 1 2 3 4 5

9 8 7 3 5

What is the heap property?

A standard that the heap is either a full binary tree or a nearly complete binary tree. Meaning there cannot be any incomplete or empty slots in any level. There is a max heap and min heap property

Suppose you have a binary heap represented as an array using 1-based indexing which means the root is at index 1. An item is at index 31 in the array what is the index of its parents? How does this work? Are there any patterns when solving this?

Answer for 31: 15

How many numbers of nodes are in a nearly complete binary tree of height h?

At most 2^(h+1)-1 nodes which are represented as vertices. Between 2^h and 2^(h+1)-1

What is the max-heap property?

For all nodes i, excluding the root, key[parent(i)] ≥ key[i] That is, the value of a node is at most the value of the parent. Guarantees that the maximum key of a max-heap is at the root

What is the min-heap property?

For all nodes i, other than the root, key[parent(i)] ≤ key[i] That is the the smallest element is at the root of the heap

Flip to the defintion side: It is a max heap because it is a nearly complete binary tree

Is this a max heap? Explain the answer

Flip to the defintion side: Its not a max heap because it is not a nearly complete binary tree

Is this a max heap? Explain the answer

Flip to the defintion side: It violates the max heap property

Is this a max heap? Explain why to your choice

What is the height of a node?

The number of edges on the longest downward path from the node to a leaf

What are heaps the most useful for as a data structure?

Using it as an application to sorting and priority queues

How many number of leaves does an n-node nearly complete binary tree have?

ceiling[n/2]

Whats a nearly complete binary tree?

complete at all levels except the lowest level

What is the height of an n-node nearly complete binary tree?

h= floor[ lgn ]


Related study sets

SIARKA W ZWIĄZKACH ORGANICZNYCH

View Set

Circulatory System: The Heart & Blood Vessels

View Set

Acct 330 - Income Tax Chapter 2 - T/F Multiple Choice

View Set

Chapter 55 Clients W/Male Reproductive Disorders

View Set