Bohyung Han CSE, POSTECH

Slides:



Advertisements
Similar presentations
COL 106 Shweta Agrawal and Amit Kumar
Advertisements

Heaps1 Part-D2 Heaps Heaps2 Recall Priority Queue ADT (§ 7.1.3) A priority queue stores a collection of entries Each entry is a pair (key, value)
Lecture16: Heap Sort Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
Data Structures Lecture 7 Fang Yu Department of Management Information Systems National Chengchi University Fall 2010.
The Priority Queue Abstract Data Type. Heaps. Adaptable Priority Queue. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich,
Priority Queues. Container of elements where each element has an associated key A key is an attribute that can identify rank or weight of an element Examples.
© 2004 Goodrich, Tamassia Priority Queues1 Heaps: Tree-based Implementation of a Priority Queue.
© 2004 Goodrich, Tamassia Heaps © 2004 Goodrich, Tamassia Heaps2 Priority Queue Sorting (§ 8.1.4) We can use a priority queue to sort a set.
Priority Queues1 Part-D1 Priority Queues. Priority Queues2 Priority Queue ADT (§ 7.1.3) A priority queue stores a collection of entries Each entry is.
1 Priority Queues CPS212 Gordon College VIP. 2 Introduction to STL Priority Queues Adaptor container - underlying container may be either: – a template.
Heaps and Priority Queues Priority Queue ADT (§ 2.4.1) A priority queue stores a collection of items An item is a pair (key, element) Main.
CSC 172 DATA STRUCTURES. Priority Queues Model Set with priorities associatedwith elements Priorities are comparable by a < operator Operations Insert.
Chapter 21 Binary Heap.
1 Heaps A heap is a binary tree. A heap is best implemented in sequential representation (using an array). Two important uses of heaps are: –(i) efficient.
Chapter 2.4: Priority Queues and Heaps PriorityQueue ADT (§2.4.1) Total order relation (§2.4.1) Comparator ADT (§2.4.1) Sorting with a priority queue (§2.4.2)
Chapter 2: Basic Data Structures. Spring 2003CS 3152 Basic Data Structures Stacks Queues Vectors, Linked Lists Trees (Including Balanced Trees) Priority.
CPSC 252 Binary Heaps Page 1 Binary Heaps A complete binary tree is a binary tree that satisfies the following properties: - every level, except possibly.
1 Heaps A heap is a binary tree. A heap is best implemented in sequential representation (using an array). Two important uses of heaps are: –(i) efficient.
1 Heap Sort. A Heap is a Binary Tree Height of tree = longest path from root to leaf =  (lgn) A heap is a binary tree satisfying the heap condition:
Quotes “From each according to his ability, to each according to his needs” -- Karl Marx/Queue ADT “In America, first you get the sugar, then you get the.
CS 367 Introduction to Data Structures Lecture 8.
Priority Queues CS 110: Data Structures and Algorithms First Semester,
Heaps © 2010 Goodrich, Tamassia. Heaps2 Priority Queue ADT  A priority queue (PQ) stores a collection of entries  Typically, an entry is a.
1 Priority Queues (Heaps). 2 Priority Queues Many applications require that we process records with keys in order, but not necessarily in full sorted.
Heaps and Priority Queues What is a heap? A heap is a binary tree storing keys at its internal nodes and satisfying the following properties:
Heaps and Heap Sort. Sorting III / Slide 2 Background: Complete Binary Trees * A complete binary tree is the tree n Where a node can have 0 (for the leaves)
Sorting With Priority Queue In-place Extra O(N) space
Partially Ordered Data ,Heap,Binary Heap
Heaps (8.3) CSE 2011 Winter May 2018.
Priority Queues © 2010 Goodrich, Tamassia Priority Queues 1
Heaps 8/2/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser,
Source: Muangsin / Weiss
Heaps © 2010 Goodrich, Tamassia Heaps Heaps
CSCE 3100 Data Structures and Algorithm Analysis
Heaps 9/13/2018 3:17 PM Heaps Heaps.
Heaps.
Chapter 2: Basic Data Structures
ADT Heap data structure
7/23/2009 Many thanks to David Sun for some of the included slides!
Heaps and Priority Queues
Priority Queues and Heaps
Part-D1 Priority Queues
Heaps and Priority Queues
CMSC 341 Lecture 14 Priority Queues & Heaps
Heaps 11/27/ :05 PM Heaps Heaps.
B-Tree Insertions, Intro to Heaps
Binary Tree Application Operations in Heaps
Tree Representation Heap.
Heaps A heap is a binary tree.
Heaps and Priority Queues
CSCE 3110 Data Structures and Algorithm Analysis
© 2013 Goodrich, Tamassia, Goldwasser
Heaps 12/4/2018 5:27 AM Heaps /4/2018 5:27 AM Heaps.
Ch. 8 Priority Queues And Heaps
Heap Sort The Heap Data Structure
Heaps and Priority Queues
Heaps © 2014 Goodrich, Tamassia, Goldwasser Heaps Heaps
Priority Queues & Heaps
CSCE 3110 Data Structures and Algorithm Analysis
Lecture 9 CS2013.
Data Structures Lecture 29 Sohail Aslam.
Heaps and Priority Queues
CSCE 3110 Data Structures and Algorithm Analysis
1 Lecture 10 CS2013.
CSE 373, Copyright S. Tanimoto, 2002 Priority Queues -
Priority Queue and Heap
CS210- Lecture 14 July 5, 2005 Agenda Inserting into Heap
Heaps 9/29/2019 5:43 PM Heaps Heaps.
Heaps.
CS210- Lecture 13 June 28, 2005 Agenda Heaps Complete Binary Tree
Presentation transcript:

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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