Chapter 14- heaps and treaps

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

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.

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

push- The Push function doesn't require priority as an argument, since the priority is already within the pushed object.

P is assigned a random priority of 65. To where does P percolate?

n3- 65 > 47, which violates the heap property. A right rotation is performed at R. R becomes P's right child. 89 > 65; heap property is not violated

Where will a new node H first be inserted?

n5- H < M, so descend to F. H > F, so insert H as F's right child

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

Given N nodes, what is the height of a max-heap?

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

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

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

Which array could be heapified with the fewest number of operations, including all swaps used for percolating?

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 are the last 2 elements swapped?

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

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

11- Pop removes and returns the item at the front of the queue. Item 11 is at the queue's front.

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

12, 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?

12, 21, 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?

2 * 6 + 1 = 13 and 2 * 6 + 2 = 14

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

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

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- GetLength returns the number of items in the priority queue, which contains five items: 11, 22, 33, 44, 55.

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 pop operation would return which item?

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

The array is heapified first. Each internal node is percolated down, from highest node index to lowest. The end index is initialized to 6, to refer to the last item. 94's "removal" starts by swapping with 68. Removing from a heap means that the rightmost node on the lowest level disappears before the percolate down. End index is decremented after percolating.

88 is swapped with 49, the last node disappears, and 49 is percolated down. The process continues until end index is 0. The array is sorted.

If the original array is represented in tree form, the tree is not a valid max-heap. Leaf nodes always satisfy the max heap property, since no child nodes exist that can contain larger keys. Heapification will start on node 92.

92 is greater than 24 and 42, so percolating 92 down ends immediately. Percolating 55 down results in a swap with 98. Percolating 77 down involves a swap with 98. The resulting array is a valid max-heap

A priority queue can be implemented such that each item's priority can be determined from the item itself. Ex: A customer object may contain information about a customer, including the customer's name and a service priority number. In this case, the priority resides within the object.

A priority queue may also be implemented such that all priorities are specified during a call to PushWithPriority: A push operation that includes an argument for the pushed item's priority.

Algorithms for basic treap operations include:

A treap delete can be done by setting the node's priority such that the node should be a leaf (-∞ for a max-heap), percolating the node down using rotations until the node is a leaf, and then removing the node.

Algorithms for basic treap operations include:

A treap insert initially inserts a node as in a BST using the main key, then assigns a random priority to the node, and percolates the node up until the heap property is not violated. In a heap, a node is moved up via a swap with the node's parent. In a treap, a node is moved up via a rotation at the parent. Unlike a swap, a rotation maintains the BST property.

Algorithms for basic treap operations include:

A treap search is the same as a BST search using the main key, since the treap is a BST

A BST built from inserts of N nodes having random-ordered keys stays well-balanced and thus has near-minimum height, meaning searches, inserts, and deletes are O(logN). Because insertion order may not be controllable, a data structure that somehow randomizes BST insertions is desirable.

A treap 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. The word "treap" is a mix of tree and heap. This section assumes the heap is a max-heap.

The keys maintain a BST, the priorities a heap. Insert B as a BST...

Assign random priority (70). Rotate (which keep a BST) the node up until the priorities maintain a heap: 20 not > 70: Rotate. 47 not > 70: Rotate. 80 > 70: Done.

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

Because upon completion that node will occupy another node's location (which was swapped upwards), the tree height remains the minimum possible.

Maintaining jobs in fully-sorted order requires more operations than necessary, since only the maximum item is needed. A max-heap is 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. (Actually, a max-heap may be any tree, but is commonly a binary tree).

Because x ≥ y and y ≥ z implies x ≥ z, the property results in a node's key being greater than or equal to all the node's descendants' keys. Therefore, a max-heap's root always has the maximum key in the entire tree.

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

Many companies have online technical support that lets a customer chat with a support agent. If the number of customers seeking support is greater than the number of available agents, customers enter a virtual waiting line. Each customer has a priority that determines their place in line. The customer with the highest priority is served by the next available agent. A min heap is commonly used to manage prioritized queues of customers awaiting support. Customers that entered the line earlier and/or have a more urgent issue get assigned a lower number, which corresponds to a higher priority. When an agent becomes available, the customer with the lowest number is removed from the heap and served by the agent.

A treap delete could be done by first doing a BST delete (copying the successor to the node-to-delete, then deleting the original successor), followed by percolating the node down until the heap property is not violated. However, a simpler approach just sets the node-to-delete's priority to -∞ (for a max-heap), percolates the node down until a leaf, and removes the node.

Percolating the node down uses rotations, not swaps, to maintain the BST property. Also, the node is rotated in the direction of the lower-priority child, so that the node rotated up has a higher priority than that child, to keep the heap property.

