Chapter 21 Binary Heap.

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)
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,
Binary Heaps CSE 373 Data Structures Lecture 11. 2/5/03Binary Heaps - Lecture 112 Readings Reading ›Sections
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.
More sorting algorithms: Heap sort & Radix sort. Heap Data Structure and Heap Sort (Chapter 7.6)
Heaps and heapsort COMP171 Fall Sorting III / Slide 2 Motivating Example 3 jobs have been submitted to a printer in the order A, B, C. Sizes: Job.
© 2004 Goodrich, Tamassia Priority Queues1 Heaps: Tree-based Implementation of a Priority Queue.
Version TCSS 342, Winter 2006 Lecture Notes Priority Queues Heaps.
© 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.
Heaps and heapsort COMP171 Fall 2005 Part 2. Sorting III / Slide 2 Heap: array implementation Is it a good idea to store arbitrary.
1 Priority Queues CPS212 Gordon College VIP. 2 Introduction to STL Priority Queues Adaptor container - underlying container may be either: – a template.
1 Chapter 8 Priority Queues. 2 Implementations Heaps Priority queues and heaps Vector based implementation of heaps Skew heaps Outline.
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.
Heapsort Based off slides by: David Matuszek
Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07.
PRIORITY QUEUES (HEAPS). Queues are a standard mechanism for ordering tasks on a first-come, first-served basis However, some tasks may be more important.
Binary Heap.
The Binary Heap. Binary Heap Looks similar to a binary search tree BUT all the values stored in the subtree rooted at a node are greater than or equal.
Data Structure & Algorithm II.  Delete-min  Building a heap in O(n) time  Heap Sort.
Chapter 21 Priority Queue: Binary Heap Saurav Karmakar.
CSC 213 – Large Scale Programming Lecture 15: Heap-based Priority Queue.
PRIORITY QUEUES AND HEAPS CS16: Introduction to Data Structures & Algorithms Tuesday, February 24,
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.
Priority Queues and Heaps. Outline and Reading PriorityQueue ADT (§8.1) Total order relation (§8.1.1) Comparator ADT (§8.1.2) Sorting with a Priority.
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.
8 January Heap Sort CSE 2011 Winter Heap Sort Consider a priority queue with n items implemented by means of a heap  the space used is.
Lecture 15 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
CS 2468: Assignment 2 (Due Week 9, Tuesday. Drop a hard copy in Mail Box 75 or hand in during the lecture) Use array representation (double a[]) to implement.
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.
FALL 2005CENG 213 Data Structures1 Priority Queues (Heaps) Reference: Chapter 7.
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:
Mergeable Heaps David Kauchak cs302 Spring Admin Homework 7?
Heaps © 2010 Goodrich, Tamassia. Heaps2 Priority Queue ADT  A priority queue (PQ) stores a collection of entries  Typically, an entry is a.
Heaps, Heap Sort, and Priority Queues. Background: Binary Trees * Has a root at the topmost level * Each node has zero, one or two children * A node that.
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:
Priority Queues A priority queue is an ADT where:
"Teachers open the door, but you must enter by yourself. "
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,
Heaps © 2010 Goodrich, Tamassia Heaps Heaps
Bohyung Han CSE, POSTECH
Heaps 9/13/2018 3:17 PM Heaps Heaps.
The Heap Data Structure
Heaps and Priority Queues
Priority Queues and Heaps
Part-D1 Priority Queues
Heaps and Priority Queues
Heaps 11/27/ :05 PM Heaps Heaps.
Heaps and the Heapsort Heaps and priority queues
Tree Representation Heap.
Heaps A heap is a binary tree.
Heaps and Priority Queues
© 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
"Teachers open the door, but you must enter by yourself. "
Heaps © 2014 Goodrich, Tamassia, Goldwasser Heaps Heaps
Heaps and Priority Queues
Priority Queues (Heaps)
Heaps 9/29/2019 5:43 PM Heaps Heaps.
CS210- Lecture 13 June 28, 2005 Agenda Heaps Complete Binary Tree
Presentation transcript:

Chapter 21 Binary Heap

Objective To learn Basic properties of the binary heap Insert and deletemin operations Linear time heap-construction algorithm Heapsort

Topics Review and analyze operations on Heap data structure Review definitions Tree, binary tree depth height full binary tree complete binary tree Heap property

Binary trees A tree is an acyclic, connected, undirected graph. Only one path exists between a pair of nodes A leaf of a tree is a node with no children. Binary tree - a tree where each node has 0,1 or 2 children

Depth and height of a tree Depth of a node is: Depth of the root of a tree is 0. The depth of its parent +1 Depth of a tree is maximum depth of its leaves. Height of a node is: Height of a leaf of a tree is 0. The maximum height of its children +1 Height of a tree is the height of the root.

Depth and height of a binary tree 2 1 1 1 2 The depth of the tree = height of the tree = 2

A complete binary tree A full binary tree (also called complete) is a binary tree such that All internal nodes have 2 children All leaves have depth d A complete binary tree (also called essentially complete) is a binary tree such that All internal nodes have 2 children except the last internal node which may have only 1 child. All leaves have depth d or d -1 Nodes with depth d are as far to the left as possible.

