- Alan Perlis. Topic 24 Heaps

Similar documents
CSE 214 Computer Science II Heaps and Priority Queues

Points off Total off Net Score. CS 314 Final Exam Spring 2016

ECE 242 Data Structures and Algorithms. Heaps I. Lecture 22. Prof. Eric Polizzi

CSCI 136 Data Structures & Advanced Programming. Lecture 22 Fall 2018 Instructor: Bills

BINARY HEAP cs2420 Introduction to Algorithms and Data Structures Spring 2015

CE 221 Data Structures and Algorithms

CS 307 Final Spring 2009

PRIORITY QUEUES AND HEAPS

Programming II (CS300)

CS350: Data Structures Heaps and Priority Queues

Before class. BSTs, Heaps, and PQs 2/26/13 1. Open TreeNodeExample.java Change main to:

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

ITI Introduction to Computing II

Abstract vs concrete data structures HEAPS AND PRIORITY QUEUES. Abstract vs concrete data structures. Concrete Data Types. Concrete data structures

ITI Introduction to Computing II

Programming Abstractions

Announcements HEAPS & PRIORITY QUEUES. Abstract vs concrete data structures. Concrete data structures. Abstract data structures

Priority Queues. 04/10/03 Lecture 22 1

CS 240 Fall Mike Lam, Professor. Priority Queues and Heaps

The priority is indicated by a number, the lower the number - the higher the priority.

PRIORITY QUEUES AND HEAPS

The heap is essentially an array-based binary tree with either the biggest or smallest element at the root.

Programming II (CS300)

Points off Total off Net Score. CS 314 Final Exam Spring 2017

Binary Search Trees. BinaryTree<E> Class (cont.) Section /27/2017

CSE 332 Autumn 2013: Midterm Exam (closed book, closed notes, no calculators)

Data Structure. A way to store and organize data in order to support efficient insertions, queries, searches, updates, and deletions.

Lecture 13: AVL Trees and Binary Heaps

Points off A 4B 5 Total off Net Score. CS 314 Final Exam Spring 2015

HEAPS & PRIORITY QUEUES

Data Structures. Giri Narasimhan Office: ECS 254A Phone: x-3748

Draw the resulting binary search tree. Be sure to show intermediate steps for partial credit (in case your final tree is incorrect).

University of Illinois at Urbana-Champaign Department of Computer Science. Second Examination

Programming Abstractions

3. Priority Queues. ADT Stack : LIFO. ADT Queue : FIFO. ADT Priority Queue : pick the element with the lowest (or highest) priority.

Data Structures and Algorithms

Section 1: True / False (1 point each, 15 pts total)

" Which data Structure to extend to create a heap? " Two binary tree extensions are needed in a heap. n it is a binary tree (a complete one)

Definition of a Heap. Heaps. Priority Queues. Example. Implementation using a heap. Heap ADT

Chapter 5. Binary Trees

CSE100 Practice Final Exam Section C Fall 2015: Dec 10 th, Problem Topic Points Possible Points Earned Grader

CSE 2123: Collections: Priority Queues. Jeremy Morris

CS 314 Final Fall 2011

PRIORITY QUEUES AND HEAPS

Priority Queue. How to implement a Priority Queue? queue. priority queue

Name CPTR246 Spring '17 (100 total points) Exam 3

CS165: Priority Queues, Heaps

Tree: non-recursive definition. Trees, Binary Search Trees, and Heaps. Tree: recursive definition. Tree: example.

Lecture 23: Priority Queues 10:00 AM, Mar 19, 2018

Priority queues. Priority queues. Priority queue operations

Heaps, stacks, queues

Final Examination CSE 100 UCSD (Practice)

Section 4 SOLUTION: AVL Trees & B-Trees

Priority Queues Heaps Heapsort

Computational Optimization ISE 407. Lecture 16. Dr. Ted Ralphs

Readings. Priority Queue ADT. FindMin Problem. Priority Queues & Binary Heaps. List implementation of a Priority Queue

