CSCI 251 Ch. 8 Heaps and Treaps

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

When performing an insert, indicate each node's new location using the template tree's labels (n1...n7). Where will a new node P first be inserted?

n6 P > M, so descend to R. P < R, so insert P as R's left child.

The upward movement of a node in a max-heap

percolating

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.

priority queue

A ______ 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.

remove

Heap sort algorithm

see image

Implementing priority queues with heaps

see image

Percolate down algorithm

see image

Percolate up algorithm

see image

parent and child indices for a heap

see image

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

min-heap

Node D was just inserted, and assigned a random priority of 47. Rotations are needed to not violate the heap property. Match the node value to the corresponding location in the tree template on the right after the rotations are completed.

n1 = d, 47 n2 = b, 12 n3 = g, 30 n4 = a, 5 n5 = h, 18

When performing an insert, indicate each node's new location using the template tree's labels (n1...n7). P is assigned a random priority of 65. To where does P percolate?

n3

When performing an insert, indicate each node's new location using the template tree's labels (n1...n7). Where will a new node H first be inserted?

n5

When performing an insert, indicate each node's new location using the template tree's labels (n1...n7). H is assigned a random priority of 20. To where does H percolate?

n5 The heap property is not violated. A rotation is not needed, so H stays at n5.

a complete binary tree that maintains the simple property that a node's key is greater than or equal to the node's childrens' keys.

max-heap

Which array could be heapified with the fewest number of operations, including all swaps used for percolating? (10, 20, 30, 40) (30, 20, 40, 10) (10, 10, 10, 10)

(10, 10, 10, 10) Heapifying this array doesn't require any swaps, since each node already satisfies the heap property.

Suppose the original array to be heapified is (11, 21, 12, 13, 19, 15). What is the heapified array? (11, 21, 12, 13, 19, 15) (21, 19, 15, 13, 12, 11) (21, 19, 15, 13, 11, 12)

(21, 19, 15, 13, 11, 12)

Each question starts from the original tree. Use this text notation for the tree: (J (F (C, G), Q (M, T))). A - means the child does not exist. What is the tree after removing G? (J (F (C, -), Q (M, T))) (J (C (-, F), Q (M, T)))

(J (F (C, -), Q (M, T)))

