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

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

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

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

Priority Queues and Binary Heaps

9. Heap : Priority Queue

CS350: Data Structures Heaps and Priority Queues

CS165: Priority Queues, Heaps

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

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

Collection of priority-job pairs; priorities are comparable.

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

CMSC 341 Lecture 14: Priority Queues, Heaps

Chapter 6 Heaps. Introduction. Heap Model. Heap Implementation

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

The Heap Data Structure

Data Structures and Algorithms

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

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.

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

CS S-08 Priority Queues Heaps 1

Dictionaries. Priority Queues

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

Programming Abstractions

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.

Binary Heaps in Dynamic Arrays

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

Properties of a heap (represented by an array A)

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.

COS 226 Midterm Exam, Spring 2009

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

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

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. Priority queues. Priority queue operations

Sorting and Searching

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

Binary Heaps. CSE 373 Data Structures Lecture 11

Data Structures in Java

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

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

Sorting and Searching

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

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

Lecture 6: Analysis of Algorithms (CS )

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

Heapsort. Heap data structure

Priority Queues (Heaps)

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

CS 241 Analysis of Algorithms

COMP Analysis of Algorithms & Data Structures

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

Heaps and Priority Queues

Topic: Heaps and priority queues

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

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

CE 221 Data Structures and Algorithms

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

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

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

Lecture 5: Sorting Part A

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

CSE 214 Computer Science II Heaps and Priority Queues

COMP Analysis of Algorithms & Data Structures

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

Programming II (CS300)

Binary Heaps. COL 106 Shweta Agrawal and Amit Kumar

Priority Queues, Binary Heaps, and Heapsort

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

CSC Design and Analysis of Algorithms. Lecture 8. Transform and Conquer II Algorithm Design Technique. Transform and Conquer

CSC Design and Analysis of Algorithms. Lecture 8. Transform and Conquer II Algorithm Design Technique. Transform and Conquer

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

Algorithms and Theory of Computation. Lecture 7: Priority Queue

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

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

Priority Queues Heaps Heapsort

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

CMSC 341 Leftist Heaps

Priority Queues (Heaps)

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

Lecture Notes on Priority Queues

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

Binary Search Trees Treesort

! 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

Lecture 13: AVL Trees and Binary Heaps

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

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

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

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

Balanced Binary Search Trees. Victor Gao

Heaps. A heap is a certain kind of complete binary tree.

Heapsort. Algorithms.

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: Sat Oct 15 16:1:5 16 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: Sat Oct 15 16:1:5 16 CS61B: Lecture #23 2

Heaps A max-heap is a binary tree that enforces the Heap Property: Both labels in 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: Sat Oct 15 16:1:5 16 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: Sat Oct 15 16:1:5 16 CS61B: Lecture #23

Heap insertion continued Now insert 1: 1 1 1 Last modified: Sat Oct 15 16:1:5 16 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: Sat Oct 15 16:1:5 16 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 1 1 Last modified: Sat Oct 15 16:1:5 16 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 T, Comparable<Key> L, Comparable<Key> U, Action 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.action (T); if (compright > 0) /* label < U */ visitrange (T.right (), L, U, whattodo); } } Last modified: Sat Oct 15 16:1:5 16 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, x, such that 25 x < 0. In this example, the h comes from the starred nodes; the M comes from other non-dashed nodes. Dashed nodes are never looked at. 2* 21* 71 10 30 50 0 5 12 22* 35 5 55 0 100 Last modified: Sat Oct 15 16:1:5 16 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, Java library type TreeSet<T> requires either that T be Comparable, or that you provide a Comparator: SortedSet<String> rev fauna = new TreeSet<String> (Collections.reverseOrder()); Last modified: Sat Oct 15 16:1:5 16 CS61B: Lecture #23 10

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: Sat Oct 15 16:1:5 16 CS61B: Lecture #23 11