7/23/2009 Many thanks to David Sun for some of the included slides!

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)
Maps A map is an object that maps keys to values Each key can map to at most one value, and a map cannot contain duplicate keys KeyValue Map Examples Dictionaries:
Heapsort Based off slides by: David Matuszek
1 HEAPS & PRIORITY QUEUES Array and Tree implementations.
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.
Priority Queues and Binary Heaps Chapter Trees Some animals are more equal than others A queue is a FIFO data structure the first element.
Chapter 21 Priority Queue: Binary Heap Saurav Karmakar.
Priority Queues and Heaps. October 2004John Edgar2  A queue should implement at least the first two of these operations:  insert – insert item at the.
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.
Lecture 8 : Priority Queue Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University.
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:
CS 367 Introduction to Data Structures Lecture 8.
Priority Queues and Heaps. John Edgar  Define the ADT priority queue  Define the partially ordered property  Define a heap  Implement a heap using.
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:
Sorting With Priority Queue In-place Extra O(N) space
Priority Queues A priority queue is an ADT where:
"Teachers open the door, but you must enter by yourself. "
CSE373: Data Structures & Algorithms Priority Queues
CSE373: Data Structures & Algorithms
Chapter 11 Heap.
Heaps (8.3) CSE 2011 Winter May 2018.
Heaps, Heapsort, and Priority Queues
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
Bohyung Han CSE, POSTECH
Heaps 9/13/2018 3:17 PM Heaps Heaps.
Heap Sort Example Qamar Abbas.
Heapsort.
Heaps.
ADT Heap data structure
original list {67, 33,49, 21, 25, 94} pass { } {67 94}
Part-D1 Priority Queues
Draw pictures to indicate the subproblems middleMax solves at each level and the resulting maxPtr and PrevPtr for each on this linked list:
CMSC 341 Lecture 14 Priority Queues & Heaps
Heaps 11/27/ :05 PM Heaps Heaps.
B-Tree Insertions, Intro to Heaps
Tree Representation Heap.
© 2013 Goodrich, Tamassia, Goldwasser
Heaps 12/4/2018 5:27 AM Heaps /4/2018 5:27 AM Heaps.
Ch. 8 Priority Queues And Heaps
Hassan Khosravi / Geoffrey Tien
Computer Science 2 Heaps.
8/04/2009 Many thanks to David Sun for some of the included slides!
"Teachers open the door, but you must enter by yourself. "
Heap Sort CSE 2011 Winter January 2019.
Binary Heaps What if we’re mostly concerned with finding the most relevant data? A binary heap is a binary tree (2 or fewer subtrees for each node) A heap.
Heapsort.
Binary Search Trees 7/16/2009.
Heaps © 2014 Goodrich, Tamassia, Goldwasser Heaps Heaps
Priority Queues & Heaps
Data Structures Lecture 29 Sohail Aslam.
Topic 5: Heap data structure heap sort Priority queue
HEAPS.
Heapsort.
CSE 373 Priority queue implementation; Intro to heaps
Heapsort.
Hash Maps: The point of a hash map is to FIND DATA QUICKLY.
Heapsort.
Data Structures and Algorithm Analysis Priority Queues (Heaps)
Computer Algorithms CISC4080 CIS, Fordham Univ.
Priority Queue and Heap
Heaps & Multi-way Search Trees
Instructor: Dr. Michael Geiger Spring 2017 Lecture 30: Sorting & heaps
Heaps 9/29/2019 5:43 PM Heaps Heaps.
Heaps.
CS210- Lecture 13 June 28, 2005 Agenda Heaps Complete Binary Tree
CMPT 225 Lecture 16 – Heap Sort.
Presentation transcript:

7/23/2009 Many thanks to David Sun for some of the included slides! Queues, Priority Queues and Heaps 7/23/2009 Many thanks to David Sun for some of the included slides!

Important Dates Project 2 due Midterm Review Midterm 2 DESIGN, DESIGN, DESIGN!!! You may have 0 or 1 partner. NO EXCEPTIONS! Due Friday 7/24/2009 – 10pm Midterm Review Saturday 7/26/2009 – 1-4pm in 306 Soda Midterm 2 Tuesday 7/28/2009 – 5-6pm in 10 Evans Important Dates

Queues (Review)

Queues (Review)

What data structure would you use to store a queue? Hash Table Binary Search Tree Linked List Array

Using a Hash Table to implement a Queue (not a “good” idea) Key is the # in line Value is the object in the queue Keep track of the current number

