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

Size: px
Start display at page:

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

Transcription

1 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 a 4 add first(e) 5 { 6 A. push(e) ; 7 } 8 remove first(e) 9 { 10 A. pop() 11 } 12 add last(e) 13 { 14 B.push(e) ; 15 } 16 remove last () 17 { 18 B.pop() ; 19 } Running Time=constant Problems 1

2 Let T be a tree with n positions. Define the lowest common ancestor(lca) between two positions p and q as the lowest point in T that has both p and q as descendants. Given two positions p and q, describe an efficient algorithm for finding the LCA of p and q. What is the running time of your algorithm? Problems 2

3 Priority Queues and Readings - Chapter 9

4 Design ADT Question! Scheduler for simulated CPU Jobs " each job has a priority " from among all jobs waiting to be processed in a time slice, the CPU must work on a job with highest priority " each job has a unique id and an attribute length that indicates the time required to process the job " once a job is scheduled, it runs for the required amount of time " when a job finishes we should remove it from our collection " jobs can be submitted to the scheduler at any point in time Prority Queues 4

5 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, v) inserts an entry with key k and value v " removemin() removes and returns the entry with smallest key, or null if the the priority queue is empty! Additional methods " min() returns, but does not remove, an entry with smallest key, or null if the the priority queue is empty " size(), isempty() 2014 Goodrich, Tamassia, Goldwasser Prority Queues 5

6 Example! A sequence of priority queue methods: 2014 Goodrich, Tamassia, Goldwasser Prority Queues 6

7 Total Order Relations! Keys in a priority queue can be arbitrary objects on which an order is defined! Two distinct entries in a priority queue can have the same key! Mathematical concept of total order relation " Comparability property: either x y or y x " Antisymmetric property: x y and y x x = y " Transitive property: x y and y z x z 2014 Goodrich, Tamassia, Goldwasser Prority Queues 7

8 Entry ADT! An entry in a priority queue is simply a keyvalue pair! Priority queues store entries to allow for efficient insertion and removal based on keys! Methods: " getkey: returns the key for this entry! As a Java interface: /** * Interface for a key-value * pair entry **/ public interface Entry<K,V> { } K getkey(); V getvalue(); " getvalue: returns the value associated with this entry 2014 Goodrich, Tamassia, Goldwasser Prority Queues 8

9 Comparator ADT! A comparator encapsulates the action of comparing two objects according to a given total order relation! The comparator is external to the keys being compared! When the priority queue needs to compare two keys, it uses its comparator! Primary method of the Comparator ADT! compare(x, y): returns an integer i such that " i < 0 if a < b, " i = 0 if a = b " i > 0 if a > b " An error occurs if a and b cannot be compared Goodrich, Tamassia, Goldwasser Prority Queues 9

