INFO1x05 Tutorial 6. Exercise 1: Heaps and Priority Queues

Size: px
Start display at page:

Download "INFO1x05 Tutorial 6. Exercise 1: Heaps and Priority Queues"

Transcription

1 INFO1x05 Tutorial 6 Heaps and Priority Queues Exercise 1: 1. How long would it take to remove the log n smallest elements from a heap that contains n entries, using the operation? 2. Suppose you label each node v of a binary tree T with a key equal to the preorder rank of v. Under what circumstances is T a heap? 3. What is the output from the following sequence of priority queue ADT methods? (Assume the operation prints out the removed element.) insert(5, A) insert(4, B) insert(7, I) insert(1, D) insert(3, J) insert(6, L) insert(8, G), insert(2, H) (a) Each removemin call runs in O (log n), so log n calls would run on the order of O ( log 2 n ). (b) Always, as the parent node will always have a smaller value than its children. Remember, a heap makes no constraints as to the ordering of siblings/cousins etc. (c) (1,D), (3, J), (4,B), (5,A), (2,H), (6,L). 1

2 Exercise 2: In Java, the PriorityQueue() implementation implements a min-heap type priority queue. Although it is technically correct to use a reverse comparator with the PriorityQueue, so that objects are inserted into the heap in reverse order, it can be confusing to get back the element with the maximal value every time we call a method called. Write a short adaptor class which wraps around the PriorityQueue() class and constructs a max-heap, with methods like RemoveMax() to access elements. See git repo tutes/w06/soln. Exercise 3: 1. Illustrate the execution of the selection-sort algorithm on the following input sequence: 22,, 36, 44, 10, 3, 9, 13, 29, Give an example of a worst-case sequence with n elements for insertion-sort, and show that insertion-sort runs in Ω(n 2 ) time on such a sequence. A worst-case sequence for insertion sort would be one that is in descending order of keys, e.g., With this sequence, each element will first be moved to the front and then moved back in the sequence incrementally, as every remaining is processed. Thus, each element will be moved n times. For n elements, this means at a total of n 2 times, which implies Ω(n 2 ) time overall. Data Structures Page 2 of 7

