heaps1 Trees IV: The Heap heaps2 Heap Like a binary search tree, a heap is a binary tree in which the data entries can be compared using total order.

Slides:



Advertisements
Similar presentations
1 A full binary tree A full binary tree is a binary tree in which all the leaves are on the same level and every non leaf node has two children. SHAPE.
Advertisements

Chapter 15 Heaps. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Define a heap abstract data structure Demonstrate.
1 Nell Dale Chapter 9 Trees Plus Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
1 A Two-Level Binary Expression ‘-’ ‘8’ ‘5’ treePtr INORDER TRAVERSAL : has value 3 PREORDER TRAVERSAL: POSTORDER TRAVERSAL: 8 5 -
1 CSC 427: Data Structures and Algorithm Analysis Fall 2004 Heaps and heap sort  complete tree, heap property  min-heaps & max-heaps  heap 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.
Priority Queues, Heaps CS3240, L. grewe 1. 2 Goals Describe a priority queue at the logical level and implement a priority queue as a list Describe the.
Heaps CS 308 – Data Structures.
1 Nell Dale Chapter 9 Trees Plus Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
Chapter 9 Heaps and Priority Queues. 9-2 What is a Heap? A heap is a binary tree that satisfies these special SHAPE and ORDER properties: –Its shape must.
Source: Muangsin / Weiss1 Priority Queue (Heap) A kind of queue Dequeue gets element with the highest priority Priority is based on a comparable value.
sorting31 Sorting III: Heapsort sorting32 A good sorting algorithm is hard to find... Quadratic sorting algorithms (with running times of O(N 2 ), such.
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.
9 Priority Queues, Heaps, and Graphs. 9-2 What is a Heap? A heap is a binary tree that satisfies these special SHAPE and ORDER properties: –Its shape.
Priority Queues and Heaps Bryce Boe 2013/11/20 CS24, Fall 2013.
1 Nell Dale Chapter 9 Trees Plus Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
Heaps and Priority Queues CS 3358 – Data Structures.
Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07.
Computer Science and Software Engineering University of Wisconsin - Platteville 12. Heap Yan Shi CS/SE 2630 Lecture Notes Partially adopted from C++ Plus.
9/17/20151 Chapter 12 - Heaps. 9/17/20152 Introduction ► Heaps are largely about priority queues. ► They are an alternative data structure to implementing.
Chapter 9 Priority Queues, Heaps, Graphs, and Sets.
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 ordered along paths from root to leaf
Topic 24 Heaps "You think you know when you can learn, are more sure when you can write even more when you can teach, but certain when you can program."
CSC 213 – Large Scale Programming Lecture 15: Heap-based Priority Queue.
Sorting Dr. Yingwu Zhu. Heaps A heap is a binary tree with properties: 1. It is complete Each level of tree completely filled Except possibly bottom level.
PRIORITY QUEUES AND HEAPS CS16: Introduction to Data Structures & Algorithms Tuesday, February 24,
Priority Queues and Heaps. October 2004John Edgar2  A queue should implement at least the first two of these operations:  insert – insert item at the.
CSC211 Data Structures Lecture 18 Heaps and Priority Queues Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.
Heaps Chapter 21. What is a heap used for? Sorting –HeapSort sorts an N-element array on O(N log N) time and uses very little extra memory Priority Queues.
Data Structures Using C++1 Chapter 10 Sorting Algorithms.
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.
CE 221 Data Structures and Algorithms Chapter 6: Priority Queues (Binary Heaps) Text: Read Weiss, §6.1 – 6.3 1Izmir University of Economics.
Queues, Stacks and Heaps. Queue List structure using the FIFO process Nodes are removed form the front and added to the back ABDC FrontBack.
CSC212 Data Structure Lecture 16 Heaps and Priority Queues Instructor: George Wolberg Department of Computer Science City College of New York.
Chapter 12 Heaps & HeapSort © John Urrutia 2014, All Rights Reserved1.
Intro. to Data Structures Chapter 6 Priority Queue (Heap) Veera Muangsin, Dept. of Computer Engineering, Chulalongkorn University 1 Priority Queue.
Sorting Dr. Yingwu Zhu. Heaps A heap is a binary tree with properties: 1. It is complete Each level of tree completely filled Except possibly bottom level.
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.
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.
Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin, and Skylight.
CS 367 Introduction to Data Structures Lecture 8.
Priority Queues and Heaps Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University.
Priority Queues CS 110: Data Structures and Algorithms First Semester,
1 Chapter 6 Heapsort. 2 About this lecture Introduce Heap – Shape Property and Heap Property – Heap Operations Heapsort: Use Heap to Sort Fixing heap.
Properties: -The value in each node is greater than all values in the node’s subtrees -Complete tree! (fills up from left to right) Max Heap.
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 CS 302 – Data Structures Sections 8.8, 9.1, and 9.2.
Chapter 9 Heaps and Priority Queues Lecture 18. Full Binary Tree Every non-leaf node has two children Leaves are on the same level Full Binary Tree.
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.
CSC 213 – Large Scale Programming. Priority Queue ADT  Prioritizes Entry s using their keys  For Entry s with equal priorities, order not specified.
Priority Queues and Heaps. John Edgar  Define the ADT priority queue  Define the partially ordered property  Define a heap  Implement a heap using.
Source: Muangsin / Weiss
Heap Chapter 9 Objectives Upon completion you will be able to:
- Alan Perlis Heaps "You think you know when you can learn,
Chapter 9 Priority Queues, Heaps, Graphs, and Sets
Draw pictures to indicate the subproblems middleMax solves at each level and the resulting maxPtr and PrevPtr for each on this linked list:
ITEC 2620M Introduction to Data Structures
CS Data Structures Chapter 17 Heaps Mehmet H Gunes
Computer Science 2 Heaps.
Heaps Chapter 11 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates.
Heaps and Priority Queues
C++ Plus Data Structures
Chapter 9 The Priority Queue ADT
Heaps.
Presentation transcript:

heaps1 Trees IV: The Heap

heaps2 Heap Like a binary search tree, a heap is a binary tree in which the data entries can be compared using total order semantics Defining qualities of a heap: –the data entry contained in any node is greater than or equal to the data entry contained in either of its children –the tree is always complete

heaps3 Heap Example

heaps4 Heap Applications Heap can be used to implement a sorting algorithm called heapsort Heap is also a handy way to implement a priority queue -- we’ll use this application to illustrate how heaps work

heaps5 Heap Implementation Because a heap is by definition a complete tree, it is easy to use an array-based implementation reHeapUp reHeapDownHeap operations reHeapUp and reHeapDown are used to maintain the other defining quality of a heap -- each node’s data entry >= data entries of its children

heaps6 ReHeapUp operation When a new node is added to the tree, it is always added at the leftmost open position in the bottom row 35 This maintains completeness of tree, but can violate the other defining characteristic of a heap

heaps7 ReHeapUp operation ReHeapUp restores the second heap condition: a parent node’s data is always greater than or equal to the data in either of its child nodes The operation is accomplished by swapping the new node’s data entry with that of its parent 35 7 Parent/child data swapping continues until the heap condition is restored 32 35

heaps8 ReHeapDown operation In a priority queue, the highest-priority item is always dequeued first -- this item would be the top item of the heap Since this is going to be an array implementation, we’ll perform the usual trick of swapping the last array entry with the first, so we can minimize the amount of copying to be done

heaps9 ReHeapDown operation Now we can effectively remove the item by simply diminishing the “used” portion of our array by 1 (already done here) We are once again faced with the same problem -- the heap is the right shape, but the data values are in the wrong positions We solve this problem, as before, by swapping data between nodes. In this case, we swap the parent node’s data with the largest data entry of the two children, continuing until the heap is restored

heaps10 Example: Priority Queue with heap implementation Priority queue has all operations associated with a queue: enqueue, dequeue and helper functions (e.g. size( ), is_empty( ), etc.) Heap is a good fit for priority queue because highest-priority item should always be dequeued first, and this item would always be found at the top of a heap

heaps11 Code for Priority Queue template class PQheap { public: PQheap( ); enum {SIZE=30}; void enqueue (const thing& entry); thing dequeue( ); size_t size( )const {return numItems;}...

heaps12 Code for Priority Queue private: thing data[SIZE]; size_t numItems; void reHeapUp(size_t n); void reHeapDown(size_t n); size_t parent(size_t n) const {return (n-1)/2;} size_t left_child(size_t n) const {return (2*n)+1;} size_t right_child(size_t n) const {return (2*n)+2;} void Swap (thing& x, thing& y); };

heaps13 Code for Priority Queue template PQheap ::PQheap() { numItems=0; }

heaps14 Code for Priority Queue template void PQheap ::enqueue (const thing& entry) { assert (size( ) < SIZE); data[numItems] = entry; reHeapUp(numItems); numItems++; }

heaps15 Code for Priority Queue template void PQheap ::reHeapUp(size_t n) { size_t x = n; while (x>0 && data[x]> data[parent(x)]) { Swap(data[x], data[parent(x)]); x=parent(x); } }

heaps16 Code for Priority Queue template void PQheap ::Swap (thing& x, thing& y) { thing temp = x; x = y; y = temp; }

heaps17 Code for Priority Queue template thing PQheap ::dequeue() { thing value=data[0]; // save return value numItems--; data[0]=data[numItems]; // swap top & bottom reHeapDown(numItems); // restore heap return value; }

heaps18 Code for Priority Queue template void PQheap ::reHeapDown(size_t n) { size_t current = 0, big_child, heap_ok = 0; while ((!heap_ok) && (left_child(current) < n)) { if (right_child(current) >= n) big_child = left_child(current); else if (data[left_child(current)] > data[right_child(current)]) big_child = left_child(current); else big_child = right_child(current);...

heaps19 Code for Priority Queue if (data[current] < data[big_child]) { Swap(data[current], data[big_child]); current = big_child; } else heap_ok = 1; } // end of while loop } // end of function