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)

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)
Trees Types and Operations
CMPT 225 Priority Queues and Heaps. Priority Queues Items in a priority queue have a priority The priority is usually numerical value Could be lowest.
1 Chapter 6 Priority Queues (Heaps) General ideas of priority queues (Insert & DeleteMin) Efficient implementation of priority queue Uses of priority queues.
Data Structures and Algorithms1 B-Trees with Minimum=1 2-3 Trees.
CS 315 March 24 Goals: Heap (Chapter 6) priority queue definition of a heap Algorithms for Insert DeleteMin percolate-down Build-heap.
Fall 2007CS 2251 Trees Chapter 8. Fall 2007CS 2252 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information.
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.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 19: Heap Sort.
CS 261 – Data Structures Priority Queues & Heaps.
Chapter 6: Priority Queues Priority Queues Binary Heaps Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College.
Heaps & Priority Queues Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
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.
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 Chapter 8 Priority Queues. 2 Implementations Heaps Priority queues and heaps Vector based implementation of heaps Skew heaps Outline.
1 HEAPS & PRIORITY QUEUES Array and Tree implementations.
B-trees (Balanced Trees) A B-tree is a special kind of tree, similar to a binary tree. However, It is not a binary search tree. It is not a binary tree.
Computer Science and Software Engineering University of Wisconsin - Platteville 12. Heap Yan Shi CS/SE 2630 Lecture Notes Partially adopted from C++ Plus.
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.
1 COP 3538 Data Structures with OOP Chapter 8 - Part 2 Binary Trees.
CS-2852 Data Structures LECTURE 13A Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
ADT Table and Heap Ellen Walker CPSC 201 Data Structures Hiram College.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
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.
Priority Queues and Binary Heaps Chapter Trees Some animals are more equal than others A queue is a FIFO data structure the first element.
Data Structure & Algorithm II.  Delete-min  Building a heap in O(n) time  Heap Sort.
Chapter 21 Binary Heap.
Data Structures Week 8 Further Data Structures The story so far  Saw some fundamental operations as well as advanced operations on arrays, stacks, and.
P p Chapter 10 has several programming projects, including a project that uses heaps. p p This presentation shows you what a heap is, and demonstrates.
Binary Search Tree vs. Balanced Search Tree. Why care about advanced implementations? Same entries, different insertion sequence: 10,20,30,40,50,60,70,
Data Structure & Algorithm II.  In a multiuser computer system, multiple users submit jobs to run on a single processor.  We assume that the time required.
Preview  Graph  Tree Binary Tree Binary Search Tree Binary Search Tree Property Binary Search Tree functions  In-order walk  Pre-order walk  Post-order.
PRIORITY QUEUES AND HEAPS CS16: Introduction to Data Structures & Algorithms Tuesday, February 24,
1 Joe Meehean.  We wanted a data structure that gave us... the smallest item then the next smallest then the next and so on…  This ADT is called a priority.
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
Chapter 12 Heaps & HeapSort © John Urrutia 2014, All Rights Reserved1.
Chapter 13 Priority Queues. 2 Priority queue A stack is first in, last out A queue is first in, first out A priority queue is least-in-first-out The “smallest”
HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.
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:
AVL Trees and Heaps. AVL Trees So far balancing the tree was done globally Basically every node was involved in the balance operation Tree balancing can.
CS 367 Introduction to Data Structures Lecture 8.
Course: Programming II - Abstract Data Types HeapsSlide Number 1 The ADT Heap So far we have seen the following sorting types : 1) Linked List sort by.
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.
Priority Queues and Heaps. John Edgar  Define the ADT priority queue  Define the partially ordered property  Define a heap  Implement a heap using.
Sorting With Priority Queue In-place Extra O(N) space
Partially Ordered Data ,Heap,Binary Heap
Heaps (8.3) CSE 2011 Winter May 2018.
Source: Muangsin / Weiss
CSCE 3100 Data Structures and Algorithm Analysis
Data Structures and Algorithms
Chapter 8 – Binary Search Tree
Priority Queue and Binary Heap Neil Tang 02/12/2008
Tree Representation Heap.
CSCE 3110 Data Structures and Algorithm Analysis
CS Data Structures Chapter 17 Heaps Mehmet H Gunes
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.
CSCE 3110 Data Structures and Algorithm Analysis
CSCE 3110 Data Structures and Algorithm Analysis
Heaps By JJ Shepherd.
Hash Maps: The point of a hash map is to FIND DATA QUICKLY.
Presentation transcript:

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)  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 like a binary search tree  A heap does order its node  A heap is a complete tree.  Every level but the leaf level is full

3 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++

4 Example of a Binary Heap

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

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

7 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”

8 Remove? How many steps? At most, how many steps to remove? Next: how would we implement a heap?

9  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 node at index i are at indices 2i+1, 2i+2 Where would we insert the next node (the child of the node containing 7)? Implementing a Heap

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

11 Deleting from a Heap 1. Set arr[0] to arr[curr_size-1], and shrink curr_size by 1 2. Set parent to 0 3. flag = true 4. while (flag) 5. Set leftchild to 2 * parent + 1, and rightchild to leftchild if leftchild ≥ curr_size, 7. flag is false 8. else: 9. Set maxchild to leftchild 10. If rightchild arr[leftchild] 11. set maxchild to rightchild 12. If arr[parent] ≥ arr[maxchild], 13. Flag is false 14. else: 15. Swap arr[parent] and arr[maxchild]; 16. set parent to maxchild

12 Performance of Heap  A complete tree of height h has:  Less than 2 h nodes (why?)  At least 2 h-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

Try:  18’s parent is?  (7-1)/2, or 20  Perform a delete  Remove 58  41 goes to position 0  Bubble 41 down

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

16 What if we just find the kth largest elements? 11, 7, 1, 3, 8, 4, 9, 2, 6, 10, 5 1. Sort the first k numbers from largest to smallest K = 5: 11, 8, 7, 4, 3 2. Compare remaining elements with the kth element in the array. 3. If new element is larger than kth element, bump the kth element out of the array and place the new element in the appropriate place. 4. Otherwise ignore the new element 1 9 -> 11, 9, 8, 7, 4, 2 6 -> 11, 9, 8, 7, 6, 10 -> 11, 10, 9, 8, 7, 5 The 5 th largest is 7 What is the running time? What does this work best for? Kth largest element:

17 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? So 5 is the 5 th smallest element in the list. Can we do better than this???? Note: Could we use this for sorting? What would be the timing?

18 Better: 11, 7, 1, 3, 8, 4, 9, 2, 6, 10, 5 1. build a heap (largest elements at top) with just the first k elements. 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. Timing of this Algorithm?