EXAMINATIONS 2015 COMP103 INTRODUCTION TO DATA STRUCTURES AND ALGORITHMS

Balanced Binary Search Trees. Victor Gao

CMSC 341 Lecture 14: Priority Queues, Heaps

Thus, it is reasonable to compare binary search trees and binary heaps as is shown in Table 1.

Chapter 9. Priority Queue

MLR Institute of Technology

Programming II (CS300)

CS200 Spring 2004 Midterm Exam II April 14, Value Points a. 5 3b. 16 3c. 8 3d Total 100

CSC 321: Data Structures. Fall 2016

CSC 321: Data Structures. Fall 2017

CS 61B Midterm 2 Guerrilla Section Spring 2018 March 17, 2018

Direct Addressing Hash table: Collision resolution how handle collisions Hash Functions:

Algorithms and Data Structures

9. Heap : Priority Queue

Dictionaries. Priority Queues

CSCI212 Computer Science. Binary Trees/Heaps/Binary Search Trees

Priority Queues Heaps Heapsort

Figure 1: A complete binary tree.

COMP 251 Winter 2017 Online quizzes with answers

Trees. (Trees) Data Structures and Programming Spring / 28

The Problem with Linked Lists. Topic 18. Attendance Question 1. Binary Search Trees. -Monty Python and The Holy Grail

Data Structures. Outline. Introduction Linked Lists Stacks Queues Trees Deitel & Associates, Inc. All rights reserved.

managing an evolving set of connected components implementing a Union-Find data structure implementing Kruskal s algorithm

Binary Trees. Directed, Rooted Tree. Terminology. Trees. Binary Trees. Possible Implementation 4/18/2013

CSC 421: Algorithm Design Analysis. Spring 2013

Spring 2018 Mentoring 8: March 14, Binary Trees

Maintain binary search tree property nodes to the left are less than the current node, nodes to the right are greater

CS302 - Data Structures using C++

Priority Queues. e.g. jobs sent to a printer, Operating system job scheduler in a multi-user environment. Simulation environments

A6-R3: DATA STRUCTURE THROUGH C LANGUAGE

Heaps. A complete binary tree can be easily stored in an array - place the root in position 1 (for convenience)

CS 314 Final Fall 2012

Data Structure. IBPS SO (IT- Officer) Exam 2017

TREES cs2420 Introduction to Algorithms and Data Structures Spring 2015

CS102 Binary Search Trees

Priority queues. Priority queues. Priority queue operations

COS 226 Algorithms and Data Structures Fall Midterm

Priority Queues. Binary Heaps

COMP 103 RECAP-TODAY. Priority Queues and Heaps. Queues and Priority Queues 3 Queues: Oldest out first

CS 151 Name Final Exam December 17, 2014

Treaps. 1 Binary Search Trees (BSTs) CSE341T/CSE549T 11/05/2014. Lecture 19

CS302 - Data Structures using C++

Trees. A tree is a directed graph with the property

Transcription:

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." - Alan Perlis

Recall priority queue Priority Queue elements enqueued based on priority dequeue removes the highest priority item Options? List? Binary Search Tree? Linked List enqueue BST enqueue A. O(N) O(1) B. O(N) O(logN) C. O(N) O(N) D. O(logN) O(logN) E. O(1) O(logN) CS314 Heaps 2

A heap Another Option not to be confused with the runtime heap (portion of memory for dynamically allocated variables) A complete binary tree all levels have maximum number of nodes except deepest where nodes are filled in from left to right Maintains the heap order property in a min heap the value in the root of any subtree is less than or equal to all other values in the subtree CS314 Heaps 3

Clicker Question 2 In a max heap with no duplicates where is the largest value? A. the root of the tree B. in the left-most node C. in the right-most node D. a node in the lowest level E. None of these CS314 Heaps 4

Example Min Heap 12 17 16 19 52 37 25 21 45 CS314 Heaps 5