Each question starts from the original tree. Use this text notation for the tree: (J (F (C, G), Q (M, T))). A - means the child does not exist. What is the tree after removing Q? (J (F (C, G), M(-, T)) (J (F (C, G), T(M, -))

(J (F (C, G), M(-, T))

Assume servicePQueue is a priority queue with contents: 11, 22, 33, 44, 55. After calling Dequeue(servicePQueue) a total of 5 times, what will GetLength(servicePQueue) return? -1 0 Undefined

0 If all items have been removed, the priority queue's length is 0.

Assume servicePQueue is a priority queue with contents: 11, 22, 33, 44, 55. What does Dequeue(servicePQueue) return? 5 11 55

11

Suppose the original array to be heapified is (11, 21, 12, 13, 19, 15). What are the last 2 elements swapped? 11 and 19 11 and 21 19 and 21

11 and 19 Element 11 is less than both 13 and 19, and is swapped with the larger of the 2 children.

Suppose the original array to be heapified is (11, 21, 12, 13, 19, 15). What are the first 2 elements swapped? 11 and 21 21 and 13 12 and 15

12 and 15 The percolate down operation is performed first on node 12. 15 is greater than 12, and the 2 elements are swapped.

Suppose the original array to be heapified is (11, 21, 12, 13, 19, 15). The percolate down operation must be performed on which nodes? 15, 19, and 13 12, 21, and 11 All nodes in the heap

12, 21, and 11 The percolate down operation is performed on internal nodes only. Nodes 12, 21, and 11 are the heap's internal nodes.

What are the child indices for a node at index 6? 7 and 8 12 and 13 13 and 14 12 and 24

13 and 14

How many times will MaxHeapPercolateDown be called by Heapsort when sorting an array with 10 elements? 5 10 14 20

14 The first loop iterates for i = 4, 3, 2, 1, and 0, and the second loop iterates for i = 9, 8, 7, 6, 5, 4, 3, 2, and 1. MaxHeapPercolateDown is called once for each loop iteration, making 5 + 9, or 14, calls total.

Assume servicePQueue is a priority queue with contents: 11, 22, 33, 44, 55. After dequeuing an item, what will Peek(servicePQueue) return? 11 22 33

22 Peek returns the item at the queue's front, and 22 is now at the front.

Given a max-heap with levels 0, 1, 2, and 3, with the last level not full, after inserting a new node, what is the maximum possible swaps needed? 1 2 3

3 The node is inserted into level 3. If the node's key is the new maximum, swaps will occur with the parent in level 2, then level 1, and finally level 0 (the root).

For an array with 7 nodes, how many percolate-down operations are necessary to heapify the array?

3 The percolate down operation must be performed on nodes at indices 2, 1, and 0 to heapify the array.

Assume servicePQueue is a priority queue with contents: 11, 22, 33, 44, 55. What does GetLength(servicePQueue) return? 5 11 55

5

What is the parent index for a node at index 12? 3 4 5 6

5

For an array with 10 nodes, how many percolate-down operations are necessary to heapify the array?

5 The percolate down operation must be performed on nodes at indices 4, 3, 2, 1, and 0 to heapify the array.

Assume that lower numbers have higher priority and that a priority queue currently holds items: 54, 71, 86 (front is 54). The dequeue operation would return which item? 54 71 86

54 The dequeue operation returns the item at the front of the queue. 54 is at the front of the queue.

An _____ 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.

insert

Assume that lower numbers have higher priority and that a priority queue currently holds items: 54, 71, 86 (front is 54). Where would an item with priority 60 reside after being enqueued? Before 54 After 54 After 86

After 54 Item 60 is lower priority than 54, but higher than 71, and will therefore reside between items 54 and 71 after being enqueued.

Assume that lower numbers have higher priority and that a priority queue currently holds items: 54, 71, 86 (front is 54). Where would an additional item with priority 54 reside after being enqueued? Before the first 54 After the first 54 After 86

After the first 54 Enqueued items are inserted after existing items of equal priority, so the second 54 will reside after the first.

Returns and removes the item at the front of PQueue

Dequeue(PQueue)

A priority queue implementation that requires objects to have a data member storing priority would implement the _____ function. Enqueue EnqueueWithPriority

Enqueue The Enqueue function doesn't require priority as an argument, since the priority is already within the enqueued object.

Inserts x after all equal or higher priority items

Enqueue(PQueue, x)

A priority queue implementation that does not require objects to have a data member storing priority would implement the _____ function. Enqueue EnqueueWithPriority

EnqueueWithPriority A priority still must be associated with each object in the priority queue. If the object doesn't have priority data, then the EnqueueWithPriority function would be implemented, and the priority would be passed as an argument.

Returns the number of items in PQueue

GetLength(PQueue)

Heap storage

Heaps are typically stored using arrays. Given a tree representation of a heap, the heap's array form is produced by traversing the tree's levels from left to right and top to bottom. The root node is always the entry at index 0 in the array, the root's left child is the entry at index 1, the root's right child is the entry at index 2, and so on.

Returns true if PQueue has no items

IsEmpty(PQueue)

(a) Treap Not a treap

Not a treap M's priority must be ≥ G's.

(d) Treap Not a treap

Not a treap Priorities obey heap property, but keys don't obey BST ordering property: H cannot be G's left child. Both properties must be obeyed in a treap.

Given a max-heap with N nodes, what is the worst-case complexity of an insert, assuming an insert is dominated by the swaps? O(N) O(logN)

O(logN) The height is O(logN), so the maximum number of swaps is O(logN).

Given a max-heap with N nodes, what is the complexity for removing the root? O(N) O(logN)

O(logN) The root is replaced by a last-level node, and then downward swaps may occur, limited by the height, which is O(logN).

Returns but does not remove the item at the front of PQueue

Peek(PQueue)

Heapify operation

The heapify operation starts on the internal node with the largest index and continues down to, and including, the root node at index 0. Given a binary tree with N nodes, the largest internal node index is ⌊N/2⌋ - 1.

(b) Treap Not a treap

Treap OK that F and H have same priority, as long as heap property is not violated.

(c) Treap Not a treap

Treap OK that G's priority is same as child F. G's must be greater than or equal .

The priority queue _____ operation removes and returns the item at the front of the queue, which has the highest priority.

dequeue

The priority queue _______ operation 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.

enqueue

54 violates the max-heap property due to being greater than 44. True False

false 44 is not 54's parent, so no relation is required.

Suppose a treap is built by inserting nodes with main keys in this order: A, B, C, D, E, F, G. The treap will have 7 levels, with each level having one node with a right child. True False

false A BST would have such a structure. But upon each insert into a treap, the random priority may cause a rotate up, which forces some nodes to become left children. The tree is thus likely to be balanced.

If 2,000 customers are waiting for technical support, removing a customer from the min heap requires about 2,000 operations. True False

false A heap removal operation is worst-case O(logN). log_2(2000) is about 11, so only about 11 operations are required.

An array sorted in ascending order is already a valid max-heap. True False

false A max-heap can be used to sort an array, but a sorted array may not be valid max-heap. Ex: The array with values 10, 20, and 30 would have a root node with 2 larger children, and would not be a valid max-heap.

A treap's nodes have random main keys. True False

false A node being inserted already has a main key. The key is used to form a BST.

MaxHeapPercolateDown checks the node's left child first, and immediately swaps the nodes if the left child has a greater key. True False

false All children are checked before any child key is moved into the parent. If greater than the parent's key, the maximum child key is moved up into the parent.

The Dequeue and Peek operations both return the value in the root, and therefore have the same worst-case runtime complexity. True False

false Both functions return the value in the root, but the Dequeue function removes the value and the Peek function does not. Dequeue is worst-case O(logN) and Peek is worst-case O(1).

If items in a priority queue with a lower numerical value have higher priority, then a max-heap should be used to implement the priority queue. True False

false If the smallest numerical value is the highest priority, then the minimum value should be in the heap's root node. A min-heap has the minimum value in the root node.

Calling Heapsort on an array with 1 element will cause an out of bounds array access. True False

false In the first loop, i is initialized with -1 and the loop body is never executed. In the second loop, i is initialized with 0 and the loop body never executes.

A priority queue is always implemented using a heap. True False

false Many ways exist to implement a priority queue. Using a heap is common because priority queue operations have a simple mapping to equivalent heap operations that run efficiently.

33 violates the max-heap property due to being greater than 22. True False

false No relation between a node's two children is required.

The formula for computing a child index evaluates to -1 if the parent is a leaf node. True False

false The formula always computes non-negative child indices. If the parent is a leaf, the computed index will be outside of the heap array bounds.

The formula for computing child node indices does not work on the root node. True False

false The formulas 2 * i + 1 and 2 * i + 2 work correctly to compute child indices of 1 and 2 for the root node.

Heapsort uses recursion. True False

false The only function Heapsort calls is MaxHeapPercolateDown, which is not recursive. So, the Heapsort algorithm is not recursive.

Match each max-heap to the corresponding storage array. 57 42 19 13 6 7 15

heap a

Match each max-heap to the corresponding storage array. 57 19 42 13 6 7 15

heap b

Match each max-heap to the corresponding storage array. 57 42 15 6 19 7 13

heap c

Match each max-heap to the corresponding storage array. 57 19 42 6 7 13 15

heap d

operation used to turn an array into a heap

heapify

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.

heapsort

A ______ uses a main key that maintains a binary search tree ordering property, and a secondary key generated randomly (often called "priority") during insertions that maintains a heap property. The combination usually keeps the tree balanced.

treap

A customer with a higher priority has a lower numerical value in the min heap. True False

true

A max-heap's root always has the maximum key in the entire tree.

true

Heapsort's worst-case runtime is O(N log N). True False

true

60 violates the max-heap property due to being greater than 55. True False

true 55 is 60's parent, but 55 is not greater than or equal to 60, so a violation of the max-heap property has occurred.

When implementing a priority queue with a heap, no operation will have a runtime complexity worse than O(logN). True False

true Enqueue and dequeue operations have runtime O(logN). All other operations happen in constant time.

A max-heap's root must have the maximum key. True False

true If not, a violation of the max-heap property must exist somewhere along the path from the maximum-keyed node to the root.

MaxHeapPercolateDown has a precondition that nodeIndex is < arraySize. True False

true MaxHeapPercolateDown accesses the array at nodeIndex in the second statement, so the index must be less than the array size.

MaxHeapPercolateUp works for a node index of 0. True False

true Percolating up on the root is a valid call, although the algorithm will terminate immediately once the node index is checked in the first statement.

The formula for computing parent node index should not be used on the root node. True False

true Since the root node has no parent, the formula must not be used on the root node. The formula holds for all other nodes in the heap.

In MaxHeapPercolateUp, the while loop's condition nodeIndex > 0 guarantees that parentIndex is >= 0. True False

true The node index is an integer value, and the parent index is computed as ⌊(nodeIndex−1)/2⌋, implying that the parent index will always be >= 0.

A treap's nodes have random priorities. True False

true Upon an insert, the treap insert operation assigns the node with a random priority, then percolates the node up until the heap property is not violated.

Given N nodes, what is the height of a max-heap? ⌊logN⌋ N Depends on the keys

⌊logN⌋ The operations keep the tree height to the minimum: Each level is filled (left-to-right).


संबंधित स्टडी सेट्स

Chapter 8 - Volcanoes and Plutons

View Set

EMT - Chapter 39: incident Management, EMT - Chapter 9: Patient Assessment, EMT - Chapter 41: A Team Approach to Healthcare, EMT - Chapter 34: Pediatric Emergencies, EMT - Chapter 14: Medical Overview, EMT - Chapter 7: Life Span Development, EMT - Ch...

View Set

NUAS240T - Chapter 22 - Nursing Management of the Postpartum Woman at Risk

View Set

AP STATS 2.07 EXPLORING RELATIONSHIPS PT 2

View Set

Chapter 11- Skin, Hair, Nails Assessment

View Set