CS61B Lecture #23. Today: Priority queues (Data Structures 6.4, 6.5) Range queries ( 6.2) Java utilities: SortedSet, Map, etc.

Similar documents
CS61B Lecture #23. Today: Priority queues (Data Structures 6.4, 6.5) Range queries ( 6.2) Java utilities: SortedSet, Map, etc.

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

CS61B Lecture #20. Today: Trees. Readings for Today: Data Structures, Chapter 5. Readings for Next Topic: Data Structures, Chapter 6

Data Structures and Algorithms

Chapter 6 Heapsort 1

CS61B Lecture #21. A Recursive Structure. Fundamental Operation: Traversal. Preorder Traversal and Prefix Expressions. (- (- (* x (+ y 3))) z)

CS350: Data Structures Heaps and Priority Queues

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

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

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

CS61B Lecture #21: Tree Searching. Last modified: Wed Oct 12 19:23: CS61B: Lecture #21 1

Priority Queues and Binary Heaps

9. Heap : Priority Queue

CMSC 341 Lecture 14: Priority Queues, Heaps

403: Algorithms and Data Structures. Heaps. Fall 2016 UAlbany Computer Science. Some slides borrowed by David Luebke

CS165: Priority Queues, Heaps

Data Structures and Algorithms

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

CS61B Lecture #20: Trees. Last modified: Mon Oct 8 21:21: CS61B: Lecture #20 1

The Heap Data Structure

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

Dictionaries. Priority Queues

Collection of priority-job pairs; priorities are comparable.

Week 10. Sorting. 1 Binary heaps. 2 Heapification. 3 Building a heap 4 HEAP-SORT. 5 Priority queues 6 QUICK-SORT. 7 Analysing QUICK-SORT.

CS S-08 Priority Queues Heaps 1

CS 361 Data Structures & Algs Lecture 9. Prof. Tom Hayes University of New Mexico

Chapter 6 Heaps. Introduction. Heap Model. Heap Implementation

Week 10. Sorting. 1 Binary heaps. 2 Heapification. 3 Building a heap 4 HEAP-SORT. 5 Priority queues 6 QUICK-SORT. 7 Analysing QUICK-SORT.

Priority queues. Priority queues. Priority queue operations

Comparisons. Θ(n 2 ) Θ(n) Sorting Revisited. So far we talked about two algorithms to sort an array of numbers. What is the advantage of merge sort?

Operations on Heap Tree The major operations required to be performed on a heap tree are Insertion, Deletion, and Merging.

So on the survey, someone mentioned they wanted to work on heaps, and someone else mentioned they wanted to work on balanced binary search trees.

Comparisons. Heaps. Heaps. Heaps. Sorting Revisited. Heaps. So far we talked about two algorithms to sort an array of numbers

Lecture 6: Analysis of Algorithms (CS )

CS61B Lecture #20: Trees. Last modified: Wed Oct 12 12:49: CS61B: Lecture #20 1

Priority Queues (Heaps)

Data Structures in Java

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

Binary Heaps. CSE 373 Data Structures Lecture 11

Programming Abstractions

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

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

Binary Heaps in Dynamic Arrays

Heap Model. specialized queue required heap (priority queue) provides at least

Topic: Heaps and priority queues

Programming II (CS300)

Properties of a heap (represented by an array A)

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

Lecture 13: AVL Trees and Binary Heaps

COS 226 Midterm Exam, Spring 2009

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

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

CSE 2123: Collections: Priority Queues. Jeremy Morris

