Presentation is loading. Please wait.

Presentation is loading. Please wait.

Bohyung Han CSE, POSTECH

Similar presentations


Presentation on theme: "Bohyung Han CSE, POSTECH"— Presentation transcript:

1 Bohyung Han CSE, POSTECH bhhan@postech.ac.kr
Lecture15: Heaps CSED233: Data Structures (2014F) Bohyung Han CSE, POSTECH

2 Priority Queues Queues Priority queues
Stores items (keys) in a linear list or array FIFO (First‐In‐First‐Out) Stored items do not have priorities. Priority queues Stores items (keys) with priorities The definition of the priority depends on the application. We assume a larger value for the key has higher priority in this class by default. The opposite case is specified separately. Has the following operations: insert: Add a new item with a given priority. DeleteMax (or DeleteMin): Remove an item with highest priority

3 Priority Queue ADT A priority queue stores entries in order
Each entry is a pair (key, value) Main methods of the Priority Queue ADT insert(k,x) inserts an entry with key k and value x deleteMax() or deleteMin() removes and returns the entry with the largest (smallest) key max() or min() returns, but does not remove, an entry with the largest (smallest) key size() isEmpty()

4 Sequence‐based Priority Queue
Implementation with an unsorted list insert: 𝑂 1 since we can insert the item at the beginning or end of the sequence removeMax and max: 𝑂 𝑛 since we have to traverse the entire sequence to find the largest key Implementation with a sorted list insert: 𝑂 𝑛 since we have to find the place where to insert the item removeMax and max: 𝑂 1 since the largest key is at the beginning 4 5 2 3 1 5 4 3 2 1

5 Heap A binary tree storing keys at its nodes
Satisfy the following properties: Complete binary tree: Tree is full to level ℎ−1 and level ℎ is filled from the left with contiguous nodes. Heap ordered: for every internal node 𝑣 other than the root, Min Heap 𝑘𝑒𝑦 𝑣 ≤𝑘𝑒𝑦 𝑝𝑎𝑟𝑒𝑛𝑡 𝑣 or 𝑘𝑒𝑦 𝑣 ≥𝑘𝑒𝑦 𝑝𝑎𝑟𝑒𝑛𝑡 𝑣 Max Heap 100 19 36 17 3 25 18 2 7 1

6 Properties of Heap The last node of a heap Height of a heap
The rightmost node of maximum depth Height of a heap A heap storing 𝑛 keys has height 𝑂 log 𝑛 2 𝑖 keys at depth 𝑖=0,…,ℎ−1 2 5 6 9 7 last node depth keys 1 1 2 h-1 2h-1 h 1

7 Heap Implementation Representation of heap
All you need to implement a heap is an array heap of items and the number of items stored in the heap (heapSize). The items are stored in positions 0 to heapSize‐1 in the array. No child pointers are needed. The root is always stored in heap[0], if the tree is not empty. The left child of heap[i] is heap[2i+1]. The right child of heap[i] is heap[2i+2]. The parent of heap[i] is heap[floor((i‐1)/2)].

8 Example of a (Max) Heap 36 17 3 25 18 2 7 1 100 19 heapSize=10 100 19

9 Insertion Insert a new element at the end of the array 36 17 3 25 18 2
100 19 36 17 3 25 18 2 7 1 80 heapSize=11 100 19 36 17 3 25 18 2 7 1 80

10 Insertion Shift up the element if the heap‐order property is violated.
100 19 36 17 80 25 18 2 7 1 3 heapSize=11 100 19 36 17 80 25 18 2 7 1 3

11 Insertion Shift up the element if the heap‐order property is violated.
100 80 36 17 19 25 18 2 7 1 3 heapSize=11 100 80 36 17 19 25 18 2 7 1 3

