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

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

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

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

Data Structures in Java

Outline. Computer Science 331. Insertion: An Example. A Recursive Insertion Algorithm. Binary Search Trees Insertion and Deletion.

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

Organizing Spatial Data

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

ECE250: Algorithms and Data Structures Binary Search Trees (Part A)

COMP Analysis of Algorithms & Data Structures

Lecture 23: Binary Search Trees

COMP Analysis of Algorithms & Data Structures

TREES Lecture 12 CS2110 Fall 2016

Outline. Binary Tree

We assume uniform hashing (UH):

TREES. Tree Overview 9/28/16. Prelim 1 tonight! Important Announcements. Tree terminology. Binary trees were in A1!

Advanced Algorithm Design and Analysis (Lecture 12) SW5 fall 2005 Simonas Šaltenis E1-215b

CSC148 Week 7. Larry Zhang

TREES Lecture 12 CS2110 Spring 2018

UNIVERSITY OF WATERLOO Faculty of Mathematics

Computational Geometry

Algorithms for GIS:! Quadtrees

Motivation Computer Information Systems Storage Retrieval Updates. Binary Search Trees. OrderedStructures. Binary Search Tree

AVL Trees. See Section 19.4of the text, p

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

Programming Languages and Techniques (CIS120)

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

WINDOWING PETR FELKEL. FEL CTU PRAGUE Based on [Berg], [Mount]

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

CS 3114 Data Structures and Algorithms READ THIS NOW!

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

Data Structures and Algorithms for Engineers

CS 315 Data Structures mid-term 2

Binary Trees

INF2220: algorithms and data structures Series 1

Module 4: Dictionaries and Balanced Search Trees

Red-Black Trees. Based on materials by Dennis Frey, Yun Peng, Jian Chen, and Daniel Hood

COMS 3137 Class Notes

Lecture 3 February 9, 2010

TREES Lecture 12 CS2110 Spring 2019

Programming Languages and Techniques (CIS120)

In addition to the correct answer, you MUST show all your work in order to receive full credit.

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

References and Homework ABSTRACT DATA TYPES; LISTS & TREES. Abstract Data Type (ADT) 9/24/14. ADT example: Set (bunch of different values)

Data Structures and Algorithms

CMSC 754 Computational Geometry 1

Friday Four Square! 4:15PM, Outside Gates

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

Geometric Algorithms. Geometric search: overview. 1D Range Search. 1D Range Search Implementations

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

CS 3114 Data Structures and Algorithms Test 1 READ THIS NOW!

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

8. Binary Search Tree

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

Sorted Arrays. Operation Access Search Selection Predecessor Successor Output (print) Insert Delete Extract-Min

Module 9: Binary trees

SFU CMPT Lecture: Week 9

Lecture 6: Analysis of Algorithms (CS )

Extra: B+ Trees. Motivations. Differences between BST and B+ 10/27/2017. CS1: Java Programming Colorado State University

CS 231 Data Structures and Algorithms Fall Recursion and Binary Trees Lecture 21 October 24, Prof. Zadia Codabux

Binary Search Trees. See Section 11.1 of the text.

Figure 4.1: The evolution of a rooted tree.

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

TREES 11/1/18. Prelim Updates. Data Structures. Example Data Structures. Tree Overview. Tree. Singly linked list: Today: trees!

Lecture #21: Search Trees, Sets. Last modified: Tue Mar 18 18:15: CS61A: Lecture #21 1

Binary Trees, Binary Search Trees

CS350: Data Structures Tree Traversal

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

Multidimensional Indexes [14]

kd-trees Idea: Each level of the tree compares against 1 dimension. Let s us have only two children at each node (instead of 2 d )

Binary Trees and Huffman Encoding Binary Search Trees

Binary Search Trees Part Two

BST Implementation. Data Structures. Lecture 4 Binary search trees (BST) Dr. Mahmoud Attia Sakr University of Ain Shams

Module 8: Range-Searching in Dictionaries for Points

10/23/2013. AVL Trees. Height of an AVL Tree. Height of an AVL Tree. AVL Trees

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

CS 206 Introduction to Computer Science II

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

Figure 18.4 A Unix directory. 02/10/04 Lecture 9 1

Data Structures in Java

CMSC 341. Binary Search Trees CMSC 341 BST

CS F-11 B-Trees 1

CS102 Binary Search Trees