Enqueue Operation Add new element to next open spot in array Swap with parent if new value is less than parent Continue back up the tree as long as the new value is less than new parent node CS314 Heaps 6

Enqueue Example Add 15 to heap (initially next left most node) 12 17 16 19 52 37 25 21 45 15 CS314 Heaps 7

Swap 15 and 52 Enqueue Example 12 17 16 19 15 37 25 21 45 52 CS314 Heaps 8

Enqueue Example Swap 15 and 17, then stop 12 15 16 19 17 37 25 21 45 52 CS314 Heaps 9

Internal Storage Interestingly heaps are often implemented with an array instead of nodes 12 17 16 19 52 37 25 for element at position i: parent index: i / 2 left child index: i * 2 right child index: i * 2 + 1 21 45 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 12 17 16 19 52 37 25 21 45 CS314 Heaps 10

PriorityQueue Class public class PriorityQueue<E extends Comparable<E>> { private int size; private E[] con; public PriorityQueue() { } heap = getarray(2); private E[] getarray(int size) { } return (E[]) (new Comparable[size]); CS314 Heaps 11

PriorityQueue enqueue public void enqueue(e val) { if ( size >= con.length - 1 ) enlargearray( con.length * 2 + 1 ); size++; int indextoplace = size; while ( indextoplace > 1 && val.compareto( con[indextoplace / 2] ) < 0 ) { } con[indextoplace] = con[indextoplace / 2]; // swap indextoplace /= 2; // change indextoplace to parent } con[indextoplace] = val; private void enlargearray(int newsize) { E[] temp = getarray(newsize); System.arraycopy(con, 1, temp, 1, size); con = temp; } 12

Dequeue min value / front of queue is in root of tree swap value from last node to root and move down swapping with smaller child unless values is smaller than both children CS314 Heaps 13

Dequeue Example Swap 35 into root (save 12 to return) 12 15 16 17 23 37 25 21 45 35 CS314 Heaps 14

Dequeue Example Swap 35 into root (save 12 to return) 35 15 16 17 23 37 25 21 45 CS314 Heaps 15

Dequeue Example Swap 35 with smaller child (15) 15 35 16 17 23 37 25 21 45 CS314 Heaps 16

Dequeue Example Swap 35 with smaller child (17) 15 17 16 35 23 37 25 21 45 CS314 Heaps 17

Dequeue Example Swap 35 with smaller child (21) 15 17 16 21 23 37 25 35 45 CS314 Heaps 18

Dequeue Code public E dequeue( ) { E top = con[1]; int hole = 1; boolean done = false; while ( hole * 2 < size &&! done ) { int child = hole * 2; // see which child is smaller if ( con[child].compareto( con[child + 1] ) > 0 ) child++; // child now points to smaller } // is replacement value bigger than child? if (con[size].compareto( con[child] ) > 0 ) { con[hole] = con[child]; hole = child; } else done = true; } con[hole] = con[size]; size--; return top; 19

PriorityQueue Comparison Run a Stress test of PQ implemented with Heap and PQ implemented with BinarySearchTree What will result be? A. Heap takes half the time or less of BST B. Heap faster, but not twice as fast C. About the same D. BST faster, but not twice as fast E. BST takes half the time or less of Heap CS314 Heaps 20

Data Structures Data structures we have studied arrays, array based lists, linked lists, maps, sets, stacks, queue, trees, binary search trees, graphs, hash tables, red-black trees, priority queues, heaps Most program languages have some built in data structures, native or library Must be familiar with performance of data structures best learned by implementing them yourself CS314 Heaps 21

Data Structures We have not covered every data structure Heaps http://en.wikipedia.org/wiki/list_of_data_structures

Data Structures deque, b-trees, quad-trees, binary space partition trees, skip list, sparse list, sparse matrix, union-find data structure, Bloom filters, AVL trees, trie, 2-3-4 trees, and more! Must be able to learn new and apply new data structures CS314 Heaps 23