Full binary tree Complete binary tree 2h n  2h+1 -1 n = 2h+1 -1

The height of a complete binary tree The number of nodes n of a complete binary tree satisfies: 2h  n  (2h+1-1) Taking the log base 2 we get: h  lg n and lg(n+1)  h+1 or lg(n+1)-1  h  lg n Since h is integer h = lg(n + 1)-1=  lg n 

The height of a complete binary tree 2h  n  (2h+1-1) Complete binary tree with 1 node at depth h Full binary tree all leaves same depth

Heap Definition A heap is a complete binary tree that satisfies the heap property. Minimum Heap Property: The value stored at each node is less than or equal to the values stored at its children. OR Maximum Heap Property: greater

Implementation of Heap For simplicity we assume the complete binary tree is an array, and the root is stored at index 1. For any element in array position i, its left child is at position 2i, and it’s right child is at 2i+1, its parent is at i/2 .

Last node in a heap The last node of a heap is the rightmost internal node of on the last level 2 5 6 9 7 last node

Heap viewed as Binary tree implemented as an array. 3 2 8 7 18 14 9 29 6 1 4 5 10 last 1 3 2 8 7 29 6 4 5 9 18 14 10

insert(v ) Item inserted as last item in the heap 2 Item inserted as last item in the heap Heap property may be violated Do percolate to restore heap property 5 6 z 9 7 insertion node 2 5 6 z 9 7 1

Percolate up After the insertion of a new key k, the heap-order property may be violated Algorithm percolate up restores the heap-order property by swapping k along an upward path from the insertion node percolate up terminates when the key k reaches the root or a node whose parent has a key smaller than or equal to k Since a heap has height O(log n), upheap runs in O(log n) time 2 1 5 1 5 2 z z 9 7 6 9 7 6

deleteMin() Save root object O(1) 10 10 2 3 30 20 4 Save root object O(1) Remove last element and store in root O(1) siftDown(1) 80 1 last 80 2 3 30 20 1 20 2 3 30 80

4 3 8 17 12 14 19 6 13 1 siftDown(1) New value at root. 5 7 9 10 siftDown(1) New value at root. Right Child is smaller Exchange root and right child Satisfy the Heap property.

4 9 8 17 12 14 19 6 13 1 3 2 Parent Left Child is smaller 5 7 10 Parent Left Child is smaller Exchange parent and left child

The worst case run time to do siftDown(index) is 4 6 8 17 12 14 19 9 13 1 2 3 5 7 10 The worst case run time to do siftDown(index) is O(h(index)) where h(index) is the height of node index When index=root=1, O(lg n)

Build heap---linear time 1. for i ¬ (last /2) downto 1 2. do siftDown( i ) Why we can start siftDown at last/2 ? because we : need to siftDown only parents the rest of the nodes are leaves and leaves satisfy the heap property

siftDown makes it a min heap 5 8 12 9 7 10 21 1 2 3 4 6 14 8 12 9 7 6 14 4 10 21 1 2 3 5 siftDown makes it a min heap

siftDown makes this into heap 8 12 9 4 6 14 7 10 21 1 2 3 5 this is a heap siftDown makes this into heap

8 12 6 4 9 14 7 10 21 1 2 3 5 i = 3 siftDown makes heap These are heaps

1 10 12 21 3 6 7 5 8 4 9 14 2 i = 2 4 6 7 9 14 8 2 5 10 4 6 8 9 14 7 2 5 10 siftDown

4 10 6 7 9 14 8 12 21 1 2 3 5 i = 1 10 6 7 9 14 8 12 21 1 2 3 4 5 5

Running time n/2 nodes For each node, at most O(log(n)) The running time is O(nlgn)

Heap-Sort Using a heap-based priority queue, we can sort a sequence of n elements in O(n log n) time The resulting algorithm is called heap-sort Heap-sort is much faster than quadratic sorting algorithms, such as insertion-sort

97 53 59 58 31 26 41 16 97 53 59 26 41 58 1 2 3 4 5 31 16 21 36 36 21 6 7 8 9 10 59 53 58 36 31 26 41 16 21 97 59 53 58 26 41 36 1 2 3 4 5 31 16 21 97 6 7 8 9 10

HeapSort //build heap for i ¬ (last /2) downto 1 do siftDown( a, i, last) //shiftDown from i to last for j ¬ last-1 downto 1 swap(a[0], a[j]) //deleteMax do siftDown(a, 0, j) // shiftDown from 0 to j

Common errors (from book, page 786) For heapsort data begins in position 0, so the children of the I are in positions 2i+1 and 2i+2. STL priority queue is a max heap, not a min heap. More errors are defined in the book.

In class exercises 21.1 Describe the structure and ordering properties of the binary heap 21.2 In a binary heap, for an item in position I where are the parent, left child, and right child located? 21.3 Show the result of inserting 10, 12, 1, 14, 6, 5, 8, 15, 3, 9, 7, 4, 11, 13, and 2, one at a time, in an initially empty heap. Then show the result of using the linear-time buildHep algorithm instead