3 Exercise 4: (INFO05) Show how to implement the stack ADT using only a priority queue and one additional integer instance variable. Then, implement a class MyStack in java, using a wrapper around the PriorityQueue class. Your class should be able to store String objects. A skeleton is provided below: Note: Keep in mind that the Java priority queue is a min-heap. 1 import java.util.priorityqueue; 2 3 public class MyStack { 4 public MyStack() { 5 // Your code goes here 6 } 7 8 public void push(string s) { 9 // Your code goes here 12 public String pop() { 13 // Your code goes here 16 public int size() { 17 // Your code goes here 20 public boolean isempty() { 21 // Your code goes here 23 } Hint: You may find the following class helpful: 1 public class KeyValue <E> 2 implements Comparable<KeyValue<E> > { 3 4 private Integer key; 5 private E value; 6 7 public KeyValue(int key, E value) { 8 this.key = key; 9 this.value = value; 12 public Integer getkey() { 13 return key; 16 public E getvalue() { 17 return value; Data Structures Page 3 of 7

4 20 public int compareto(keyvalue<e> o) { 21 return key.compareto(o.key); } Extension: Modify your class so it can store arbitrary data types. Maintain a variable m initialized to 0. On a push operation for element e, call insert(m, e) and decrement m. On a pop operation, call remove and increment m. Programming Solution in repo. Exercise 5: (INFO05) Show how to implement the (standard) queue ADT using only a priority queue and one additional integer instance variable. Then, implement a class MyQueue in java, using a wrapper around the PriorityQueue class. Your class should be able to store String objects. A skeleton is provided below: Note: Keep in mind that the Java priority queue is a min-heap. 1 import java.util.priorityqueue; 2 3 public class MyQueue { 4 public MyQueue() { 5 // Your code goes here 6 } 7 8 public void enqueue(string s) { 9 // Your code goes here 12 public String dequeue() { 13 // Your code goes here 16 public int size() { 17 // Your code goes here 20 public boolean isempty() { 21 // Your code goes here 23 } Data Structures Page 4 of 7

5 Hint: You may find the following class helpful: 1 public class KeyValue <E> 2 implements Comparable<KeyValue<E> > { 3 4 private Integer key; 5 private E value; 6 7 public KeyValue(int key, E value) { 8 this.key = key; 9 this.value = value; 12 public Integer getkey() { 13 return key; 16 public E getvalue() { 17 return value; 20 public int compareto(keyvalue<e> o) { 21 return key.compareto(o.key); } Extension: Modify your class so it can store arbitrary data types. Maintain a maxkey variable initialized to 0. On an enqueue operation for element e, call insertitem(maxkey, e) and increment maxkey. On a dequeue operation, call removeminelement and decrement maxkey. Programming Solution in Repo. Exercise 6: (INFO05) Given a heap T and a key k, give an algorithm to compute all the entries in T with key less than or equal to k. For example, the heap show in Figure?? and query k = 7, the algorithm should report the entries with keys 2, 4, 5, 6, and 7 (but not necessarily in this order). Your algorithm should run in time proportional to the number of entries returned. Data Structures Page 5 of 7

6 (2, C) (5, A) (4, C) (, K) (9, F) (7, Q) (6, Z) (16, X) (25, J) (14, E) (12, H) (, S) (8, W) (20, B) (10, L) Figure 1: Heap T Starting at the root of the tree, recursively search the left and right subtrees if the root of the subtree has a key value less than k. This algorithm takes O(k) time, because there is no node in T storing a key larger than k that has a descendent storing a key less than k. Exercise 7: (INFO05) Develop an algorithm that computes the kth smallest element of a set of n distinct integers in O(n + k log n) time. Construct a heap, which takes O(n) time. Then call removemin k times, which takes O(k log n) time. Exercise 8: (INFO05) An online computer system for trading stock need to process orders of the form buy 100 shares at $x each or sell 100 shares at $y each. A buy order for $x can only be processed if there is an existing sell order with price $y such that y x. Likewise, a sell order for $y can only be processed if there is an existing buy order with price $x such that y x. If a buy or sell order is entered but cannot be processed, it must be stored until an order is entered which allows it to be processed. Describe a scheme that allows for buy and sell orders to be entered in O(log n) time, independent of whether or not they can be immediately processed. Data Structures Page 6 of 7

7 Create two heaps, one for sell orders, the other for buy orders. We need to store in each node both the quantity and price, with the price as the key. When an order comes in, keep popping items off the corresponding priority queue, until a large enough quantity has been removed, subject to the constraint. On failure, add the popped items back onto the priority queue and the other order onto the other queue. On average we would expect this algorithm to run in O (log n) time, however in the worst case, it will take O (n log n) time, as to fulfil an order we may have to empty the queue. Exercise 9: (INFO05) Write a comparator for nonnegative integers that determines order based on the number of 1 s in each integer s binary expansion, so that i < j if the number of 1 s in the binary representation of i is less than the number of 1 s in the binary representation of j. 1 import java.util.comparator; 2 public class integercomparator implements Comparator { 3 private int countbits(object a) { 4 if( a == null ) { 5 throw new RuntimeException("Null Argument"); 6 } 7 try{ 8 int numones = 0; 9 int tmp = ((Integer) a).intvalue(); 10 while( tmp!= 0 ) { int bit = tmp & 1; 12 if( bit == 1 ) 13 numones++; 14 tmp = tmp >> 1; } 16 return numones; 17 } 18 catch( ClassCastException e ) { throw new RuntimeException("Argument is not an Integer"); 20 } 21 } 22 public int compare(object a, Object b) { 23 int vala = countbits( a ); 24 int valb = countbits( b ); 25 return ( vala - valb ); 26 } 27 } Data Structures Page 7 of 7

Describe how to implement deque ADT using two stacks as the only instance variables. What are the running times of the methods

Describe how to implement deque ADT using two stacks as the only instance variables. What are the running times of the methods Describe how to implement deque ADT using two stacks as the only instance variables. What are the running times of the methods 1 2 Given : Stack A, Stack B 3 // based on requirement b will be reverse of

More information

Priority Queues & Heaps. Chapter 9

Priority Queues & Heaps. Chapter 9 Priority Queues & Heaps Chapter 9 The Java Collections Framework (Ordered Data Types) Interface Abstract Class Class Iterable Collection Queue Abstract Collection List Abstract Queue Abstract List Priority

More information

Priority queues. Priority queues. Priority queue operations

Priority queues. Priority queues. Priority queue operations Priority queues March 30, 018 1 Priority queues The ADT priority queue stores arbitrary objects with priorities. An object with the highest priority gets served first. Objects with priorities are defined

More information

Stores a collection of elements each with an associated key value

Stores a collection of elements each with an associated key value CH9. PRIORITY QUEUES ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN JAVA, GOODRICH, TAMASSIA AND GOLDWASSER (WILEY 201) PRIORITY QUEUES Stores a collection

More information

Priority Queues 3/19/14

Priority Queues 3/19/14 Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser, Wiley, 2014 Priority Queues Priority Queues 1 Priority

More information

Priority queues. Priority queues. Priority queue operations

Priority queues. Priority queues. Priority queue operations Priority queues March 8, 08 Priority queues The ADT priority queue stores arbitrary objects with priorities. An object with the highest priority gets served first. Objects with priorities are defined by

More information

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

Name CPTR246 Spring '17 (100 total points) Exam 3 Name CPTR246 Spring '17 (100 total points) Exam 3 1. Linked Lists Consider the following linked list of integers (sorted from lowest to highest) and the changes described. Make the necessary changes in

More information

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

3. Priority Queues. ADT Stack : LIFO. ADT Queue : FIFO. ADT Priority Queue : pick the element with the lowest (or highest) priority. 3. Priority Queues 3. Priority Queues ADT Stack : LIFO. ADT Queue : FIFO. ADT Priority Queue : pick the element with the lowest (or highest) priority. Malek Mouhoub, CS340 Winter 2007 1 3. Priority Queues

More information

Priority Queues Goodrich, Tamassia. Priority Queues 1

Priority Queues Goodrich, Tamassia. Priority Queues 1 Priority Queues Priority Queues 1 Priority Queue ADT A priority queue stores a collection of entries Each entry is a pair (key, value) Main methods of the Priority Queue ADT insert(k, x) inserts an entry

More information

Priority Queues. Outline. COMP9024: Data Structures and Algorithms. Priority Queue ADT. Total Order Relations. Entry ADT

Priority Queues. Outline. COMP9024: Data Structures and Algorithms. Priority Queue ADT. Total Order Relations. Entry ADT COMP0: Data Structures and Algorithms Week Seven: Priority Queues Hui Wu Outline Priority Queues Heaps Adaptable Priority Queues Session, 0 http://www.cse.unsw.edu.au/~cs0 Priority Queue ADT Priority Queues

More information

CH 8. HEAPS AND PRIORITY QUEUES

CH 8. HEAPS AND PRIORITY QUEUES CH 8. HEAPS AND PRIORITY QUEUES ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA AND MOUNT (WILEY 2004) AND SLIDES FROM NANCY

More information

CH. 8 PRIORITY QUEUES AND HEAPS

CH. 8 PRIORITY QUEUES AND HEAPS CH. 8 PRIORITY QUEUES AND HEAPS ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA AND MOUNT (WILEY 2004) AND SLIDES FROM NANCY

More information

Data Structures and Algorithms " Priority Queues!

Data Structures and Algorithms  Priority Queues! Data Structures and Algorithms " Priority Queues! Outline" Priority Queues! Heaps! Adaptable Priority Queues! Priority Queues" Priority Queue ADT" A priority queue stores a collection of entries! Each

More information

The smallest element is the first one removed. (You could also define a largest-first-out priority queue)

The smallest element is the first one removed. (You could also define a largest-first-out priority queue) Priority Queues Priority queue A stack is first in, last out A queue is first in, first out A priority queue is least-first-out The smallest element is the first one removed (You could also define a largest-first-out

More information

CSE 2123: Collections: Priority Queues. Jeremy Morris

CSE 2123: Collections: Priority Queues. Jeremy Morris CSE 2123: Collections: Priority Queues Jeremy Morris 1 Collections Priority Queue Recall: A queue is a specific type of collection Keeps elements in a particular order We ve seen two examples FIFO queues

More information

Heaps and Priority Queues

Heaps and Priority Queues Heaps and Priority Queues Lecture delivered by: Venkatanatha Sarma Y Assistant Professor MSRSAS-Bangalore 11 Objectives To introduce Priority Queue ADT To discuss and illustrate Priority Queues for sorting

More information

Programming II (CS300)

Programming II (CS300) 1 Programming II (CS300) Chapter 10: Search and Heaps MOUNA KACEM mouna@cs.wisc.edu Spring 2018 Search and Heaps 2 Linear Search Binary Search Introduction to trees Priority Queues Heaps Linear Search

More information

Priority Queues. INFO0902 Data Structures and Algorithms. Priority Queues (files à priorités) Keys. Priority Queues

Priority Queues. INFO0902 Data Structures and Algorithms. Priority Queues (files à priorités) Keys. Priority Queues Priority Queues INFO0902 Data Structures and Algorithms Priority Queues Justus H. Piater Priority Queues (files à priorités) Keys Extract the top-priority element at any time. No notion of order, positions,

More information

Algorithms and Data Structures

Algorithms and Data Structures Algorithms and Data Structures Dr. Malek Mouhoub Department of Computer Science University of Regina Fall 2002 Malek Mouhoub, CS3620 Fall 2002 1 6. Priority Queues 6. Priority Queues ffl ADT Stack : LIFO.

More information

INF2220: algorithms and data structures Series 1

INF2220: algorithms and data structures Series 1 Universitetet i Oslo Institutt for Informatikk A. Maus, R.K. Runde, I. Yu INF2220: algorithms and data structures Series 1 Topic Trees & estimation of running time (Exercises with hints for solution) Issued:

More information

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

CSCI 136 Data Structures & Advanced Programming. Lecture 22 Fall 2018 Instructor: Bills CSCI 136 Data Structures & Advanced Programming Lecture 22 Fall 2018 Instructor: Bills Last Time Lab 7: Two Towers Array Representations of (Binary) Trees Application: Huffman Encoding 2 Today Improving

More information

CSED233: Data Structures (2017F) Lecture9: Priority Queues and Heaps

CSED233: Data Structures (2017F) Lecture9: Priority Queues and Heaps (2017F) Lecture9: Priority Queues and Heaps Daijin Kim CSE, POSTECH dkim@postech.ac.kr Priority Queue ADT A priority queue stores a coll ection of entries Each entry is a pair (key, value) Main methods

More information

CE 221 Data Structures and Algorithms

CE 221 Data Structures and Algorithms CE 2 Data Structures and Algorithms Chapter 6: Priority Queues (Binary Heaps) Text: Read Weiss, 6.1 6.3 Izmir University of Economics 1 A kind of queue Priority Queue (Heap) Dequeue gets element with the

More information

HEAPS: IMPLEMENTING EFFICIENT PRIORITY QUEUES

HEAPS: IMPLEMENTING EFFICIENT PRIORITY QUEUES HEAPS: IMPLEMENTING EFFICIENT PRIORITY QUEUES 2 5 6 9 7 Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition, by M. T. Goodrich, R. Tamassia, and M. H., Wiley, 2014

More information

COS 226 Algorithms and Data Structures Fall Midterm

COS 226 Algorithms and Data Structures Fall Midterm COS 226 Algorithms and Data Structures Fall 2017 Midterm This exam has 10 questions (including question 0) worth a total of 55 points. You have 0 minutes. This exam is preprocessed by a computer, so please

More information

STACKS AND QUEUES. Problem Solving with Computers-II

STACKS AND QUEUES. Problem Solving with Computers-II STACKS AND QUEUES Problem Solving with Computers-II 2 Stacks container class available in the C++ STL Container class that uses the Last In First Out (LIFO) principle Methods i. push() ii. iii. iv. pop()

More information

CSE 214 Computer Science II Heaps and Priority Queues

CSE 214 Computer Science II Heaps and Priority Queues CSE 214 Computer Science II Heaps and Priority Queues Spring 2018 Stony Brook University Instructor: Shebuti Rayana shebuti.rayana@stonybrook.edu http://www3.cs.stonybrook.edu/~cse214/sec02/ Introduction

More information

Heaps 2. Recall Priority Queue ADT. Heaps 3/19/14

Heaps 2. Recall Priority Queue ADT. Heaps 3/19/14 Heaps 3// Presentation for use with the textbook Data Structures and Algorithms in Java, th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser, Wiley, 0 Heaps Heaps Recall Priority Queue ADT

More information

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

 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) /7/ Queue vs Priority Queue Priority Queue Implementations queue 0 0 Keep them sorted! (Have we implemented it already?) Appropriate if the number of items is small Sorted Array-based items 0... items

More information

Data Structures Lecture 7

Data Structures Lecture 7 Fall 2017 Fang Yu Software Security Lab. Dept. Management Information Systems, National Chengchi University Data Structures Lecture 7 Recap We have talked about object oriented programing Chapter 1, 2,

More information

Entry and Priority Queue ADTs

Entry and Priority Queue ADTs Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser, Wiley, 2014 Adaptable Priority Queues 3 a 5 g 4 e Adaptable

More information

Lecture 13: AVL Trees and Binary Heaps

Lecture 13: AVL Trees and Binary Heaps Data Structures Brett Bernstein Lecture 13: AVL Trees and Binary Heaps Review Exercises 1. ( ) Interview question: Given an array show how to shue it randomly so that any possible reordering is equally

More information

Programming II (CS300)

Programming II (CS300) 1 Programming II (CS300) Chapter 12: Heaps and Priority Queues MOUNA KACEM mouna@cs.wisc.edu Fall 2018 Heaps and Priority Queues 2 Priority Queues Heaps Priority Queue 3 QueueADT Objects are added and

More information

Heaps Goodrich, Tamassia. Heaps 1

Heaps Goodrich, Tamassia. Heaps 1 Heaps Heaps 1 Recall Priority Queue ADT A priority queue stores a collection of entries Each entry is a pair (key, value) Main methods of the Priority Queue ADT insert(k, x) inserts an entry with key k

More information

Data Structures Question Bank Multiple Choice

Data Structures Question Bank Multiple Choice Section 1. Fundamentals: Complexity, Algorthm Analysis 1. An algorithm solves A single problem or function Multiple problems or functions Has a single programming language implementation 2. A solution

More information

Priority Queues. Reading: 7.1, 7.2

Priority Queues. Reading: 7.1, 7.2 Priority Queues Reading: 7.1, 7.2 Generalized sorting Sometimes we need to sort but The data type is not easily mapped onto data we can compare (numbers, strings, etc) The sorting criteria changes depending

More information

Chapter 5 Data Structures Algorithm Theory WS 2016/17 Fabian Kuhn

Chapter 5 Data Structures Algorithm Theory WS 2016/17 Fabian Kuhn Chapter 5 Data Structures Algorithm Theory WS 06/ Fabian Kuhn Examples Dictionary: Operations: insert(key,value), delete(key), find(key) Implementations: Linked list: all operations take O(n) time (n:

More information

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

Priority Queue. How to implement a Priority Queue? queue. priority queue Priority Queue cs cs cs0 cs cs cs queue cs cs cs cs0 cs cs cs cs cs0 cs cs cs Reading LDC Ch 8 priority queue cs0 cs cs cs cs cs How to implement a Priority Queue? Keep them sorted! (Haven t we implemented

More information

Summer Final Exam Review Session August 5, 2009

Summer Final Exam Review Session August 5, 2009 15-111 Summer 2 2009 Final Exam Review Session August 5, 2009 Exam Notes The exam is from 10:30 to 1:30 PM in Wean Hall 5419A. The exam will be primarily conceptual. The major emphasis is on understanding

More information

Priority Queues. 1 Introduction. 2 Naïve Implementations. CSci 335 Software Design and Analysis III Chapter 6 Priority Queues. Prof.

Priority Queues. 1 Introduction. 2 Naïve Implementations. CSci 335 Software Design and Analysis III Chapter 6 Priority Queues. Prof. Priority Queues 1 Introduction Many applications require a special type of queuing in which items are pushed onto the queue by order of arrival, but removed from the queue based on some other priority

More information

COMP Data Structures

COMP Data Structures COMP 2140 - Data Structures Shahin Kamali Topic 5 - Sorting University of Manitoba Based on notes by S. Durocher. COMP 2140 - Data Structures 1 / 55 Overview Review: Insertion Sort Merge Sort Quicksort

More information

Lecture Notes. char myarray [ ] = {0, 0, 0, 0, 0 } ; The memory diagram associated with the array can be drawn like this

Lecture Notes. char myarray [ ] = {0, 0, 0, 0, 0 } ; The memory diagram associated with the array can be drawn like this Lecture Notes Array Review An array in C++ is a contiguous block of memory. Since a char is 1 byte, then an array of 5 chars is 5 bytes. For example, if you execute the following C++ code you will allocate

More information

CS2 Algorithms and Data Structures Note 6

CS2 Algorithms and Data Structures Note 6 CS Algorithms and Data Structures Note 6 Priority Queues and Heaps In this lecture, we will discuss another important ADT: PriorityQueue. Like stacks and queues, priority queues store arbitrary collections

More information

Priority Queues 1 / 15

Priority Queues 1 / 15 Priority Queues 1 / 15 Outline 1 API 2 Elementary Implementations 3 Heap Definitions 4 Algorithms on Heaps 5 Heap Sort 6 Heap Sort and Other Sorts 2 / 15 API Many applications require that we process items

More information

Heaps. Heaps Priority Queue Revisit HeapSort

Heaps. Heaps Priority Queue Revisit HeapSort Heaps Heaps Priority Queue Revisit HeapSort Heaps A heap is a complete binary tree in which the nodes are organized based on their data values. For each non- leaf node V, max- heap: the value in V is greater

More information

CS2 Algorithms and Data Structures Note 6

CS2 Algorithms and Data Structures Note 6 CS Algorithms and Data Structures Note 6 Priority Queues and Heaps In this lecture, we will discuss priority queues, another important ADT. As stacks and queues, priority queues store arbitrary collections

More information

COMP 250 Midterm #2 March 11 th 2013

COMP 250 Midterm #2 March 11 th 2013 NAME: STUDENT ID: COMP 250 Midterm #2 March 11 th 2013 - This exam has 6 pages - This is an open book and open notes exam. No electronic equipment is allowed. 1) Questions with short answers (28 points;

More information

Heaps. Heaps. A heap is a complete binary tree.

Heaps. Heaps. A heap is a complete binary tree. A heap is a complete binary tree. 1 A max-heap is a complete binary tree in which the value in each internal node is greater than or equal to the values in the children of that node. A min-heap is defined

More information

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

Definition of a Heap. Heaps. Priority Queues. Example. Implementation using a heap. Heap ADT Heaps Definition of a heap What are they for: priority queues Insertion and deletion into heaps Implementation of heaps Heap sort Not to be confused with: heap as the portion of computer memory available

More information

Programming Abstractions

Programming Abstractions Programming Abstractions C S 1 0 6 B Cynthia Lee Topics: Priority Queue Linked List implementation Sorted Unsorted Heap structure implementation TODAY S TOPICS NOT ON THE MIDTERM 2 Some priority queue

More information

Stacks (5.1) Abstract Data Types (ADTs) CSE 2011 Winter 2011

Stacks (5.1) Abstract Data Types (ADTs) CSE 2011 Winter 2011 Stacks (5.1) CSE 2011 Winter 2011 26 January 2011 1 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations on the data Error

More information

EXAMINATIONS 2015 COMP103 INTRODUCTION TO DATA STRUCTURES AND ALGORITHMS

EXAMINATIONS 2015 COMP103 INTRODUCTION TO DATA STRUCTURES AND ALGORITHMS T E W H A R E W Ā N A N G A O T E Student ID:....................... Ū P O K O O T E I K A A M Ā U I VUW VICTORIA U N I V E R S I T Y O F W E L L I N G T O N EXAMINATIONS 2015 TRIMESTER 2 COMP103 INTRODUCTION

More information

CMSC351 - Fall 2014, Homework #2

CMSC351 - Fall 2014, Homework #2 CMSC351 - Fall 2014, Homework #2 Due: October 8th at the start of class Name: Section: Grades depend on neatness and clarity. Write your answers with enough detail about your approach and concepts used,

More information

9/26/2018 Data Structure & Algorithm. Assignment04: 3 parts Quiz: recursion, insertionsort, trees Basic concept: Linked-List Priority queues Heaps

9/26/2018 Data Structure & Algorithm. Assignment04: 3 parts Quiz: recursion, insertionsort, trees Basic concept: Linked-List Priority queues Heaps 9/26/2018 Data Structure & Algorithm Assignment04: 3 parts Quiz: recursion, insertionsort, trees Basic concept: Linked-List Priority queues Heaps 1 Quiz 10 points (as stated in the first class meeting)

More information

Stacks and Queues

Stacks and Queues Stacks and Queues 2-25-2009 1 Opening Discussion Let's look at solutions to the interclass problem. Do you have any questions about the reading? Do you have any questions about the assignment? Minute Essays

More information

CH ALGORITHM ANALYSIS CH6. STACKS, QUEUES, AND DEQUES

CH ALGORITHM ANALYSIS CH6. STACKS, QUEUES, AND DEQUES CH4.2-4.3. ALGORITHM ANALYSIS CH6. STACKS, QUEUES, AND DEQUES ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN JAVA, GOODRICH, TAMASSIA AND GOLDWASSER

More information

Priority Queues and Binary Heaps

Priority Queues and Binary Heaps Yufei Tao ITEE University of Queensland In this lecture, we will learn our first tree data structure called the binary heap which serves as an implementation of the priority queue. Priority Queue A priority

More information

Data Structures in Java

Data Structures in Java Data Structures in Java Lecture 9: Binary Search Trees. 10/7/015 Daniel Bauer 1 Contents 1. Binary Search Trees. Implementing Maps with BSTs Map ADT A map is collection of (key, value) pairs. Keys are

More information

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

Data Structures. Giri Narasimhan Office: ECS 254A Phone: x-3748 Data Structures Giri Narasimhan Office: ECS 254A Phone: x-3748 giri@cs.fiu.edu Motivation u Many applications where Items have associated priorities Job scheduling Long print jobs vs short ones; OS jobs

More information

PRIORITY QUEUES AND HEAPS

PRIORITY QUEUES AND HEAPS PRIORITY QUEUES AND HEAPS Lecture 17 CS2110 Spring 201 Readings and Homework 2 Read Chapter 2 A Heap Implementation to learn about heaps Exercise: Salespeople often make matrices that show all the great

More information

Algorithm Theory, Winter Term 2015/16 Problem Set 5 - Sample Solution

Algorithm Theory, Winter Term 2015/16 Problem Set 5 - Sample Solution Albert-Ludwigs-Universität, Inst. für Informatik Prof. Dr. Fabian Kuhn M. Ahmadi, O. Saukh, A. R. Molla November, 20 Algorithm Theory, Winter Term 20/6 Problem Set - Sample Solution Exercise : Amortized

More information

CS 216 Exam 1 Fall SOLUTION

CS 216 Exam 1 Fall SOLUTION CS 216 Exam 1 Fall 2004 - SOLUTION Name: Lab Section: Email Address: Student ID # This exam is closed note, closed book. You will have an hour and fifty minutes total to complete the exam. You may NOT

More information

Priority Queues and Heaps. Heaps and Priority Queues 1

Priority Queues and Heaps. Heaps and Priority Queues 1 Priority Queues and Heaps 2 5 6 9 7 Heaps and Priority Queues 1 Priority Queue ADT A priority queue stores a collection of items An item is a pair (key, element) Main methods of the Priority Queue ADT

More information

Priority Queues and Heaps. Heaps of fun, for everyone!

Priority Queues and Heaps. Heaps of fun, for everyone! Priority Queues and Heaps Heaps of fun, for everyone! Learning Goals After this unit, you should be able to... Provide examples of appropriate applications for priority queues and heaps Manipulate data

More information

CS 231 Data Structures and Algorithms Fall Binary Search Trees Lecture 23 October 29, Prof. Zadia Codabux

CS 231 Data Structures and Algorithms Fall Binary Search Trees Lecture 23 October 29, Prof. Zadia Codabux CS 231 Data Structures and Algorithms Fall 2018 Binary Search Trees Lecture 23 October 29, 2018 Prof. Zadia Codabux 1 Agenda Ternary Operator Binary Search Tree Node based implementation Complexity 2 Administrative

More information

INFO1x05 Tutorial 09

INFO1x05 Tutorial 09 INFO1x05 Tutorial 09 (2,3) Trees, (2,4) Trees and Hash Tables Exercise 1: (INFO1105 and INFO1905) Is the search tree shown below a (2,4) tree? Why or why not? 22 5 10 25 3 4 6 8 14 23 24 27 11 13 17 Figure

More information

Priority Queues (Heaps)

Priority Queues (Heaps) Priority Queues (Heaps) 1 Priority Queues Many applications require that we process records with keys in order, but not necessarily in full sorted order. Often we collect a set of items and process the

More information

Heaps. 2/13/2006 Heaps 1

Heaps. 2/13/2006 Heaps 1 Heaps /13/00 Heaps 1 Outline and Reading What is a heap ( 8.3.1) Height of a heap ( 8.3.) Insertion ( 8.3.3) Removal ( 8.3.3) Heap-sort ( 8.3.) Arraylist-based implementation ( 8.3.) Bottom-up construction

More information

Data Structures Brett Bernstein

Data Structures Brett Bernstein Data Structures Brett Bernstein Final Review 1. Consider a binary tree of height k. (a) What is the maximum number of nodes? (b) What is the maximum number of leaves? (c) What is the minimum number of

More information

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

Priority Queues. e.g. jobs sent to a printer, Operating system job scheduler in a multi-user environment. Simulation environments Heaps 1 Priority Queues Many applications require that we process records with keys in order, but not necessarily in full sorted order. Often we collect a set of items and process the one with the current

More information

MID TERM MEGA FILE SOLVED BY VU HELPER Which one of the following statement is NOT correct.

MID TERM MEGA FILE SOLVED BY VU HELPER Which one of the following statement is NOT correct. MID TERM MEGA FILE SOLVED BY VU HELPER Which one of the following statement is NOT correct. In linked list the elements are necessarily to be contiguous In linked list the elements may locate at far positions

More information

Week 6. Data structures

Week 6. Data structures 1 2 3 4 5 n Week 6 Data structures 6 7 n 8 General remarks We start considering Part III Data Structures from CLRS. As a first example we consider two special of buffers, namely stacks and queues. Reading

More information

Lecture 23, Fall 2018 Monday October 29

Lecture 23, Fall 2018 Monday October 29 Binary search trees Oliver W. Layton CS231: Data Structures and Algorithms Lecture 23, Fall 2018 Monday October 29 Plan Tree traversals Binary search trees Code up binary tree Pre-order traversal 1. Process

More information

COSC 2007 Data Structures II Final Exam. Part 1: multiple choice (1 mark each, total 30 marks, circle the correct answer)

COSC 2007 Data Structures II Final Exam. Part 1: multiple choice (1 mark each, total 30 marks, circle the correct answer) COSC 2007 Data Structures II Final Exam Thursday, April 13 th, 2006 This is a closed book and closed notes exam. There are total 3 parts. Please answer the questions in the provided space and use back

More information

FINAL EXAMINATION. COMP-250: Introduction to Computer Science - Fall 2011

FINAL EXAMINATION. COMP-250: Introduction to Computer Science - Fall 2011 STUDENT NAME: STUDENT ID: McGill University Faculty of Science School of Computer Science FINAL EXAMINATION COMP-250: Introduction to Computer Science - Fall 2011 December 13, 2011 2:00-5:00 Examiner:

More information

Computer Science 136 Exam 2

Computer Science 136 Exam 2 Computer Science 136 Exam 2 Sample exam Show all work. No credit will be given if necessary steps are not shown or for illegible answers. Partial credit for partial answers. Be clear and concise. Write

More information

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

Abstract vs concrete data structures HEAPS AND PRIORITY QUEUES. Abstract vs concrete data structures. Concrete Data Types. Concrete data structures 10/1/17 Abstract vs concrete data structures 2 Abstract data structures are interfaces they specify only interface (method names and specs) not implementation (method bodies, fields, ) HEAPS AND PRIORITY

More information

Binary heaps (chapters ) Leftist heaps

Binary heaps (chapters ) Leftist heaps Binary heaps (chapters 20.3 20.5) Leftist heaps Binary heaps are arrays! A binary heap is really implemented using an array! 8 18 29 20 28 39 66 Possible because of completeness property 37 26 76 32 74

More information

COP 3502 (Computer Science I) Test #3: Data Structures Date: 11/1/2013 VERSION A

COP 3502 (Computer Science I) Test #3: Data Structures Date: 11/1/2013 VERSION A COP 3502 (Computer Science I) Test #3: Data Structures Date: 11/1/2013 VERSION A Directions: This is a multiple choice test. Each question is worth 5 points. Please choose the most accurate answer listed.

More information

Tables, Priority Queues, Heaps

Tables, Priority Queues, Heaps Tables, Priority Queues, Heaps Table ADT purpose, implementations Priority Queue ADT variation on Table ADT Heaps purpose, implementation heapsort EECS 268 Programming II 1 Table ADT A table in generic

More information

CS350: Data Structures Heaps and Priority Queues

CS350: Data Structures Heaps and Priority Queues Heaps and Priority Queues James Moscola Department of Engineering & Computer Science York College of Pennsylvania James Moscola Priority Queue An abstract data type of a queue that associates a priority

More information

CMPSCI 187: Programming With Data Structures. Lecture #28: Binary Search Trees 21 November 2011

CMPSCI 187: Programming With Data Structures. Lecture #28: Binary Search Trees 21 November 2011 CMPSCI 187: Programming With Data Structures Lecture #28: Binary Search Trees 21 November 2011 Binary Search Trees The Idea of a Binary Search Tree The BinarySearchTreeADT Interface The LinkedBinarySearchTree

More information

ITI Introduction to Computing II

ITI Introduction to Computing II ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science Binary search tree (part I) Version of March 24, 2013 Abstract These lecture notes are meant

More information

Data Structures and Algorithms

Data Structures and Algorithms Berner Fachhochschule - Technik und Informatik Data Structures and Algorithms Heaps and Priority Queues Philipp Locher FS 2018 Heaps and Priority Queues Page 1 Outline Heaps Heap-Sort Priority Queues Heaps

More information

3137 Data Structures and Algorithms in C++

3137 Data Structures and Algorithms in C++ 3137 Data Structures and Algorithms in C++ Lecture 3 July 12 2006 Shlomo Hershkop 1 Announcements Homework 2 out tonight Please make sure you complete hw1 asap if you have issues, please contact me will

More information

CSCI-1200 Data Structures Fall 2018 Lecture 23 Priority Queues II

CSCI-1200 Data Structures Fall 2018 Lecture 23 Priority Queues II Review from Lecture 22 CSCI-1200 Data Structures Fall 2018 Lecture 23 Priority Queues II Using STL s for_each, Function Objects, a.k.a., Functors STL s unordered_set (and unordered_map) Hash functions

More information

THE UNIVERSITY OF WESTERN AUSTRALIA

THE UNIVERSITY OF WESTERN AUSTRALIA THE UNIVERSITY OF WESTERN AUSTRALIA MID SEMESTER EXAMINATION April 2016 SCHOOL OF COMPUTER SCIENCE & SOFTWARE ENGINEERING DATA STRUCTURES AND ALGORITHMS CITS2200 This Paper Contains: 6 Pages 10 Questions

More information

FINAL EXAMINATION. COMP-250: Introduction to Computer Science - Fall 2011

FINAL EXAMINATION. COMP-250: Introduction to Computer Science - Fall 2011 STUDENT NAME: STUDENT ID: McGill University Faculty of Science School of Computer Science FINAL EXAMINATION COMP-250: Introduction to Computer Science - Fall 2011 December 13, 2011 2:00-5:00 Examiner:

More information

Elementary Data Structures 2

Elementary Data Structures 2 Elementary Data Structures Priority Queues, & Dictionaries Priority Queues Sell 00 IBM $ Sell 300 IBM $0 Buy 00 IBM $9 Buy 400 IBM $8 Priority Queue ADT A priority queue stores a collection of items An

More information

Matriculation number:

Matriculation number: Department of Informatics Prof. Dr. Michael Böhlen Binzmühlestrasse 14 8050 Zurich Phone: +41 44 635 4333 Email: boehlen@ifi.uzh.ch AlgoDat Repetition Exam Spring 2018 18.05.2018 Name: Matriculation number:

More information

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

The heap is essentially an array-based binary tree with either the biggest or smallest element at the root. The heap is essentially an array-based binary tree with either the biggest or smallest element at the root. Every parent in a Heap will always be smaller or larger than both of its children. This rule

More information

PRIORITY QUEUES AND HEAPS

PRIORITY QUEUES AND HEAPS 10//1 Readings and Homework Read Chapter A Heap Implementation to learn about heaps PRIORITY QUEUES AND HEAPS Lecture 17 CS10 Fall 01 Exercise: Salespeople often make matrices that show all the great features

More information

Basic Data Structures

Basic Data Structures Basic Data Structures Some Java Preliminaries Generics (aka parametrized types) is a Java mechanism that enables the implementation of collection ADTs that can store any type of data Stack s1

More information

ITI Introduction to Computing II

ITI Introduction to Computing II ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science Binary search tree (part I) Version of March 24, 2013 Abstract These lecture notes are meant

More information

Priority Queues Heaps Heapsort

Priority Queues Heaps Heapsort Priority Queues Heaps Heapsort Complete the Doublets partner(s) evaluation by tonight. Use your individual log to give them useful feedback! Like 230 and have workstudy funding? We are looking for CSSE230

More information

Binary Trees, Binary Search Trees

Binary Trees, Binary Search Trees Binary Trees, Binary Search Trees Trees Linear access time of linked lists is prohibitive Does there exist any simple data structure for which the running time of most operations (search, insert, delete)

More information

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

Binary Search Trees. BinaryTree<E> Class (cont.) Section /27/2017 Binary Search Trees Section.4 BinaryTree Class (cont.) public class BinaryTree { // Data members/fields...just the root is needed - Node root; // Constructor(s) + BinaryTree() + BinaryTree(Node

More information

! 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 Trees (& Heaps) Week 12 Gaddis: 20 Weiss: 21.1-3 CS 5301 Spring 2015 Jill Seaman 1 Tree: non-recursive definition! Tree: set of nodes and directed edges - root: one node is distinguished as the root -

More information

COS 226 Algorithms and Data Structures Spring Midterm

COS 226 Algorithms and Data Structures Spring Midterm COS 226 Algorithms and Data Structures Spring 2018 Midterm This exam has 8 questions (including question 0) worth a total of 80 points. You have 80 minutes. This exam is preprocessed by a computer, so

More information

Faculty of Science FINAL EXAMINATION COMP-250 A Introduction to Computer Science School of Computer Science, McGill University

Faculty of Science FINAL EXAMINATION COMP-250 A Introduction to Computer Science School of Computer Science, McGill University NAME: STUDENT NUMBER:. Faculty of Science FINAL EXAMINATION COMP-250 A Introduction to Computer Science School of Computer Science, McGill University Examimer: Prof. Mathieu Blanchette December 8 th 2005,

More information