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

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

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

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

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

Binary Trees

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

TREES. Trees - Introduction

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

TREES Lecture 12 CS2110 Fall 2016

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

Binary Trees, Binary Search Trees

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

INF2220: algorithms and data structures Series 1

There are many other applications like constructing the expression tree from the postorder expression. I leave you with an idea as how to do it.

Programming II (CS300)

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

CS350: Data Structures Tree Traversal

CSC148 Week 6. Larry Zhang

Data Structure Advanced

TREES Lecture 10 CS2110 Spring2014

Binary Search Trees Treesort

Tree Structures. A hierarchical data structure whose point of entry is the root node

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

Draw a diagram of an empty circular queue and describe it to the reader.

Lec 17 April 8. Topics: binary Trees expression trees. (Chapter 5 of text)

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

Data Structures Question Bank Multiple Choice

Trees! Ellen Walker! CPSC 201 Data Structures! Hiram College!

CE 221 Data Structures and Algorithms

Topics. Trees Vojislav Kecman. Which graphs are trees? Terminology. Terminology Trees as Models Some Tree Theorems Applications of Trees CMSC 302

Data Structures. Binary Trees. Root Level = 0. number of leaves:?? leaves Depth (Maximum level of the tree) leaves or nodes. Level=1.

COMP 250 Fall binary trees Oct. 27, 2017

CS24 Week 8 Lecture 1

Computer Science 210 Data Structures Siena College Fall Topic Notes: Trees

a graph is a data structure made up of nodes in graph theory the links are normally called edges

TREES Lecture 12 CS2110 Spring 2018

Data Structures. Trees. By Dr. Mohammad Ali H. Eljinini. M.A. Eljinini, PhD

CSI33 Data Structures

Chapter 20: Binary Trees

Data Structure. IBPS SO (IT- Officer) Exam 2017

Friday Four Square! 4:15PM, Outside Gates

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

Computer Science E-119 Fall Problem Set 4. Due prior to lecture on Wednesday, November 28

Data Structures and Algorithms for Engineers

First Semester - Question Bank Department of Computer Science Advanced Data Structures and Algorithms...

Trees. (Trees) Data Structures and Programming Spring / 28

Trees. Q: Why study trees? A: Many advance ADTs are implemented using tree-based data structures.

Largest Online Community of VU Students

1 Binary trees. 1 Binary search trees. 1 Traversal. 1 Insertion. 1 An empty structure is an empty tree.

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

Tree. A path is a connected sequence of edges. A tree topology is acyclic there is no loop.

Topic 14. The BinaryTree ADT

3137 Data Structures and Algorithms in C++

tree nonlinear Examples

Also, recursive methods are usually declared private, and require a public non-recursive method to initiate them.

Recursive Data Structures and Grammars

Principles of Computer Science

The tree data structure. Trees COL 106. Amit Kumar Shweta Agrawal. Acknowledgement :Many slides are courtesy Douglas Harder, UWaterloo

Trees : Part 1. Section 4.1. Theory and Terminology. A Tree? A Tree? Theory and Terminology. Theory and Terminology

March 20/2003 Jayakanth Srinivasan,

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

An Introduction to Trees

CISC 235 Topic 3. General Trees, Binary Trees, Binary Search Trees

Data Structures and Algorithms

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

Tree: non-recursive definition. Trees, Binary Search Trees, and Heaps. Tree: recursive definition. Tree: example.

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

Trees. Dr. Ronaldo Menezes Hugo Serrano Ronaldo Menezes, Florida Tech

Why Do We Need Trees?

LECTURE 11 TREE TRAVERSALS

Trees. T.U. Cluj-Napoca -DSA Lecture 2 - M. Joldos 1

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

Binary Trees and Binary Search Trees

Data Structure Lecture#10: Binary Trees (Chapter 5) U Kang Seoul National University

CS61BL. Lecture 3: Asymptotic Analysis Trees & Tree Traversals Stacks and Queues Binary Search Trees (and other trees)

Trees. CSE 373 Data Structures

Binary Search Trees. See Section 11.1 of the text.

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

Data Structures in Java

CSCI-401 Examlet #5. Name: Class: Date: True/False Indicate whether the sentence or statement is true or false.

12 Abstract Data Types


Revision Statement while return growth rate asymptotic notation complexity Compare algorithms Linear search Binary search Preconditions: sorted,

INF2220: algorithms and data structures Series 1

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

Section 5.5. Left subtree The left subtree of a vertex V on a binary tree is the graph formed by the left child L of V, the descendents

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

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

8. Write an example for expression tree. [A/M 10] (A+B)*((C-D)/(E^F))

Binary Trees. College of Computing & Information Technology King Abdulaziz University. CPCS-204 Data Structures I


Data Structures And Algorithms

EE 368. Week 6 (Notes)

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

Uses for Trees About Trees Binary Trees. Trees. Seth Long. January 31, 2010

CMPSCI 187: Programming With Data Structures. Lecture #26: Binary Search Trees David Mix Barrington 9 November 2012

