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.

Slides:



Advertisements
Similar presentations
Heaps & Priority Queues Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Advertisements

Source: Muangsin / Weiss1 Priority Queue (Heap) A kind of queue Dequeue gets element with the highest priority Priority is based on a comparable value.
1 HEAPS & PRIORITY QUEUES Array and Tree implementations.
ADT Table and Heap Ellen Walker CPSC 201 Data Structures Hiram College.
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 Binary Heap.
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.
Queues, Stacks and Heaps. Queue List structure using the FIFO process Nodes are removed form the front and added to the back ABDC FrontBack.
CS223 Advanced Data Structures and Algorithms 1 Priority Queue and Binary Heap Neil Tang 02/09/2010.
Data StructuresData Structures Priority Queue. Recall Queues FIFO:First-In, First-Out Some contexts where this seems right? Some contexts where some things.
Lecture 15 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
Heaps & Priority Queues
HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.
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:
Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin, and Skylight.
2 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)
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.
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:
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
"Teachers open the door, but you must enter by yourself. "
Partially Ordered Data ,Heap,Binary Heap
Heaps (8.3) CSE 2011 Winter May 2018.
Priority Queues and Heaps
Heapsort CSE 373 Data Structures.
Priority Queues © 2010 Goodrich, Tamassia Priority Queues 1
Source: Muangsin / Weiss
CSCE 3100 Data Structures and Algorithm Analysis
Bohyung Han CSE, POSTECH
Heaps 9/13/2018 3:17 PM Heaps Heaps.
Heap Sort Example Qamar Abbas.
Heaps.
7/23/2009 Many thanks to David Sun for some of the included slides!
original list {67, 33,49, 21, 25, 94} pass { } {67 94}
CMSC 341: Data Structures Priority Queues – Binary Heaps
Chapter 8 – Binary Search Tree
Priority Queues and Heaps
Part-D1 Priority Queues
Heapsort Heap & Priority Queue.
Priority Queue and Binary Heap Neil Tang 02/12/2008
A Kind of Binary Tree Usually Stored in an Array
Heaps and the Heapsort Heaps and priority queues
Tree Representation Heap.
CSCE 3110 Data Structures and Algorithm Analysis
Ch. 8 Priority Queues And Heaps
CS Data Structures Chapter 17 Heaps Mehmet H Gunes
Computer Science 2 Heaps.
"Teachers open the door, but you must enter by yourself. "
Heaps Chapter 11 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates.
C++ Plus Data Structures
Heapsort CSE 373 Data Structures.
Priority Queues & Heaps
CSCE 3110 Data Structures and Algorithm Analysis
CSE 12 – Basic Data Structures
HEAPS.
CSCE 3110 Data Structures and Algorithm Analysis
CSE 373, Copyright S. Tanimoto, 2002 Priority Queues -
Heaps By JJ Shepherd.
Hash Maps: The point of a hash map is to FIND DATA QUICKLY.
Chapter 12 Heap ADT © 2011 Pearson Addison-Wesley. All rights reserved.
Data Structures and Algorithm Analysis Priority Queues (Heaps)
Heaps & Multi-way Search Trees
Priority Queues Binary Heaps
The Heap ADT A heap is a complete binary tree where each node’s datum is greater than or equal to the data of all of the nodes in the left and right.
EE 312 Software Design and Implementation I
Heaps.
Presentation transcript:

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 is structured so that the node with the most relevant data is the root node, the next most relevant as the children of the root, etc. A heap is not a binary search tree A heap does not order its node A heap is a complete tree. Every level but the leaf level is full Binary Heap – another way to implement a priority queue!

Definition of a Binary Heap A tree is a binary heap if It is a complete tree Every level is full except the bottom level The value in the root is the largest of the tree Or smallest, if the smallest value is the most relevant and that is how you choose to structure your tree Every subtree is also a binary heap Equivalently, a complete tree is a binary heap if Node value > child value, for each child of the node Note: This use of the word “heap” is entirely different from the heap that is the allocation area in C++

Is this a Binary Heap?

Inserting an Item into a Binary Heap Insert the item in the next position across the bottom of the complete tree: preserves completeness Restore “heap-ness”: while new item is not root and is greater than its parent swap new item with its parent

Insert 80? 80 74 80 6 66 80 How many steps? At most, how many steps to insert?

Removing an Item We always remove the top (root) node! The point of a heap is to find the largest (or smallest) value in a set of numbers and that number is at the root! Remove the root Leaves a “hole”: Fill the “hole” with the last item (lower right-hand leaf) L Preserve completeness Swap L with largest child, as necessary Restore “heap-ness”

Remove? 89 80 66 66 74 66 How many steps? At most, how many steps to remove? Next: how would we implement a heap?