PR quadtree. public class prquadtree< T extends Compare2D<? super T> > {

Module 8: Binary trees

Binary Search Trees Treesort

Balanced Search Trees

Disk Accesses. CS 361, Lecture 25. B-Tree Properties. Outline

Recursive Data Structures and Grammars

15.4 Longest common subsequence

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

Balanced Binary Search Trees. Victor Gao

Geometric Algorithms and Data Structures. Part II: Scan-Line Algorithms and Planar Decompositions. Jim Fix

TREES. Trees - Introduction

COMP Analysis of Algorithms & Data Structures

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

Algorithms. AVL Tree

CS61A Notes 02b Fake Plastic Trees. 2. (cons ((1 a) (2 o)) (3 g)) 3. (list ((1 a) (2 o)) (3 g)) 4. (append ((1 a) (2 o)) (3 g))

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

DDS Dynamic Search Trees

Transcription:

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

Divide and Conquer Much (most?) computation is devoted to finding things in response to various forms of query. Linear search for response can be expensive, especially when data set is too large for primary memory. Preferable to have criteria for dividing data to be searched into pieces recursively Remember figure for lg N algorithms: at 1µsec per comparison, could process 10 300000 items in 1 sec. Tree is a natural framework for the representation: decision data decision data no yes low mid high Last modified: Wed Oct 12 19:23:14 2016 CS61B: Lecture #21 2

Binary Search Property: Binary Search Trees Tree nodes contain keys, and possibly other data. All nodes in left subtree of node have smaller keys. All nodes in right subtree of node have larger keys. Smaller means any complete transitive, anti-symmetric ordering on keys: exactly one of x y and y x true. x y and y z imply x z. (To simplify, won t allow duplicate keys this semester). E.g., in dictionary database, node label would be (word, definition ): word is the key. For concreteness here, we ll just use the standard Java convention of calling.compareto. Last modified: Wed Oct 12 19:23:14 2016 CS61B: Lecture #21 3

Searching for 50 and 49: 19 16 25 42 30 60 50 91 Finding / Node in T containing L, or null if none / static BST find(bst T, Key L) { if (T == null) return T; if (L.compareTo(T.label()) == 0) return T; else if (L.compareTo(T.label()) < 0) return find(t.left(), L); else return find(t.right(), L); } Dashed boxes show which node labels we look at. Number looked at proportional to height of tree. Last modified: Wed Oct 12 19:23:14 2016 CS61B: Lecture #21 4

Inserting 27 16 19 25 27 42 50 91 30 60 Inserting / Insert L in T, replacing existing value if present, and returning new tree. / static BST insert(bst T, Key L) { if (T == null) return new BST(L); if (L.compareTo(T.label()) == 0) T.setLabel(L); else if (L.compareTo(T.label()) < 0) T.setLeft(insert(T.left(), L)); else T.setRight(insert(T.right(), L)); return T; } Starred edges are set (to themselves, unless initially null). Again, time proportional to height. Last modified: Wed Oct 12 19:23:14 2016 CS61B: Lecture #21 5

Deletion 42 19 60 16 25 50 91 30 27 Initial 16 27 42 19 60 25 50 91 30 Remove 27 16 42 19 60 30 50 91 19 16 30 50 60 91 formerly contained 42 25 Remove 25 50 Remove 42 Last modified: Wed Oct 12 19:23:14 2016 CS61B: Lecture #21 6

Deletion Algorithm 42 19 60 16 25 50 91 30 27 / Remove L from T, returning new tree. / static BST remove(bst T, Key L) { if (T == null) return null; if (L.compareTo(T.label()) == 0) { if (T.left() == null) return T.right(); else if (T.right() == null) return T.left(); else { Key smallest = minval(t.right()); //?? T.setRight(remove(T.right(), smallest)); T.setLabel(smallest); } } else if (L.compareTo(T.label()) < 0) T.setLeft(remove(T.left(), L)); else T.setRight(remove(T.right(), L)); return T; } Last modified: Wed Oct 12 19:23:14 2016 CS61B: Lecture #21 7

More Than Two Choices: Quadtrees Want to index information about 2D locations so that items can be retrieved by position. Quadtrees do so using standard data-structuring trick: Divide and Conquer. Idea: divide (2D) space into four quadrants, and store items in the appropriate quadrant. Repeat this recursively with each quadrant that contains more than one item. Original definition: a quadtree is either Empty, or An item at some position (x,y), called the root, plus four quadtrees, each containing only items that are northwest, northeast, southwest, and southeast of (x, y). Big idea is that if you are looking for point (x,y ) and the root is not the point you are looking for, you can narrow down which of the four subtrees of the root to look in by comparing coordinates (x,y) with (x,y ). Last modified: Wed Oct 12 19:23:14 2016 CS61B: Lecture #21 8

Classical Quadtree: Example C D B C B E A E A D A B E D C C B A D E Last modified: Wed Oct 12 19:23:14 2016 CS61B: Lecture #21 9

Point-region (PR) Quadtrees If we use a QuadTree to track moving objects, it may be useful to be able to delete items from a tree: when an object moves, the subtree that it goes in may change. Difficult to do with the classical data structure above, so we ll define instead: A quadtree consists of a bounding rectangle, B and either Zero up to a small number of items that lie in that rectangle, or Four quadtrees whose bounding rectangles are the four quadrants of B (all of equal size). A completely empty quadtree can have an arbitrary bounding rectangle, or you can wait for the first point to be inserted. Last modified: Wed Oct 12 19:23:14 2016 CS61B: Lecture #21 10

Example of PR Quadtree 20 F 0 40 A 0 B A/F B 20-10 -15 E D C -20-20 0 5 10 20 10 ( 2 points per leaf) C/D E 5 Last modified: Wed Oct 12 19:23:14 2016 CS61B: Lecture #21 11

Navigating PR Quadtrees To find an item at (x,y) in quadtree T, 1. If (x,y) is outside the bounding rectangle of T, or T is empty, then (x,y) is not in T. 2. Otherwise, if T contains a small set of items, then (x,y) is in T iff it is among these items. 3. Otherwise, T consists of four quadtrees. Recursively look for (x,y) in each (however, step #1 above will cause all but one of these bounding boxes to reject the point immediately). Similar procedure works when looking for all items within some rectangle, R: 1. If R does not intersect the bounding rectangle of T, or T is empty, then there are no items in R. 2. Otherwise, if T contains a set of items, return those that are in R, if any. 3. Otherwise, T consists of four quadtrees. Recursively look for points in R in each one of them. Last modified: Wed Oct 12 19:23:14 2016 CS61B: Lecture #21 12

Insertion into PR Quadtrees Various cases for inserting a new point N, assuming maximum occupancy of a region is 2, showing initial state = final state. = N = N = N (5,5) = N Last modified: Wed Oct 12 19:23:14 2016 CS61B: Lecture #21 13