Abstract Data Structures IB Computer Science. Content developed by Dartford Grammar School Computer Science Department

CS302 - Data Structures using C++

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

Binary Trees and Huffman Encoding Binary Search Trees

Transcription:

CS61B Lecture #20 Today: Trees Readings for Today: Data Structures, Chapter 5 Readings for Next Topic: Data Structures, Chapter 6 Last modified: Wed Oct 14 03:20:09 2015 CS61B: Lecture #20 1

A Recursive Structure Trees naturally represent recursively defined, hierarchical objects with more than one recursive subpart for each instance. Common examples: expressions, sentences. Expressions have definitions such as an expression consists of a literal or two expressions separated by an operator. Also describe structures in which we recursively divide a set into multiple subsets. Last modified: Wed Oct 14 03:20:09 2015 CS61B: Lecture #20 2

Fundamental Operation: Traversal Traversing a tree means enumerating (some subset of) its nodes. Typically done recursively, because that is natural description. As nodes are enumerated, we say they are visited. Three basic orders for enumeration (+ variations): Preorder: visit node, traverse its children. Postorder: traverse children, visit node. Inorder: traverse first child, visit node, traverse second child (binary trees only). 6 0 4 3 5 1 5 1 5 0 2 4 2 3 6 0 3 6 1 4 2 Postorder Preorder inorder Last modified: Wed Oct 14 03:20:09 2015 CS61B: Lecture #20 3