PushWithPriority calls push objects A, B, and C into the priority queue with the specified priorities. In this implementation, the objects pushed into the queue do not have data members representing priority.

Priorities specified during PushWithPriority calls are stored alongside the queue's objects.

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

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

n addition to push and pop, a priority queue usually supports peeking and length querying

Table 14.4.1: Common priority queue ADT operations----- A peek operation returns the highest priority item, without removing the item from the front of the queue.

A priority queue is commonly implemented using a heap. A heap will keep the highest priority item in the root node and allow access in O(1) time. Adding and removing items from the queue will operate in worst-case O(logN) time.

Table 14.4.2: Implementing priority queues with heaps.

Following is the pseudocode for the array-based percolate-up and percolate-down functions.

The functions operate on an array that represents a max-heap and refer to nodes by array index.

Heapsort is 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. An array of unsorted values must first be converted into a heap.

The heapify operation is used to turn an array into a heap. Since leaf nodes already satisfy the max heap property, heapifying to build a max-heap is achieved by percolating down on every non-leaf node in reverse order.

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

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

The max-heap's array form is produced by traversing levels left to right and top to bottom. When 63 is inserted, the percolate-up operation happens within the array.

The max-heap's array form is produced by traversing levels left to right and top to bottom. When 63 is inserted, the percolate-up operation happens within the array.

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?

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).

A priority queue is 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.

The priority queue push 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. The priority queue pop operation removes and returns the item at the front of the queue, which has the highest priority.

Heapsort begins by heapifying the array into a max-heap and initializing an end index value to the size of the array minus 1. Heapsort repeatedly removes the maximum value, stores that value at the end index, and decrements the end index.

The removal loop repeats until the end index is 0.

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

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

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

Heapsort uses 2 loops to sort an array. The first loop heapifies the array using MaxHeapPercolateDown.

The second loop removes the maximum value, stores that value at the end index, and decrements the end index, until the end index is 0.

An insert 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. Inserts fill a level (left-to-right) before adding another level, so the tree's height is always the minimum possible

The upward movement of a node in a max-heap is called percolating.

This tree is a max-heap. A new node gets initially inserted in the last level... ...and then percolate node up until the max-heap property isn't violated. Removing a node (always the root): Replace with last node, then percolate node down.

This tree is a max-heap. A new node gets initially inserted in the last level... ...and then percolate node up until the max-heap property isn't violated. Removing a node (always the root): Replace with last node, then percolate node down.

Pushing a single item with priority 7 initializes the priority queue with 1 item. If a lower numerical value indicates higher priority, pushing 11 adds the item to the end of the queue. Since 5 < 7, pushing 5 puts the item at the priority queue's front.

When pushing items of equal priority, the first-in-first-out rules apply. The 2nd item with priority 7 comes after the first. Popping removes from the front of the queue, which is always the highest priority item.

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 pushed

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

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 pushed?

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

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

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.

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.

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

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

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

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.

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 Pop and Peek operations both return the value in the root, and therefore have the same worst-case runtime complexity.

false- Both functions return the value in the root, but the Pop function removes the value and the Peek function does not. Pop 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.

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.

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

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.

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.

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.

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.

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

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

figure 14.3- 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.

What is the parent index for a node at index 12?

floor((12 - 1) / 2) = floor(5.5) = 5

Because heaps are not implemented with node structures and parent/child pointers, traversing from a node to parent or child nodes requires referring to nodes by index. The table below shows parent and child index formulas for a heap.

table 14.2 Parent and child indices for a heap.

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 floor(N / 2) - 1.

table 14.3 Max-heap largest internal node index.

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

ter performing the percolate down operation on internal nodes 12, 21, and 11, the resulting array is (21, 19, 15, 13, 11, 12).

MaxHeapPercolateDown has a precondition that nodeIndex is < arraySize

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

n MaxHeapPercolateUp, the while loop's condition nodeIndex > 0 guarantees that parentIndex is >= 0

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.

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

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.

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

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

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

true- MaxHeapPercolateDown's worst-case runtime is O(log N). Both loops in Heapsort iterate at most N times. O(N log N + N log N) = O(N log N).

see figures 14.2 MaxHeapPercolateUp works for a node index of 0.

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.

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

true- Priority-based systems commonly use lower numbers for higher priorities. Ex: Airlines and other businesses give first class customers priority over economy customers.

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

true- Push and pop operate have runtime O(logN). All other operations happen in constant time.

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

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.

A treap's nodes have random priorities

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.


Ensembles d'études connexes

BJU Biology, Chapter 18B: Amphibians

View Set

NURS 155 Exam 2 Success Questions Maternal Chapters 6 & 7

View Set

Chapter 16 PrepU: Outcome Identification

View Set

Prep U (COMBINED) - Chapter 20: Informatics

View Set