Heaps CS 302 – Data Structures Sections 8.8, 9.1, and 9.2.

Slides:



Advertisements
Similar presentations
DATA STRUCTURES AND ALGORITHMS Lecture Notes 9 Prepared by İnanç TAHRALI.
Advertisements

Queues CS 308 – Data Structures. What is a queue? It is an ordered group of homogeneous items of elements. Queues have two ends: –Elements are added at.
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.
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 HeapSort CS 3358 Data Structures. 2 Heapsort: Basic Idea Problem: Arrange an array of items into sorted order. 1) Transform the array of items into.
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.
CS 315 March 24 Goals: Heap (Chapter 6) priority queue definition of a heap Algorithms for Insert DeleteMin percolate-down Build-heap.
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.
More sorting algorithms: Heap sort & Radix sort. Heap Data Structure and Heap Sort (Chapter 7.6)
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.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (4) Data Structures 11/18/2008 Yang Song.
Source: Muangsin / Weiss1 Priority Queue (Heap) A kind of queue Dequeue gets element with the highest priority Priority is based on a comparable value.
Week 10: Heap and Priority queue. Any feature here?
Implementing a Queue as a Linked Structure CS 308 – Data Structures.
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.
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.
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 21 Binary Heap.
1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.
Chapter 9 Priority Queues, Heaps, Graphs 1 Fall 2010.
Queues CS 302 – Data Structures Sections 5.3 and 5.4.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 8 Prepared by İnanç TAHRALI.
COSC2007 Data Structures II Chapter 12 Tables & Priority Queues III.
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.
CE 221 Data Structures and Algorithms Chapter 6: Priority Queues (Binary Heaps) Text: Read Weiss, §6.1 – 6.3 1Izmir University of Economics.
Heaps & Priority Queues
Chapter 12 Heaps & HeapSort © John Urrutia 2014, All Rights Reserved1.
1 Heaps A heap is a binary tree. A heap is best implemented in sequential representation (using an array). Two important uses of heaps are: –(i) efficient.
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part R3. 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.
FALL 2005CENG 213 Data Structures1 Priority Queues (Heaps) Reference: Chapter 7.
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.
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)
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.
Partially Ordered Data ,Heap,Binary Heap
CS 201 Data Structures and Algorithms
Section 9.2b Adding and Removing from a Heap.
Source: Muangsin / Weiss
Bohyung Han CSE, POSTECH
Chapter 9 Priority Queues, Heaps, Graphs, and Sets
C++ Classes and Data Structures Jeffrey S. Childs
Tree Representation Heap.
CS Data Structures Chapter 17 Heaps Mehmet H Gunes
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.
Ch. 12 Tables and Priority Queues
Chapter 9 Priority Queues and Sets
Heaps and Priority Queues
C++ Plus Data Structures
Chapter 9 The Priority Queue ADT
Priority Queue and Heap
Presentation transcript:

Heaps CS 302 – Data Structures Sections 8.8, 9.1, and 9.2

Full Binary Tree Every non-leaf node has two children 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 from left to right (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 savings (i.e., no pointers) Preserve parent-child relationships Store: (i) level by level, and (ii) left to right

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] Leaf nodes: –tree.nodes[numElements/2] to tree.nodes[numElements - 1] Array-based representation of binary trees (cont.) (int division)

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

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

Not unique!

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

Heap implementation Heaps are always implemented as arrays!

Heap Specification template struct HeapType { void ReheapDown(int, int); void ReheapUp(int, int); ItemType *elements; // dynamic array int numElements; };

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

ReheapDown function template void HeapType ::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 { // two children if(elements[leftChild] <= elements[rightChild]) maxChild = rightChild; else maxChild = leftChild; } if(elements[root] < elements[maxChild]) {// compare max child with parent Swap(elements, root, maxChild); ReheapDown(maxChild, bottom); } rightmost node at the last level O(logN)

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

ReheapUp function template void HeapType ::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); } O(logN) rightmost node at the last level

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 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 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.)

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

Enqueue: insert a new element into the heap (1) Insert new element in the leftmost place at the bottom level (start new level if last level is full). (2) Fix the heap property by calling ReheapUp.

Inserting a new element into the heap (cont.)

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

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

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

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

Exercise 46, p. 545