Preorder Traversal and Prefix Expressions - - z Problem: Convert (- (- ( x (+ y 3))) z) x + y 3 (Tree<Label > is supposed to mean Tree whose labels have type Label.) static String tolisp (Tree<String> T) { if (T == null) return ""; else if (T.degree () == 0) return T.label (); else { String R; R = ""; for (int i = 0; i < T.numChildren (); i += 1) R += " " + tolisp (T.child (i)); return String.format ("(%s%s)", T.label (), R); Last modified: Wed Oct 14 03:20:09 2015 CS61B: Lecture #20 4

Problem: Convert - Inorder Traversal and Infix Expressions - z ((-(x(y+3)))-z) To think about: how to get rid of all those parentheses. x + y 3 static String toinfix (Tree<String> T) { if (T == null) return ""; if (T.degree () == 0) return T.label (); else { return String.format ("(%s%s%s)", toinfix (T.left ()), T.label (), toinfix (T.right ()) Last modified: Wed Oct 14 03:20:09 2015 CS61B: Lecture #20 5

Postorder Traversal and Postfix Expressions Problem: Convert - - z x y 3 +:2 :2 -:1 z -:2 x + y 3 static String topolish (Tree<String> T) { if (T == null) return ""; else { String R; R = ""; for (int i = 0; i < T.numChildren (); i += 1) R += topolish (T.child (i)) + " "; return String.format ("%s%s:%d", R, T.label (), T.degree ()); Last modified: Wed Oct 14 03:20:09 2015 CS61B: Lecture #20 6

A General Traversal: The Visitor Pattern void preordertraverse (Tree<Label> T, Action<Label> whattodo) { if (T!= null) { whattodo.action (T); for (int i = 0; i < T.numChildren (); i += 1) preordertraverse (T.child (i), whattodo); What is Action? interface Action<Label> { void action (Tree<Label> T); Now, using Java 8 lambda syntax, I can print all labels in the tree in preorder with: preordertraverse(mytree, (Tree<String> T) -> System.out.print(T.label())); Last modified: Wed Oct 14 03:20:09 2015 CS61B: Lecture #20 7

Times The traversal algorithms have roughly the form of the boom example in 1.3.3 of Data Structures an exponential algorithm. However, the role of M in that algorithm is played by the height of the tree, not the number of nodes. In fact, easy to see that tree traversal is linear: Θ(N), where N is the # of nodes: Form of the algorithm implies that there is one visit at the root, and then one visit for every edge in the tree. Since every node but the root has exactly one parent, and the root has none, must be N 1 edges in any non-empty tree. In positional tree, is also one recursive call for each empty tree, but # of empty trees can be no greater than kn, where k is arity. For k-ary tree (max # children is k), h + 1 N kh+1 1 k 1, where h is height. So h Ω(log k N) = Ω(lg N) and h O(N). Many tree algorithms look at one child only. For them, time is proportional to the height of the tree, and this is Θ(lg N), assuming that tree is bushy each level has about as many nodes as possible. Last modified: Wed Oct 14 03:20:09 2015 CS61B: Lecture #20 8

Level-Order (Breadth-First) Traversal Problem: Traverse all nodes at depth 0, then depth 1, etc: 0 1 2 3 4 5 6 One technique: Iterative Deepening. For each level, k, from 0 to h, call dolevel(t,k) void dolevel (Tree T, int lev) { if (lev == 0) visit T else for each non-null child, C, of T { dolevel (C, lev-1); Last modified: Wed Oct 14 03:20:09 2015 CS61B: Lecture #20 9

Iterative Deepening Time? 0 0 1 2 1 3 4 5 6 2 7 8 9 10 11 12 Let h be height, N be # of nodes. 13 14 Count # edges traversed (i.e, # of calls, not counting null nodes). First (full) tree: 1 for level 0, 3 for level 1, 7 for level 2, 15 for level 3. Or in general (2 1 1) + (2 2 1) +... + (2 h+1 1) = 2 h+2 h Θ(N), since N = 2 h+1 1 for this tree. Second (right leaning) tree: 1 for level 0, 2 for level 2, 3 for level 3. Or in general (h + 1)(h + 2)/2 = N(N + 1)/2 Θ(N 2 ), since N = h + 1 for this kind of tree. 3 Last modified: Wed Oct 14 03:20:09 2015 CS61B: Lecture #20 10

Iterative Traversals Tree recursion conceals data: a stack of nodes (all thetarguments) and a little extra information. Can make the data explicit, e.g.: void preordertraverse2 (Tree<Label> T, Action whattodo) { Stack<Tree<Label>> s = new Stack<>(); s.push (T); while (! s.isempty ()) { Tree<Label> node = s.pop (); if (node == null) continue; whattodo.action (node); for (int i = node.numchildren ()-1; i >= 0; i -= 1) s.push (node.child (i)); To do a breadth-first traversal, use a queue instead of a stack, replace push with add, and pop with removefirst. Makes breadth-first traversal worst-case linear time in all cases, but also linear space for bushy trees. Last modified: Wed Oct 14 03:20:09 2015 CS61B: Lecture #20 11

Iterators for Trees Frankly, iterators are not terribly convenient on trees. But can use ideas from iterative methods. class PreorderTreeIterator<Label> implements Iterator<Label> { private Stack<Tree<Label>> s = new Stack<Tree<Label>> (); public PreorderTreeIterator (Tree<Label> T) { s.push (T); public boolean hasnext () { return! s.isempty (); public T next () { Tree<Label> result = s.pop (); for (int i = result.numchildren ()-1; i >= 0; i -= 1) s.push (result.child (i)); return result.label (); void remove () { throw new UnsupportedOperationException (); Example: (what do I have to add to class Tree first?) for (String label : atree) System.out.print (label + " "); Last modified: Wed Oct 14 03:20:09 2015 CS61B: Lecture #20 12

Tree Representation 0 1 2 3 0 0 1... 2... 3... (a) Embedded child pointers (+ optional parent pointers) 0 1 2 3......... 1... 2... 3... (b) Array of child pointers (+ optional parent pointers) 0 1 2 3 (d) breadth-first array (complete trees) (c) child/sibling pointers Last modified: Wed Oct 14 03:20:09 2015 CS61B: Lecture #20 13

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 14 03:20:09 2015 CS61B: Lecture #20 14

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. Last modified: Wed Oct 14 03:20:09 2015 CS61B: Lecture #20 15

Finding Searching for 50 and 49: 19 16 25 42 30 60 50 91 / Node in T containing L, or null if none / static BST find(bst T, Key L) { if (T == null) return T; if (L.keyequals (T.label())) return T; else if (L T.label()) 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 14 03:20:09 2015 CS61B: Lecture #20 16

Inserting Inserting 27 16 19 25 27 42 50 91 30 60 / Insert L in T, replacing existing value if present, and returning new tree. / BST insert(bst T, Key L) { if (T == null) return new BST(L); if (L.keyequals (T.label())) T.setLabel (L); else if (L T.label()) 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 14 03:20:09 2015 CS61B: Lecture #20 17

Deletion 16 42 19 60 16 25 50 91 30 27 Initial 42 19 60 30 50 91 42 19 60 16 25 50 91 30 27 Remove 27 50 formerly contained 42 19 60 16 30 91 25 Remove 25 50 Remove 42 Last modified: Wed Oct 14 03:20:09 2015 CS61B: Lecture #20 18

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 14 03:20:09 2015 CS61B: Lecture #20 19

Classical Quadtree: Example B B C C D D A A E E A D B E C B A C E D Last modified: Wed Oct 14 03:20:09 2015 CS61B: Lecture #20 20

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 14 03:20:09 2015 CS61B: Lecture #20 21

Example of PR Quadtree 20 0 40 0 B A A B 20-10 -15 C D -20-20 0 5 10 20 10 ( 1 point per leaf) D C 5 Last modified: Wed Oct 14 03:20:09 2015 CS61B: Lecture #20 22

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 14 03:20:09 2015 CS61B: Lecture #20 23

Insertion into PR Quadtrees Various cases for inserting a new point N, showing initial state = final state. (10,10) (10,10) (10,10) (10,10) = N = N (0,0) (0,0) (0,0) (0,0) (10,10) (10,10) (10,10) = N (5,5) = N (0,0) (0,0) (0,0) (0,0) Last modified: Wed Oct 14 03:20:09 2015 CS61B: Lecture #20 24