Regular Queues with an Array Start End 6 3 2 8 9 7 Insert 5 Start End 6 3 2 8 9 7 5

Regular Queues with an Array Start End 6 3 2 8 9 7 5 Remove First Start End 3 2 8 9 7 5

An Empty Queue Start End

A Queue Based upon Priority PriorityQueue

PriorityQueues Implemented with Sorted Arrays Operation Run Time Other Requirements size() O(1) Keep size var. isEmpty() Call size() == 0 insert(…) O(n)  max() removeMax()

PriorityQueues Implemented with Non-Sorted Arrays Operation Run Time Other Requirements size() O(1) Keep size var. isEmpty() Call size() == 0 insert(…) O(1)* max() O(n) removeMax()

This is a Max Heap (And Heaps are AWESOME!)

Binary Heaps A Binary Heap is a binary tree, with two additional properties Shape Property: It is a complete binary tree – a binary tree in which every row is full, except possibly the bottom row, which is filled from left to right. Heap Property (or Heap Order Property): No child has a key greater than its parent's key. This property is applied recursively: any subtree of a binary heap is also a binary heap. If we use the notion of smaller than in the Heap Property we get a min-heap. We’ll look at max-heap in this lecture.

Heaps Implement PriorityQueue

max() Trivial: The heap-order property ensures that the entry with the maximum key is always at the top of the heap. Hence, we simply return the entry at the root node. If the heap is empty, return null or throw an exception. Runs in O(1) time.

insert (8)

insert(8) Let x be the new entry (k, v). Place the new entry x in the bottom level of the tree, at the first free spot from the left. If the bottom level is full, start a new level with x at the far left. If the new entry's key violates the heap-order property then compare x's key with its parent's key; if x's key is larger, we exchange x with its paren. Repeat the procedure with x's new parent. Original Inserting <8,v> Dashed boxes show where the heap property violated Re-heapify up

We might have to bubble it all the way up! insert(18)

insert(18) Inserting <18,v> What’s the time complexity of insert?

Remove Max

removeMax() If the heap is empty, return null or throw an exception. Otherwise, remove the entry at the root node. Replace the root with the last entry in the tree x, so that the tree is still complete. If the root violates the heap property then compare x with it’s children, swap x with the child with the larger key, repeat until x is greater than or equal to its children or reach a leaf. Starting Replace root Re-heapify down Final

There are tons of Heap Animations Heapify http://students.ceid.upatras.gr/~perisian/data_structure/HeapSort/heap_applet.html

Representing Trees (Review) Array representations are common for Heaps This is not a heap! Left Child at 2n + 1 Right Child at 2n + 2 10 5 15 3 7 13 18 2 1 2 3 4 5 6 7 8 9

A Different Tree Implementation (don’t use the 0 index) This is not a heap! Left Child at 2n Right Child at 2n + 1 10 5 15 3 7 18 1 2 3 4 5 6 7 8 9 10

Remove Max

removeMax() Starting Replace root Re-heapify down Final

Bottom-Up Heap Construction Suppose we are given a bunch of randomly ordered entries, and want to make a heap out of them. What’s the obvious way Apply insert to each item in O(n log n) time. A better way: bottomUpHeap() Make a complete tree out of the entries, in any random order. Start from the last internal node (non-leaf node), in reverse order of the level order traversal, heapify down the heap as in removeMax().

Example 7 4 9 5 3 1 7 4 9 5 3 1 Why this works? We can argue inductively: The leaf nodes satisfy the heap order property vacuously (they have no children). Before we bubble an entry down, we know that its two child subtrees must be heaps. Hence, by bubbling the entry down, we create a larger heap rooted at the node where that entry started. 7 5 9 4 3 1 7 4 9 5 3 1 9 5 7 4 3 1

Cost of Bottom Up Construction If each internal node bubbles all the way down, then the running time is proportional to the sum of the heights of all the nodes in the tree. Turns out this sum is less than n, where n is the number of entries being coalesced into a heap. Hence, the running time is in O(n), which is better than inserting n entries into a heap individually.

Heap Sort Demo…

Running Times (Appendix) We could use a list or array, sorted or unsorted, to implement a priority queue. The following table shows running times for different implementations, with n entries in the queue. Binary Heap Sorted List/Array Unsorted List/Array max Theta(1) Theta(n) insert (worst-cast) Theta(log n)* Theta(1)* insert(best-case) it depends removeMax (worst) Theta(log n) removeMax (best) * If you are using an array-based data structure, these running times assume that you don’t run of room. If you do, it will take Omega(n) time to allocate a larger array and copy them into it.