10 Example Comparator! Lexicographic comparison of 2-D points: /** Comparator for 2D points under the standard lexicographic order. */ public class Lexicographic implements Comparator { int xa, ya, xb, yb; public int compare(object a, Object b) throws ClassCastException { xa = ((Point2D) a).getx(); ya = ((Point2D) a).gety(); xb = ((Point2D) b).getx(); yb = ((Point2D) b).gety(); if (xa!= xb) return (xb - xa); else return (yb - ya); } }! Point objects: /** Class representing a point in the plane with integer coordinates */ public class Point2D { protected int xc, yc; // coordinates public Point2D(int x, int y) { xc = x; yc = y; } public int getx() { return xc; } public int gety() { return yc; } } 2014 Goodrich, Tamassia, Goldwasser Prority Queues 10

11 Sequence-based Priority Queue! Implementation with an unsorted list! Implementation with a sorted list ! Performance: " insert takes O(1) time since we can insert the item at the beginning or end of the sequence " removemin and min take O(n) time since we have to traverse the entire sequence to find the smallest key! Performance: " insert takes O(n) time since we have to find the place where to insert the item " removemin and min take O(1) time, since the smallest key is at the beginning 2014 Goodrich, Tamassia, Goldwasser Prority Queues 11

12 Better Implementation of Priority Queue?

13

14 ! A heap is a binary tree storing keys at its nodes and satisfying the following properties:! Heap-Order: for every internal node v other than the root, key(v) key(parent(v))! Complete Binary Tree: let h be the height of the heap " for i = 0,, h 1, there are 2 i nodes of depth i " at depth h 1, the internal nodes are to the left of the external nodes! The last node of a heap is the rightmost node of maximum depth heap property 9 Structural property last node 2014 Goodrich, Tamassia, Goldwasser 14

15 Example of non-! Heap property violation! Structural property violation

16 Finding the minimum element! The element with the smallest key value always sits at the root of the heap. " If it is elsewhere, it would have a parent with a larger key, thus violating the heap property. " hence finding the minimum can be done in O(1) time 16

17 Height of a Heap! Theorem: A heap storing n keys has height O(log n) Proof: (we apply the complete binary tree property) " Let h be the height of a heap storing n keys " Since there are 2 i keys at depth i = 0,, h 1 and at least one key at depth h, we have n h " Thus, n 2 h, i.e., h log n depth 0 1 h 1 h keys h Goodrich, Tamassia, Goldwasser 17

18 Array-based Heap Implementation! We can represent a heap with n keys by means of an array of length n! For the node at index i " the left child is at index 2i + 1 " the right child is at index 2i + 2 " parent(i) is floor((i-1)/2)! Operation add corresponds to inserting at index n + 1! Operation remove_min corresponds to removing at index n

19 and Priority Queues! We can use a heap to implement a priority queue! We store a (key, element) item at each internal node! We keep track of the position of the last node (2, Sue) (5, Pat) (6, Mark) (9, Jeff) (7, Anna) 2014 Goodrich, Tamassia, Goldwasser 19

20 Insertion into a Heap! Method insertitem of the priority queue ADT corresponds to the insertion of a key k to the heap! The insertion algorithm consists of three steps " Find the insertion node z (the new last node) " Store k at z " Restore the heap-order property (discussed next) (4,C) (5,A) (6,Z) (2, T) (15,K) (9,F) (7,Q) (20,B) (16,X) (25,J) (14,E) (12,H) (11,S) (13,W) 20

21 Insertion into a Heap! Method insertitem of the priority queue ADT corresponds to the insertion of a key k to the heap! The insertion algorithm consists of three steps " Find the insertion node z (the new last node) " Store k at z " Restore the heap-order property (discussed next) (4,C) (5,A) (6,Z) (2, T) (15,K) (9,F) (7,Q) (20,B) (16,X) (25,J) (14,E) (12,H) (11,S) (13,W) (2,T) 21

22 Upheap! After the insertion of a new key k, the heap-order property may be violated! Algorithm upheap restores the heap-order property by swapping k along an upward path from the insertion node! Upheap terminates when the key k reaches the root or a node whose parent has a key smaller than or equal to k! Since a heap has height O(log n), upheap runs in O(log n) time (4,C) (5,A) (6,Z) (15,K) (9,F) (7,Q) (20,B) (16,X) (25,J) (14,E) (12,H) (11,S) (13,W) (2,T) 22

23 Upheap! After the insertion of a new key k, the heap-order property may be violated! Algorithm upheap restores the heap-order property by swapping k along an upward path from the insertion node! Upheap terminates when the key k reaches the root or a node whose parent has a key smaller than or equal to k! Since a heap has height O(log n), upheap runs in O(log n) time (4,C) (5,A) (6,Z) (15,K) (9,F) (7,Q) (20,B) (2,T) (16,X) (25,J) (14,E) (12,H) (11,S) (13,W) 23

24 Upheap! After the insertion of a new key k, the heap-order property may be violated! Algorithm upheap restores the heap-order property by swapping k along an upward path from the insertion node! Upheap terminates when the key k reaches the root or a node whose parent has a key smaller than or equal to k! Since a heap has height O(log n), upheap runs in O(log n) time (4,C) (5,A) (6,Z) (15,K) (9,F) (7,Q) (2,T) (16,X) (25,J) (14,E) (12,H) (11,S) (13,W) (20,B) 24

25 Upheap! After the insertion of a new key k, the heap-order property may be violated! Algorithm upheap restores the heap-order property by swapping k along an upward path from the insertion node! Upheap terminates when the key k reaches the root or a node whose parent has a key smaller than or equal to k! Since a heap has height O(log n), upheap runs in O(log n) time (4,C) (5,A) (2,T) (15,K) (9,F) (7,Q) (6,Z) (16,X) (25,J) (14,E) (12,H) (11,S) (13,W) (20,B) 25

26 Upheap! After the insertion of a new key k, the heap-order property may be violated! Algorithm upheap restores the heap-order property by swapping k along an upward path from the insertion node! Upheap terminates when the key k reaches the root or a node whose parent has a key smaller than or equal to k! Since a heap has height O(log n), upheap runs in O(log n) time (4,C) (5,A) (2,T) (15,K) (9,F) (7,Q) (6,Z) (16,X) (25,J) (14,E) (12,H) (11,S) (13,W) (20,B) 26

27 Upheap! After the insertion of a new key k, the heap-order property may be violated! Algorithm upheap restores the heap-order property by swapping k along an upward path from the insertion node! Upheap terminates when the key k reaches the root or a node whose parent has a key smaller than or equal to k! Since a heap has height O(log n), upheap runs in O(log n) time (2,T) (5,A) (4,C) (15,K) (9,F) (7,Q) (6,Z) (16,X) (25,J) (14,E) (12,H) (11,S) (13,W) (20,B) 27

28 Upheap! After the insertion of a new key k, the heap-order property may be violated! Algorithm upheap restores the heap-order property by swapping k along an upward path from the insertion node! Upheap terminates when the key k reaches the root or a node whose parent has a key smaller than or equal to k! Since a heap has height O(log n), upheap runs in O(log n) time (2,T) (5,A) (4,C) (15,K) (9,F) (7,Q) (6,Z) (16,X) (25,J) (14,E) (12,H) (11,S) (13,W) (20,B) 28

29 Another View of Insertion! Enlarge heap! Consider path from root to inserted node! Find topmost element on this path with higher priority that of inserted element! Insert new element at this location by shifting down the other elements on the path (4,C) (2,T) (5,A) (6,Z) (5,A) (4,C) (15,K) (9,F) (7,Q) (20,B) (15,K) (9,F) (7,Q) (6,Z) (16,X) (25,J) (14,E) (12,H) (11,S) (13,W) (2,T) (16,X) (25,J) (14,E) (12,H) (11,S) (13,W) (20,B) 29

30 Correctness of Upheap! The only nodes whose contents change are the ones on the path! Heap property may be violated only for children on these nodes! But new contents of these nodes only have lower priority! So heap property is not violated (4,C) (5,A) (6,Z) (15,K) (9,F) (7,Q) (20,B) (16,X) (25,J) (14,E) (12,H) (11,S) (13,W) (2,T) 30

31 Removal from a Heap! Method removemin of the priority queue ADT corresponds removing the root key from the heap! The removal algorithm consists of three steps " Replace the root key with the key of the last node w " Remove w " Restore the heap-order property (discussed next) (4,C) (5,A) (6,Z) (4, C) (15,K) (9,F) (7,Q) (20,B) (16,X) (25,J) (14,E) (12,H) (11,S) (13,W) 31

32 Removal from a Heap! Method removemin of the priority queue ADT corresponds removing the root key from the heap! The removal algorithm consists of three steps " Replace the root key with the key of the last node w " Remove w " Restore the heap-order property (discussed next) (4,C) (13,W) (5,A) (6,Z) (15,K) (9,F) (7,Q) (20,B) (16,X) (25,J) (14,E) (12,H) (11,S) 32

33 Downheap! After replacing the root key with the key k of the last node, the heap-order property may be violated! Algorithm downheap restores the heap-order property by swapping key k along a downward path from the root! Upheap terminates when key k reaches a leaf or a node whose children have keys greater than or equal to k! Since a heap has height O(log n), downheap runs in O(log n) time (13,W) (5,A) (6,Z) (15,K) (9,F) (7,Q) (20,B) (16,X) (25,J) (14,E) (12,H) (11,S) 33

34 Downheap! After replacing the root key with the key k of the last node, the heap-order property may be violated! Algorithm downheap restores the heap-order property by swapping key k along a downward path from the root! Upheap terminates when key k reaches a leaf or a node whose children have keys greater than or equal to k! Since a heap has height O(log n), downheap runs in O(log n) time (13,W) (5,A) (6,Z) (15,K) (9,F) (7,Q) (20,B) (16,X) (25,J) (14,E) (12,H) (11,S) 34

35 Downheap! After replacing the root key with the key k of the last node, the heap-order property may be violated! Algorithm downheap restores the heap-order property by swapping key k along a downward path from the root! Upheap terminates when key k reaches a leaf or a node whose children have keys greater than or equal to k! Since a heap has height O(log n), downheap runs in O(log n) time (5,A) (13,W) (6,Z) (15,K) (9,F) (7,Q) (20,B) (16,X) (25,J) (14,E) (12,H) (11,S) 35

36 Downheap! After replacing the root key with the key k of the last node, the heap-order property may be violated! Algorithm downheap restores the heap-order property by swapping key k along a downward path from the root! Upheap terminates when key k reaches a leaf or a node whose children have keys greater than or equal to k! Since a heap has height O(log n), downheap runs in O(log n) time (5,A) (9,F) (6,Z) (15,K) (13,W) (7,Q) (20,B) (16,X) (25,J) (14,E) (12,H) (11,S) 36

37 Downheap! After replacing the root key with the key k of the last node, the heap-order property may be violated! Algorithm downheap restores the heap-order property by swapping key k along a downward path from the root! Upheap terminates when key k reaches a leaf or a node whose children have keys greater than or equal to k! Since a heap has height O(log n), downheap runs in O(log n) time (5,A) (9,F) (6,Z) (15,K) (13,W) (7,Q) (20,B) (16,X) (25,J) (14,E) (12,H) (11,S) 37

38 Downheap! After replacing the root key with the key k of the last node, the heap-order property may be violated! Algorithm downheap restores the heap-order property by swapping key k along a downward path from the root! Downheap terminates when key k reaches a leaf or a node whose children have keys greater than or equal to k! Since a heap has height O(log n), downheap runs in O(log n) time (5,A) (9,F) (6,Z) (15,K) (13,W) (12,H) (7,Q) (20,B) (16,X) (25,J) (14,E) (11,S) 38

39 Downheap! After replacing the root key with the key k of the last node, the heap-order property may be violated! Algorithm downheap restores the heap-order property by swapping key k along a downward path from the root! Upheap terminates when key k reaches a leaf or a node whose children have keys greater than or equal to k! Since a heap has height O(log n), downheap runs in O(log n) time (5,A) (9,F) (6,Z) (15,K) (12,H) (7,Q) (20,B) (16,X) (25,J) (14,E) (13,W) (11,S) 39

40 Correctness of Downheap! Downheap traces a path down the tree! For a node on the path (say j) - both key(left(j)) and key(right(j)) > key(j)! All elements on path have lower key values than their siblings! All elements on this path are moved up.! Hence the heap property is not violated. (13,W) (5,A) (6,Z) (15,K) (9,F) (7,Q) (20,B) (16,X) (25,J) (14,E) (12,H) (11,S) 40

41 Run Time Analysis! heap of n nodes has height O(log n)! insertion - Upheap move the element all the way to the top " O(log n) steps in worst case! removal Downheap move the root element all the way to a leaf " O(log n) steps in worst case 41

42 Array-based Heap Implementation! We can represent a heap with n keys by means of an array of length n! For the node at index i " the left child is at index 2i + 1 " the right child is at index 2i + 2 " parent(i) is floor((i-1)/2)! Operation add corresponds to inserting at index n + 1! Operation remove_min corresponds to removing at index n

43 Building a heap! Call Upheap procedure of the heap n times. " Upheap is O(log n) " inserting first element O(log 1) " inserting second element O(log 2) ".. " inserting the nth element O(log n) " so for the n insertions log 1+ log 2+ +log n = log n! = O(nlog n) 43

44 Merging Two! We are given two heaps and a key k! We create a new heap with the root node storing k and with the two heaps as subtrees ! We perform Downheap to restore the heaporder property Goodrich, Tamassia, Goldwasser 44

45 Building a Heap Bottomup

46 Building a Heap Bottomup

47 Building a Heap Bottomup

48 Building a Heap Bottomup

49 Building a Heap Bottomup

50 Building a Heap Bottomup

51 Building a Heap Bottomup

52 Bottom-up Heap Construction! We can construct a heap storing n given keys in using a bottom-up construction with log n phases! In phase i, pairs of heaps with 2 i 1 keys are merged into heaps with 2 i+1 1 keys 2 i 1 2 i 1 2 i Goodrich, Tamassia, Goldwasser 52

53 Building a Heap Bottomup Analysis! Correctness: induction on i, all trees rooted at m > i are heaps! Running time: n calls to Downheap " Downheap O(log n) " so total running time O(nlog n)! We can provide a better bound O(n) " Idea for most of the time, Downheap works on smaller than n element heaps 53

54 Building a Heap Bottomup Analysis (2)! height of node: length of longest path from node to leaf! height of tree: height of root height h h keys h 1 2 h 54

55 Building a Heap Bottomup Analysis (3)! height of node: length of longest path from node to leaf! height of tree: height of root! time for Downheap(i) = O(height of subtree rooted at i)! Let us assume a complete binary tree " n = 2 h

56 Building a Heap Bottomup Analysis (4) 3 Total work for B 3* * * *8 56

57 Building a Heap Bottomup Analysis (5)! 0 th level - 2 h nodes, no need to call Downheap! 1 st level - 2 h-1 nodes, Downheap - 1 swap! 2 nd level - 2 h-2 nodes, Downheap - 2 swaps! j th level - 2 h-j nodes, Downheap - j swaps! h th level 1 node, Downheap h swaps! So total number of swaps is h h j2 h j = j 2h j=0 j=0 " It can be shown that, this is bound by 2 (h+1) " 2 (h+1) = n-1! Therefore O(n) h = 2 h j 2 j 2 j j=0 57

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

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

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

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

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

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

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

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

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

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

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

Priority Queue ADT ( 7.1) Heaps and Priority Queues 2. Comparator ADT ( 7.1.4) Total Order Relation. Using Comparators in C++

Priority Queue ADT ( 7.1) Heaps and Priority Queues 2. Comparator ADT ( 7.1.4) Total Order Relation. Using Comparators in C++ Heaps and Priority Queues 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 insertitem(k, o) inserts an item ith

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

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

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

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

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

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

Chapter 2: Basic Data Structures

Chapter 2: Basic Data Structures Chapter 2: Basic Data Structures Basic Data Structures Stacks Queues Vectors, Linked Lists Trees (Including Balanced Trees) Priority Queues and Heaps Dictionaries and Hash Tables Spring 2014 CS 315 2 Two

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

Sorting. Outline. Sorting with a priority queue Selection-sort Insertion-sort Heap Sort Quick Sort

Sorting. Outline. Sorting with a priority queue Selection-sort Insertion-sort Heap Sort Quick Sort Sorting Hiroaki Kobayashi 1 Outline Sorting with a priority queue Selection-sort Insertion-sort Heap Sort Quick Sort Merge Sort Lower Bound on Comparison-Based Sorting Bucket Sort and Radix Sort Hiroaki

More information

Height of a Heap. Heaps. 1. Insertion into a Heap. Heaps and Priority Queues. The insertion algorithm consists of three steps

Height of a Heap. Heaps. 1. Insertion into a Heap. Heaps and Priority Queues. The insertion algorithm consists of three steps Heaps A heap is a binary tree storing keys at its nodes and satisfying the folloing properties:! Heap-Order: " for every internal node v other than the root, key(v)! key(parent(v))! Complete Binary Tree:

More information

Algorithms and Data Structures

Algorithms and Data Structures Priority Queues and Heaps Page 1 BFH-TI: Softwareschule Schweiz Priority Queues and Heaps Dr. CAS SD01 Priority Queues and Heaps Page 2 Outline Priority Queues Heaps Heap-Based Priority Queues Priority

More information

Priority Queues and Heaps. More Data Structures. Priority Queue ADT ( 2.4.1) Total Order Relation. Sorting with a Priority Queue ( 2.4.

Priority Queues and Heaps. More Data Structures. Priority Queue ADT ( 2.4.1) Total Order Relation. Sorting with a Priority Queue ( 2.4. More Data Structures Priority Queues and Heaps Priority Queues, Comparators, Locators, Dictionaries More Data Structures v. More Data Structures v. Priority Queue ADT (.4.) Total Order Relation A priority

More information

Priority Queues & Heaps. CS16: Introduction to Data Structures & Algorithms Spring 2019

Priority Queues & Heaps. CS16: Introduction to Data Structures & Algorithms Spring 2019 Priority Queues & Heaps CS16: Introduction to Data Structures & Algorithms Spring 2019 Outline Priority Queues Motivation ADT Implementation Heaps insert( ) and upheap( ) removemin( ) and downheap( ) Motivation

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

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

9. Heap : Priority Queue

9. Heap : Priority Queue 9. Heap : Priority Queue Where We Are? Array Linked list Stack Queue Tree Binary Tree Heap Binary Search Tree Priority Queue Queue Queue operation is based on the order of arrivals of elements FIFO(First-In

More information

Sorting and Searching

Sorting and Searching Sorting and Searching Lecture 2: Priority Queues, Heaps, and Heapsort Lecture 2: Priority Queues, Heaps, and Heapsort Sorting and Searching 1 / 24 Priority Queue: Motivating Example 3 jobs have been submitted

More information

INFO1x05 Tutorial 6. Exercise 1: Heaps and Priority Queues

INFO1x05 Tutorial 6. Exercise 1: Heaps and Priority Queues 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

More information

Priority Queues. Lecture15: Heaps. Priority Queue ADT. Sequence based Priority Queue

Priority Queues. Lecture15: Heaps. Priority Queue ADT. Sequence based Priority Queue Priority Queues (0F) Lecture: Heaps Bohyung Han CSE, POSTECH bhhan@postech.ac.kr Queues Stores items (keys) in a linear list or array FIFO (First In First Out) Stored items do not have priorities. Priority

More information

Sorting and Searching

Sorting and Searching Sorting and Searching Lecture 2: Priority Queues, Heaps, and Heapsort Lecture 2: Priority Queues, Heaps, and Heapsort Sorting and Searching 1 / 24 Priority Queue: Motivating Example 3 jobs have been submitted

More information

Data Structures and Algorithms

Data Structures and Algorithms Data Structures and Algorithms Spring 2017-2018 Outline 1 Priority Queues Outline Priority Queues 1 Priority Queues Jumping the Queue Priority Queues In normal queue, the mode of selection is first in,

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

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

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

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

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

CS 240 Fall Mike Lam, Professor. Priority Queues and Heaps CS 240 Fall 2015 Mike Lam, Professor Priority Queues and Heaps Priority Queues FIFO abstract data structure w/ priorities Always remove item with highest priority Store key (priority) with value Store

More information

Heaps Outline and Required Reading: Heaps ( 7.3) COSC 2011, Fall 2003, Section A Instructor: N. Vlajic

Heaps Outline and Required Reading: Heaps ( 7.3) COSC 2011, Fall 2003, Section A Instructor: N. Vlajic 1 Heaps Outline and Required Reading: Heaps (.3) COSC 2011, Fall 2003, Section A Instructor: N. Vlajic Heap ADT 2 Heap binary tree (T) that stores a collection of keys at its internal nodes and satisfies

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

Computer Science 210 Data Structures Siena College Fall Topic Notes: Priority Queues and Heaps

Computer Science 210 Data Structures Siena College Fall Topic Notes: Priority Queues and Heaps Computer Science 0 Data Structures Siena College Fall 08 Topic Notes: Priority Queues and Heaps Heaps and Priority Queues From here, we will look at some ways that trees are used in other structures. First,

More information

(2,4) Trees Goodrich, Tamassia (2,4) Trees 1

(2,4) Trees Goodrich, Tamassia (2,4) Trees 1 (2,4) Trees 9 2 5 7 10 14 2004 Goodrich, Tamassia (2,4) Trees 1 Multi-Way Search Tree A multi-way search tree is an ordered tree such that Each internal node has at least two children and stores d -1 key-element

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

In a postorder traversal, a node is visited after its descendants Application: compute space used by files in a directory and its subdirectories 9 1

In a postorder traversal, a node is visited after its descendants Application: compute space used by files in a directory and its subdirectories 9 1 What is a Tree Trees Stock Fraud Make Money Fast! Winning Lotto / Bank Robbery In computer science, a tree is an abstract model of a hierarchical structure A tree consists of nodes ith a parent-child relation

More information

Properties of a heap (represented by an array A)

Properties of a heap (represented by an array A) Chapter 6. HeapSort Sorting Problem Input: A sequence of n numbers < a1, a2,..., an > Output: A permutation (reordering) of the input sequence such that ' ' ' < a a a > 1 2... n HeapSort O(n lg n) worst

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

Binary Heaps in Dynamic Arrays

Binary Heaps in Dynamic Arrays Yufei Tao ITEE University of Queensland We have already learned that the binary heap serves as an efficient implementation of a priority queue. Our previous discussion was based on pointers (for getting

More information

CSCI2100B Data Structures Heaps

CSCI2100B Data Structures Heaps CSCI2100B Data Structures Heaps Irwin King king@cse.cuhk.edu.hk http://www.cse.cuhk.edu.hk/~king Department of Computer Science & Engineering The Chinese University of Hong Kong Introduction In some applications,

More information

CS2223: Algorithms Sorting Algorithms, Heap Sort, Linear-time sort, Median and Order Statistics

CS2223: Algorithms Sorting Algorithms, Heap Sort, Linear-time sort, Median and Order Statistics CS2223: Algorithms Sorting Algorithms, Heap Sort, Linear-time sort, Median and Order Statistics 1 Sorting 1.1 Problem Statement You are given a sequence of n numbers < a 1, a 2,..., a n >. You need to

More information

Overview of Presentation. Heapsort. Heap Properties. What is Heap? Building a Heap. Two Basic Procedure on Heap

Overview of Presentation. Heapsort. Heap Properties. What is Heap? Building a Heap. Two Basic Procedure on Heap Heapsort Submitted by : Hardik Parikh(hjp0608) Soujanya Soni (sxs3298) Overview of Presentation Heap Definition. Adding a Node. Removing a Node. Array Implementation. Analysis What is Heap? A Heap is a

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

Heaps, Heap Sort, and Priority Queues.

Heaps, Heap Sort, and Priority Queues. Heaps, Heap Sort, and Priority Queues Sorting III / Slide 2 Background: Binary Trees Has a root at the topmost level Each node has zero, one or two children A node that has no child is called a leaf For

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

CS/COE 1501

CS/COE 1501 CS/COE 1501 www.cs.pitt.edu/~nlf4/cs1501/ Priority Queues We mentioned priority queues in building Huffman tries Primary operations they needed: Insert Find item with highest priority E.g., findmin() or

More information

COMP 250 Fall priority queues, heaps 1 Nov. 9, 2018

COMP 250 Fall priority queues, heaps 1 Nov. 9, 2018 COMP 250 Fall 2018 26 - priority queues, heaps 1 Nov. 9, 2018 Priority Queue Recall the definition of a queue. It is a collection where we remove the element that has been in the collection for the longest

More information

Topics on the Midterm

Topics on the Midterm Midterm Review Topics on the Midterm Data Structures & Object-Oriented Design Run-Time Analysis Linear Data Structures The Java Collections Framework Recursion Trees Priority Queues & Heaps Maps, Hash

More information

Module 2: Priority Queues

Module 2: Priority Queues Module 2: Priority Queues CS 240 Data Structures and Data Management T. Biedl K. Lanctot M. Sepehri S. Wild Based on lecture notes by many previous cs240 instructors David R. Cheriton School of Computer

More information

Module 2: Priority Queues

Module 2: Priority Queues Module 2: Priority Queues CS 240 Data Structures and Data Management T. Biedl K. Lanctot M. Sepehri S. Wild Based on lecture notes by many previous cs240 instructors David R. Cheriton School of Computer

More information

Algorithms and Data Structures

Algorithms and Data Structures Algorithms and Data Structures CMPSC 465 LECTURES 11-12 Priority Queues and Heaps Adam Smith 1 Priority Queue ADT Dynamic set of pairs (key, data), called elements Supports operations: MakeNewPQ() Insert(S,x)

More information

DataStruct 8. Heaps and Priority Queues

DataStruct 8. Heaps and Priority Queues 2013-2 DataStruct 8. Heaps and Priority Queues Michael T. Goodrich, et. al, Data Structures and Algorithms in C++, 2 nd Ed., John Wiley & Sons, Inc., 2011. November 13, 2013 Advanced Networking Technology

More information

Partha Sarathi Manal

Partha Sarathi Manal MA 515: Introduction to Algorithms & MA353 : Design and Analysis of Algorithms [3-0-0-6] Lecture 11 http://www.iitg.ernet.in/psm/indexing_ma353/y09/index.html Partha Sarathi Manal psm@iitg.ernet.in Dept.

More information

Round 3: Trees. Tommi Junttila. Aalto University School of Science Department of Computer Science. CS-A1140 Data Structures and Algorithms Autumn 2017

Round 3: Trees. Tommi Junttila. Aalto University School of Science Department of Computer Science. CS-A1140 Data Structures and Algorithms Autumn 2017 Round 3: Trees Tommi Junttila Aalto University School of Science Department of Computer Science CS-A1140 Data Structures and Algorithms Autumn 2017 Tommi Junttila (Aalto University) Round 3 CS-A1140 /

More information

B-Trees and External Memory

B-Trees and External Memory Presentation for use with the textbook, Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, 2015 and External Memory 1 1 (2, 4) Trees: Generalization of BSTs Each internal node

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

Dictionaries. 2/17/2006 Dictionaries 1

Dictionaries. 2/17/2006 Dictionaries 1 Dictionaries < 6 > 1 4 = 8 9 /17/006 Dictionaries 1 Outline and Reading Dictionary ADT ( 9.3) Log file ( 9.3.1) Binary search ( 9.3.3) Lookup table ( 9.3.3) Binary search tree ( 10.1) Search ( 10.1.1)

More information

10/1/2018 Data Structure & Algorithm. Circularly Linked List Doubly Linked List Priority queues Heaps

10/1/2018 Data Structure & Algorithm. Circularly Linked List Doubly Linked List Priority queues Heaps 10/1/2018 Data Structure & Algorithm Circularly Linked List Doubly Linked List Priority queues Heaps 1 Linked list: Head and Tail NULL Element Next 1. Head 2. Tail 3. Size 2 Make SinglyLinkedList implements

More information

B-Trees and External Memory

B-Trees and External Memory Presentation for use with the textbook, Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, 2015 B-Trees and External Memory 1 (2, 4) Trees: Generalization of BSTs Each internal

More information

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

Readings. Priority Queue ADT. FindMin Problem. Priority Queues & Binary Heaps. List implementation of a Priority Queue Readings Priority Queues & Binary Heaps Chapter Section.-. CSE Data Structures Winter 00 Binary Heaps FindMin Problem Quickly find the smallest (or highest priority) item in a set Applications: Operating

More information

Heap: A binary heap is a complete binary tree in which each, node other than root is smaller than its parent. Heap example: Fig 1. NPTEL IIT Guwahati

Heap: A binary heap is a complete binary tree in which each, node other than root is smaller than its parent. Heap example: Fig 1. NPTEL IIT Guwahati Heap sort is an efficient sorting algorithm with average and worst case time complexities are in O(n log n). Heap sort does not use any extra array, like merge sort. This method is based on a data structure

More information

Data Structures and Algorithms for Engineers

Data Structures and Algorithms for Engineers 04-630 Data Structures and Algorithms for Engineers David Vernon Carnegie Mellon University Africa vernon@cmu.edu www.vernon.eu Data Structures and Algorithms for Engineers 1 Carnegie Mellon University

More information

(2,4) Trees. 2/22/2006 (2,4) Trees 1

(2,4) Trees. 2/22/2006 (2,4) Trees 1 (2,4) Trees 9 2 5 7 10 14 2/22/2006 (2,4) Trees 1 Outline and Reading Multi-way search tree ( 10.4.1) Definition Search (2,4) tree ( 10.4.2) Definition Search Insertion Deletion Comparison of dictionary

More information

Binary Search Trees > = 2014 Goodrich, Tamassia, Goldwasser. Binary Search Trees 1

Binary Search Trees > = 2014 Goodrich, Tamassia, Goldwasser. Binary Search Trees 1 Binary Search Trees < > = Binary Search Trees 1 Ordered Dictionary (Map) ADT get (k): record with key k put (k,data): add record (k,data) remove (k): delete record with key k smallest(): record with smallest

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

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

The priority is indicated by a number, the lower the number - the higher the priority. CmSc 250 Intro to Algorithms Priority Queues 1. Introduction Usage of queues: in resource management: several users waiting for one and the same resource. Priority queues: some users have priority over

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

Module 2: Priority Queues

Module 2: Priority Queues Module 2: Priority Queues CS 240 - Data Structures and Data Management Sajed Haque Veronika Irvine Taylor Smith Based on lecture notes by many previous cs240 instructors David R. Cheriton School of Computer

More information

CMSC 341 Priority Queues & Heaps. Based on slides from previous iterations of this course

CMSC 341 Priority Queues & Heaps. Based on slides from previous iterations of this course CMSC 341 Priority Queues & Heaps Based on slides from previous iterations of this course Today s Topics Priority Queues Abstract Data Type Implementations of Priority Queues: Lists BSTs Heaps Heaps Properties

More information

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

Thus, it is reasonable to compare binary search trees and binary heaps as is shown in Table 1. 7.2 Binary Min-Heaps A heap is a tree-based structure, but it doesn t use the binary-search differentiation between the left and right sub-trees to create a linear ordering. Instead, a binary heap only

More information

Priority Queues, Binary Heaps, and Heapsort

Priority Queues, Binary Heaps, and Heapsort Priority Queues, Binary eaps, and eapsort Learning Goals: Provide examples of appropriate applications for priority queues and heaps. Implement and manipulate a heap using an array as the underlying data

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

Chapter 10: Search Trees

Chapter 10: Search Trees < 6 > 1 4 = 8 9 Chapter 10: Search Trees Nancy Amato Parasol Lab, Dept. CSE, Texas A&M University Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++,

More information

CMSC 341 Lecture 14: Priority Queues, Heaps

CMSC 341 Lecture 14: Priority Queues, Heaps CMSC 341 Lecture 14: Priority Queues, Heaps Prof. John Park Based on slides from previous iterations of this course Today s Topics Priority Queues Abstract Data Type Implementations of Priority Queues:

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

CSE 241 Class 17. Jeremy Buhler. October 28, Ordered collections supported both, plus total ordering operations (pred and succ)

CSE 241 Class 17. Jeremy Buhler. October 28, Ordered collections supported both, plus total ordering operations (pred and succ) CSE 241 Class 17 Jeremy Buhler October 28, 2015 And now for something completely different! 1 A New Abstract Data Type So far, we ve described ordered and unordered collections. Unordered collections didn

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

Heaps. Presentation for use with the textbook Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, 2015

Heaps. Presentation for use with the textbook Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, 2015 Presetatio for use with the textbook Algorithm Desig ad Applicatios, by M. T. Goodrich ad R. Tamassia, Wiley, 201 Heaps 201 Goodrich ad Tamassia xkcd. http://xkcd.com/83/. Tree. Used with permissio uder

More information

csci 210: Data Structures Priority Queues and Heaps

csci 210: Data Structures Priority Queues and Heaps csci 210: Data Structures Priority Queues and Heaps Summary Topics the Priority Queue ADT Priority Queue vs Dictionary and Queues implementation of PQueue linked lists binary search trees heaps Heaps READING:

More information

Learning Goals. CS221: Algorithms and Data Structures Lecture #3 Mind Your Priority Queues. Today s Outline. Back to Queues. Priority Queue ADT

Learning Goals. CS221: Algorithms and Data Structures Lecture #3 Mind Your Priority Queues. Today s Outline. Back to Queues. Priority Queue ADT CS: Algorithms and Data Structures Lecture # Mind Your Priority Queues Steve Wolfman 0W Learning Goals Provide examples of appropriate applications for priority queues. Describe efficient implementations

More information

DATA STRUCTURES AND ALGORITHMS

DATA STRUCTURES AND ALGORITHMS LECTURE 3 Babeş - Bolyai University Computer Science and Mathematics Faculty 2017-2018 In Lecture 2... Algorithm Analysis Dynamic Array Iterator Today 1 2 3 Singly Iterator An iterator is a structure that

More information

FINALTERM EXAMINATION Fall 2009 CS301- Data Structures Question No: 1 ( Marks: 1 ) - Please choose one The data of the problem is of 2GB and the hard

FINALTERM EXAMINATION Fall 2009 CS301- Data Structures Question No: 1 ( Marks: 1 ) - Please choose one The data of the problem is of 2GB and the hard FINALTERM EXAMINATION Fall 2009 CS301- Data Structures Question No: 1 The data of the problem is of 2GB and the hard disk is of 1GB capacity, to solve this problem we should Use better data structures

More information

Lecture 11: Multiway and (2,4) Trees. Courtesy to Goodrich, Tamassia and Olga Veksler

Lecture 11: Multiway and (2,4) Trees. Courtesy to Goodrich, Tamassia and Olga Veksler Lecture 11: Multiway and (2,4) Trees 9 2 5 7 10 14 Courtesy to Goodrich, Tamassia and Olga Veksler Instructor: Yuzhen Xie Outline Multiway Seach Tree: a new type of search trees: for ordered d dictionary

More information

Heapsort. Heap data structure

Heapsort. Heap data structure Heapsort Heap data structure. Heap A (not garbage-collected storage) is a nearly complete binary tree.. Height of node = # of edges on a longest simple path from the node down to a leaf.. Height of heap

More information

CS 234. Module 8. November 15, CS 234 Module 8 ADT Priority Queue 1 / 22

CS 234. Module 8. November 15, CS 234 Module 8 ADT Priority Queue 1 / 22 CS 234 Module 8 November 15, 2018 CS 234 Module 8 ADT Priority Queue 1 / 22 ADT Priority Queue Data: (key, element pairs) where keys are orderable but not necessarily distinct, and elements are any data.

More information

Topic: Heaps and priority queues

Topic: Heaps and priority queues David Keil Data Structures 8/05 1 Topic: Heaps and priority queues The priority-queue problem The heap solution Binary trees and complete binary trees Running time of heap operations Array implementation

More information

Merge Sort Goodrich, Tamassia Merge Sort 1

Merge Sort Goodrich, Tamassia Merge Sort 1 Merge Sort 7 2 9 4 2 4 7 9 7 2 2 7 9 4 4 9 7 7 2 2 9 9 4 4 2004 Goodrich, Tamassia Merge Sort 1 Review of Sorting Selection-sort: Search: search through remaining unsorted elements for min Remove: remove

More information

Data Structures and Algorithms Dr. Naveen Garg Department of Computer Science and Engineering Indian Institute of Technology, Delhi

Data Structures and Algorithms Dr. Naveen Garg Department of Computer Science and Engineering Indian Institute of Technology, Delhi Data Structures and Algorithms Dr. Naveen Garg Department of Computer Science and Engineering Indian Institute of Technology, Delhi Lecture 20 Priority Queues Today we are going to look at the priority

More information