12 Insertion After the insertion of a new key Time complexity
The heap‐order property may be violated. The heap‐order property should be restored by swapping the new node with its parent. This procedure continues until the key reaches the root or a node whose parent has a key smaller than or equal to the key. Time complexity A heap has height: 𝑂 log 𝑛 Insertion of a heap runs in 𝑂 log 𝑛 time. 9 7 ? 1 𝑧 2 3 6 9 ? 7 6 𝑧 2 3 1

13 Insertion Example Insert 15, 5, 10, 30, 20, 0, 35, and 25 into an empty heap. 15 5 10 30 heapSize=4 15 5 10 30

14 Insertion Example Insert 15, 5, 10, 30, 20, 0, 35, and 25 into an empty heap. 30 15 10 5 heapSize=4 30 15 10 5

15 Insertion Example Insert 15, 5, 10, 30, 20, 0, 35, and 25 into an empty heap. 30 15 10 5 20 heapSize=5 30 15 10 5 20

16 Insertion Example Insert 15, 5, 10, 30, 20, 0, 35, and 25 into an empty heap. 30 20 10 5 15 heapSize=5 30 20 10 5 15

17 Insertion Example Insert 15, 5, 10, 30, 20, 0, 35, and 25 into an empty heap. 30 20 10 5 15 heapSize=6 30 20 10 5 15

18 Insertion Example Insert 15, 5, 10, 30, 20, 0, 35, and 25 into an empty heap. 30 20 10 5 15 35 heapSize=7 30 20 10 5 15 35

19 Insertion Example Insert 15, 5, 10, 30, 20, 0, 35, and 25 into an empty heap. 35 20 30 5 15 10 heapSize=7 35 20 30 5 15 10

20 Insertion Example Insert 15, 5, 10, 30, 20, 0, 35, and 25 into an empty heap. 35 20 30 5 15 10 25 heapSize=8 35 20 30 5 15 10 25

21 Insertion Example Insert 15, 5, 10, 30, 20, 0, 35, and 25 into an empty heap. 35 25 30 20 15 10 5 heapSize=8 35 25 30 20 15 10 5

22 Implementation of Insertion
public void insert(int priority) { int i; if (!isFull()) heap[heapSize] = priority; i = heapSize++; while ((i > 0) && (heap[(i‐1)/2] < heap[i])) swap(heap[i], heap[(i‐1)/2]); i = (i‐1) / 2; } // end while } // end if } // end insert

23 Deletion Delete the node with the highest priority (root) 36 17 19 25
100 80 36 17 19 25 18 2 7 1 3 heapSize=11 100 80 36 17 19 25 18 2 7 1 3

24 Deletion Replace the root with the last node 36 17 19 25 18 2 7 1 3 80
heapSize=10 3 80 36 17 19 25 18 2 7 1

25 Deletion Restore heap order in the heap by shifting down
Repeatedly swap the node with the larger one of its children 80 19 36 17 3 25 18 2 7 1 heapSize=10 80 19 36 17 3 25 18 2 7 1

26 Delete Max Removal of the root from the heap
The removal algorithm consists of three steps Replace the root key with the key of the last node 𝑤 Remove 𝑤 Restore the heap‐order property Shift‐down swapping key 𝑘 along a downward path from the root Find the maximum child 𝑐 Swap 𝑐 and 𝑘 if 𝑐>𝑘 Time complexity: 𝑂 log 𝑛 9 7 6 𝑤 2 5 last node 5 7 6 𝑤 2 7 5 6 𝑤 2 new last node

27 Implementation of Deletion
public int deleteMax() { int max; // Highest priority to return. int i; // Current position in heap. if (!isEmpty()) max = heap[0]; heap[0] = heap[‐‐heapSize]; i = 0; while (((2*i+2) < heapSize) && ((heap[i] < heap[2*i+1]) || (heap[i] < heap[2*i+2]))) Exchange heap[i] with its larger child. if (((2*i+1) < heapSize) && (heap[i] < heap[2*i+1])) Exchange heap[i] with its left child. } return max; } // end deleteMax

28


Download ppt "Bohyung Han CSE, POSTECH"

Similar presentations


Ads by Google