Implementing a Heap Yeah, yeah, we could use nodes and pointers, but… Recall: a heap is a complete binary tree If we know the number of nodes ahead of time, a complete binary tree fits nicely in an array: The root is at index 0 Children of 0 are at 1 and 2 Children of 1 are at 3 and 4 Children of 2 are at 5 and 6 Children of 3 are at 7 and 8 Children of 4 are at 9 and 10 Is there a formula for figuring out where children of a node are? Parents? Where would we insert the next node (the child of the node containing 7)? 11 11 8 7 2 5 4 1 3 6 7 8 3 8 1 7 2 2 3 5 5 4 4 5

Inserting into a Heap Insert new item at end; set child to curr_size-1 Set parent to (child – 1)/2 while (parent ≥ 0 and arr[parent] < arr[child]) Swap arr[parent] and arr[child] Set child equal to parent // so child is now (child – 1)/2 Set parent to (child – 1) / 2 How do we delete?

Deleting from a Heap Set arr[0] to arr[curr_size-1], and shrink curr_size by 1 Set parent to 0 flag = true while (flag) Set leftchild to 2 * parent + 1, and rightchild to leftchild + 1 if leftchild ≥ curr_size, flag is false else: Set maxchild to leftchild If rightchild < curr_size and arr[rightchild] > arr[leftchild] set maxchild to rightchild If arr[parent] ≥ arr[maxchild], Flag is false Swap arr[parent] and arr[maxchild]; set parent to maxchild

Performance of Heap A complete tree of height h has: Less than 2h nodes (why?) At least 2h-1 nodes Thus complete tree of n nodes has height O(log n) Insertion and deletion at most are O(log n), always Heap is useful for priority queues

Which of the following is (are) a heap? 1 2 3 4 5 6 7 8 9 42 21 58 12 31 48 64 14 29 A 1 2 3 4 5 6 7 8 9 42 41 35 39 37 24 12 38 36 B 1 2 3 4 5 6 7 8 9 thunder storm mud squirrels grass flowers dandelions petunias spring baby birds C

Try: 18’s parent is? Perform a delete 1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 2 3 4 5 6 7 8 9 10 11 12 13 14 58 45 56 20 33 50 18 19 24 41 18’s parent is? (7-1)/2, or 20 (at location 3) Perform a delete Remove 58 41 goes to position 0 Bubble 41 down 1 2 3 4 5 6 7 8 9 10 11 12 13 14 41 45 56 20 33 50 18 19 24 1 2 3 4 5 6 7 8 9 10 11 12 13 14 56 45 41 20 33 50 18 19 24 1 2 3 4 5 6 7 8 9 10 11 12 13 14 56 45 50 20 33 41 18 19 24

Heapsort Heapsort Idea: Works in place: no additional storage Insert each element into a priority queue Repeatedly remove from priority queue to array Array slots go from 0 to n-1

Heapsort Picture

Algorithm for In-Place Heapsort Build heap starting from unsorted array While the heap is not empty Remove the first item from the heap: Swap it with the last item Restore the heap property

Heapsort Analysis Insertion cost is log n for heap of size n This is O(n log n) Removal cost is also log n for heap of size n Total removal cost = O(n log n) Total cost is O(n log n)

Heapsort Picture

Example: Selection problem (Kth largest) If we had a set of unordered numbers, how would we determine the kth largest (or smallest, by reversing the process)? E.g., 11, 7, 1, 3, 8, 4, 9, 2, 6, 10, 5 What if we wanted the 5th largest element? How would we do this? How long would it take?

Can we do better? Of course. 11, 7, 1, 3, 8, 4, 9, 2, 6, 10, 5 (for this, pretend as if we’re looking for the kth smallest element) 1. build a heap with the array with the smallest value at the top. 2. delete k elements from the heap. Running time? 11 10 5 3 8 2 4 8 1 1 2 3 4 10 11 2 5 6 8 3 8 4 7 11 3 6 8 10 5 7 8 9 11 6 8 10 8 So 5 is the 5th smallest element in the list. Can we do better than this???? Note: Could we use this for sorting? What would be the timing?

Better: 11, 7, 1, 3, 8, 4, 9, 2, 6, 10, 5 build a heap (largest elements at top) with just the first k elements. 11 4 2 6 5 8 7 7 2 4 4 8 1 3 7 4 2 2. Compare rest of elements with heap. If the new element is smaller than the root, we insert the new element and remove the root. 3. Otherwise we ignore. Note: we are finding the kth smallest element. To find the kth largest element, we would make a heap with the smallest number as the root, and ascend as we move down. Then new elements would be inserted if they were larger than the root.