How much space does this routine use in the worst case for a given n? public static void use_space(int n) { int b; int [] A;

Priority Queues, Binary Heaps, and Heapsort

Priority Queues and Binary Heaps. See Chapter 21 of the text, pages

Binary Heaps. COL 106 Shweta Agrawal and Amit Kumar

Sorting and Searching

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

CS302 Midterm Exam Answers & Grading James S. Plank September 30, 2010

COMP Analysis of Algorithms & Data Structures

Priority Queues (Heaps)

CS60020: Foundations of Algorithm Design and Machine Learning. Sourangshu Bhattacharya

CS106X Programming Abstractions in C++ Dr. Cynthia Bailey Lee

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

Heaps and Priority Queues

Sorting and Searching

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

Algorithms in Systems Engineering ISE 172. Lecture 16. Dr. Ted Ralphs

Section 4 SOLUTION: AVL Trees & B-Trees

Priority Queues and Binary Heaps. See Chapter 21 of the text, pages

School of Computing National University of Singapore CS2010 Data Structures and Algorithms 2 Semester 2, AY 2015/16. Tutorial 2 (Answers)

Lecture #21: Search and Sets. Last modified: Wed Mar 9 15:44: CS61A: Lecture #21 1

Module 4: Index Structures Lecture 13: Index structure. The Lecture Contains: Index structure. Binary search tree (BST) B-tree. B+-tree.

Programming II (CS300)

DATA STRUCTURES AND ALGORITHMS. Hierarchical data structures: AVL tree, Bayer tree, Heap

Heapsort. Heap data structure

COMP Analysis of Algorithms & Data Structures

CSE 214 Computer Science II Heaps and Priority Queues

Algorithms, Spring 2014, CSE, OSU Lecture 2: Sorting

Lecture 5: Sorting Part A

Binary Trees. Recursive definition. Is this a binary tree?

CMSC 341 Leftist Heaps

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Priority Queues / Heaps Date: 9/27/17

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

Trees 2: Linked Representation, Tree Traversal, and Binary Search Trees

BINARY SEARCH TREES cs2420 Introduction to Algorithms and Data Structures Spring 2015

Priority Queues Heaps Heapsort

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

The beautiful binary heap.

CS 241 Analysis of Algorithms

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

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

A data structure and associated algorithms, NOT GARBAGE COLLECTION

Lecture: Analysis of Algorithms (CS )

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

Balanced Binary Search Trees. Victor Gao

Binary Search Trees Treesort

CE 221 Data Structures and Algorithms

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

Transcription:

CS61B Lecture #23 Today: Priority queues (Data Structures 6., 6.5) Range queries ( 6.2) Java utilities: SortedSet, Map, etc. Next topic: Hashing (Data Structures Chapter 7). Last modified: Mon Oct 15 1::56 1 CS61B: Lecture #23 1

Priority Queues, Heaps Priority queue: defined by operations add, find largest, remove largest. Examples: scheduling long streams of actions to occur at various future times. Also useful for sorting (keep removing largest). Common implementation is the heap, a kind of tree. (Confusingly,thissametermisusedtodescribedthepoolofstorage that the new operator uses. Sorry about that.) Last modified: Mon Oct 15 1::56 1 CS61B: Lecture #23 2

Heaps A max-heap is a binary tree that enforces the Heap Property: Labels of both children of each node are less than node s label. So node at top has largest label. Looser than binary search property, which allows us to keep tree bushy. That is, it s always valid to put the smallest nodes anywhere at the bottom of the tree. Thus, heaps can be made nearly complete: all but possibly the last row have as many keys as possible. As a result, insertion of new value and deletion of largest value always take time proportional to lgn in worst case. A min-heap is basically the same, but with the minimum value at the root and children having larger values than their parents. Last modified: Mon Oct 15 1::56 1 CS61B: Lecture #23 3

Data: 1 5 Example: Inserting into a simple heap Initial Heap: 5 1 Add : Dashed boxes show where heap property violated re-heapify up 5 1 Last modified: Mon Oct 15 1::56 1 CS61B: Lecture #23

Heap insertion continued Now insert 1: 1 1 1 Last modified: Mon Oct 15 1::56 1 CS61B: Lecture #23 5

Removing Largest from Heap To remove largest: Move bottommost, rightmost node to top, then re-heapify down as needed (swap offending node with larger child) to re-establish heap property. 1 1 Initial 1 Final 1 Last modified: Mon Oct 15 1::56 1 CS61B: Lecture #23 6

Heaps in Arrays Since heaps are nearly complete(missing items only at bottom level), can use arrays for compact representation. Example of removal from last slide (dashed arrows show children): 1 2 3 5 6 7 10 = 1 1 1 Nodes stored in level order. Children of node at index #K are in 2K and 2K +1 if numbering from 1, or 2K +1 and 2K +2 if from 0. 1 1 Last modified: Mon Oct 15 1::56 1 CS61B: Lecture #23 7

Ranges So far, have looked for specific items But for BSTs, need an ordering anyway, and can also support looking for ranges of values. Example: perform some action on all values in a BST that are within some range (in natural order): /** Apply WHATTODO to all labels in T that are >= L and < U, * in ascending natural order. */ static void visitrange(bst<string> T, String L, String U, Consumer<BST<String>> whattodo) { if (T!= null) { int compleft = L.compareTo(T.label ()), compright = U.compareTo(T.label ()); if (compleft < 0) /* L < label */ visitrange (T.left(), L, U, whattodo); if (compleft <= 0 && compright > 0) /* L <= label < U */ whattodo.accept(t); if (compright > 0) /* label < U */ visitrange (T.right (), L, U, whattodo); } } Last modified: Mon Oct 15 1::56 1 CS61B: Lecture #23

Time for Range Queries Time for range query O(h+M), where h is height of tree, and M is number of data items that turn out to be in the range. Consider searching the tree below for all values 25 x < 0. Dashed nodes are never looked at. Starred nodes are looked at but not output. Thehcomes from the starrednodes; them comes from unstarred non-dashed nodes. 2* 21* 71 10 30 50 0 5 12 22* 35 5 55 0 100 Last modified: Mon Oct 15 1::56 1 CS61B: Lecture #23

Ordered Sets and Range Queries in Java Class SortedSet supports range queries with views of set: S.headSet(U): subset of S that is < U. S.tailSet(L): subset that is L. S.subSet(L,U): subset that is L, < U. Changes to views modify S. Attempts to, e.g., add to a headset beyond U are disallowed. Can iterate through a view to process a range: SortedSet<String> fauna = new TreeSet<String> (Arrays.asList ("axolotl", "elk", "dog", "hartebeest", "duck")); for (String item : fauna.subset ("bison", "gnu")) System.out.printf ("%s, ", item); would print dog, duck, elk, Last modified: Mon Oct 15 1::56 1 CS61B: Lecture #23 10

TreeSet Java library type TreeSet<T> requires either that T be Comparable, or that you provide a Comparator, as in: SortedSet<String> rev fauna = new TreeSet<String>(Collections.reverseOrder()); Comparator is a type of function object: interface Comparator<T> { /** Return <0 if LEFT<RIGHT, >0 if LEFT>RIGHT, else 0. */ int compare(t left, T right); } (We ll deal with what Comparator<T extends Comparable<T>> is all about later.) For example, the reverseorder comparator is defined like this: /** A Comparator that gives the reverse of natural order. */ static <T extends Comparable<T>> Comparator<T> reverseorder() { // Java figures out this lambda expression is a Comparable<T>. return (x, y) -> y.compareto(x); } Last modified: Mon Oct 15 1::56 1 CS61B: Lecture #23 11

Example of Representation: BSTSet Same representation for both sets and subsets. Pointer to BST, plus bounds (if any). SortedSet<String> fauna = new BSTSet<String>(stuff); subset1 = fauna.subset("bison","gnu"); subset2 = subset1.subset("axolotl","dog");.size() is expensive! fauna: hartebeest sentinel subset1: bison gnu dog subset2: bison dog axolotl duck elk Last modified: Mon Oct 15 1::56 1 CS61B: Lecture #23 12