Heaps and Priority Queues

Slides:



Advertisements
Similar presentations
What is a Queue? n Logical (or ADT) level: A queue is an ordered group of homogeneous items (elements), in which new elements are added at one end (the.
Advertisements

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.
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 Heaps & Priority Queues (Walls & Mirrors - Remainder of Chapter 11)
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.
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.
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.
1 C++ Plus Data Structures Nell Dale Chapter 7 Programming with Recursion Modified from the slides by Sylvia Sorkin, Community College of Baltimore County.
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.
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.
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.
Chapter 9 Priority Queues, Heaps, Graphs, and Sets.
Chapter 9 Priority Queues, Heaps, and Graphs. 2 Goals Describe a priority queue at the logical level and implement a priority queue as a list Describe.
Chapter 9 Priority Queues, Heaps, Graphs 1 Fall 2010.
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.
Heaps & Priority Queues
Intro. to Data Structures Chapter 6 Priority Queue (Heap) Veera Muangsin, Dept. of Computer Engineering, Chulalongkorn University 1 Priority Queue.
HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.
CS 367 Introduction to Data Structures Lecture 8.
CSI 312 Dr. Yousef Qawqzeh Heaps and Priority Queue.
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.
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:
Priority Queues A priority queue is an ADT where:
Partially Ordered Data ,Heap,Binary Heap
CS 201 Data Structures and Algorithms
Priority Queues and Heaps
Section 9.2b Adding and Removing from a Heap.
Source: Muangsin / Weiss
Bohyung Han CSE, POSTECH
Priority Queues Linked-list Insert Æ Æ head head
Binary Trees Lecture 36 Wed, Apr 21, /21/2018 Binary Trees.
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Chapter 9 Priority Queues, Heaps, Graphs, and Sets
CSCI2100 Data Structures Tutorial 7
Chapter 8 – Binary Search Tree
C++ Classes and Data Structures Jeffrey S. Childs
Heapsort Heap & Priority Queue.
B-Trees.
ITEC 2620M Introduction to Data Structures
Tree Representation Heap.
Heaps A heap is a binary tree.
CS Data Structures Chapter 17 Heaps Mehmet H Gunes
CS Data Structures Chapter 17 Heaps Mehmet H Gunes
CE 221 Data Structures and Algorithms
C++ Plus Data Structures
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.
Ch. 12 Tables and Priority Queues
Priority Queues & Heaps
Chapter 9 Priority Queues and Sets
CSI 1340 Introduction to Computer Science II
CS 367 – Introduction to Data Structures
Hash Maps: The point of a hash map is to FIND DATA QUICKLY.
Chapter 12 Heap ADT © 2011 Pearson Addison-Wesley. All rights reserved.
C++ Plus Data Structures
Chapter 9 The Priority Queue ADT
Priority Queue and Heap
Priority Queues Binary Heaps
EE 312 Software Design and Implementation I
CS210- Lecture 13 June 28, 2005 Agenda Heaps Complete Binary Tree
Presentation transcript:

Heaps and Priority Queues EE 312 Software Design and Implementation I

Full Binary Tree Every non-leaf node has two children All the leaves are on the same level Full Binary Tree

Complete Binary Tree (1) A binary tree that is either full or full through the next-to-last level (2) The last level is full from left to right (i.e., leaves are as far to the left as possible) Complete Binary Tree

Array-based representation of binary trees Memory space can be saved (no pointers are required) Preserve parent-child relationships by storing the tree elements in the array (i) level by level, and (ii) left to right 1 2 4 5 6 3 8 7 9

Array-based representation of binary trees (cont.) Parent-child relationships: left child of tree.nodes[index] = tree.nodes[2*index+1] right child of tree.nodes[index] = tree.nodes[2*index+2] parent node of tree.nodes[index] = tree.nodes[(index-1)/2] (int division, i.e., truncate) Leaf nodes: tree.nodes[numElements/2] to tree.nodes[numElements - 1] (int division, i.e., truncate)

Array-based representation of binary trees (cont.) Full or complete trees can be implemented easily using an array-based representation (elements occupy contiguous array slots) WARNING: "Dummy nodes" are required for trees which are not full or complete

What is a heap? It is a binary tree with the following properties: Property 1: it is a complete binary tree Property 2: the value stored at a node is greater or equal to the values stored at the children (heap property)

What is a heap? (cont.) Not unique!

Largest heap element From Property 2, the largest value of the heap is always stored at the root

Heap implementation using array representation A heap is a complete binary tree, so it is easy to be implemented using an array representation

Heap Specification template<class ItemType> struct HeapType { void ReheapDown(int, int); void ReheapUp(int, int); ItemType *elements; int numElements; // heap elements };

The ReheapDown function Assumption: heap property is violated at the root of the tree bottom

ReheapDown function template<class ItemType> rightmost node in the last level template<class ItemType> void HeapType<ItemType>::ReheapDown(int root, int bottom) { int maxChild, rightChild, leftChild;   leftChild = 2*root+1; rightChild = 2*root+2; if(leftChild <= bottom) { // left child is part of the heap if(leftChild == bottom) // only one child maxChild = leftChild; else { if(elements[leftChild] <= elements[rightChild]) maxChild = rightChild; else } if(elements[root] < elements[maxChild]) { Swap(elements, root, maxChild); ReheapDown(maxChild, bottom);

The ReheapUp function Assumption: heap property is violated at the bottom bottom Assumption: heap property is violated at the rightmost node at the last level of the tree bottom

ReheapUp function template<class ItemType> Assumption: heap property is violated at bottom template<class ItemType> void HeapType<ItemType>::ReheapUp(int root, int bottom) { int parent;   if(bottom > root) { // tree is not empty parent = (bottom-1)/2; if(elements[parent] < elements[bottom]) { Swap(elements, parent, bottom); ReheapUp(root, parent); }

Priority Queues What is a priority queue? It is a queue with each element being associated with a "priority" From the elements in the queue, the one with the highest priority is dequeued first

Priority queue specification template<class ItemType> class PQType { public: PQType(int); ~PQType(); void MakeEmpty(); bool IsEmpty() const; bool IsFull() const; void Enqueue(ItemType); void Dequeue(ItemType&); private: int numItems; // num of elements in the queue HeapType<ItemType> heap; int maxItems; // array size };

Dequeue: remove the largest element from the heap (1) Copy the bottom rightmost element to the root (2) Delete the bottom rightmost node (3) Fix the heap property by calling ReheapDown

Removing the largest element from the heap (cont.)

Removing the largest element from the heap (cont.)

Dequeue template<class ItemType> void PQType<ItemType>::Dequeue(ItemType& item) { item = heap.elements[0]; heap.elements[0] = heap.elements[numItems-1]; numItems--; heap.ReheapDown(0, numItems-1); } bottom

Enqueue: insert a new element into the heap (1) Insert the new element in the next bottom leftmost place (2) Fix the heap property by calling ReheapUp

Inserting a new element into the heap (cont.)

Enqueue template<class ItemType> void PQType<ItemType>::Enqueue(ItemType newItem) { numItems++; heap.elements[numItems-1] = newItem; heap.ReheapUp(0, numItems-1]); } bottom

Other Functions template<class ItemType> PQType<ItemType>::PQType(int max) { maxItems = max; heap.elements = new ItemType[max]; numItems = 0; }   PQType<ItemType>::MakeEmpty() PQType<ItemType>::~PQType() delete [] heap.elements;

Other Functions (cont.) template<class ItemType> bool PQType<ItemType>::IsFull() const { return numItems == maxItems; }   bool PQType<ItemType>::IsEmpty() const return numItems == 0;

Comparing heaps with other priority queue implementations Priority queue using a sorted list 12 4 O(N) on the average! Priority queue using heaps - Remove a key in O(logN) time - Insert a key in O(logN) time O(lgN) on the average!