COMP 250. Lecture 20. tree traversal. Oct. 25/26, 2017

Similar documents
MA 513: Data Structures Lecture Note Partha Sarathi Mandal

Uninformed Search Lecture 4

19 Optimal behavior: Game theory

CS 188: Artificial Intelligence Spring 2007

Preview 11/1/2017. Greedy Algorithms. Coin Change. Coin Change. Coin Change. Coin Change. Greedy algorithms. Greedy Algorithms

Recursively Enumerable and Recursive. Languages

INF2220: algorithms and data structures Series 1

Algorithms & Data Structures Homework 8 HS 18 Exercise Class (Room & TA): Submitted by: Peer Feedback by: Points:

CMSC 132, Object-Oriented Programming II Summer Lecture 12

Let's start with an example:

CS103B Handout 18 Winter 2007 February 28, 2007 Finite Automata

In Linear Time from Regular Expressions to NFAs

CMPSCI 250: Introduction to Computation. Lecture #31: What DFA s Can and Can t Do David Mix Barrington 9 April 2014

Tries and suffixes trees

Convert the NFA into DFA

CS 310 (sec 20) - Winter Final Exam (solutions) SOLUTIONS

DATA Search I 魏忠钰. 复旦大学大数据学院 School of Data Science, Fudan University. March 7 th, 2018

Minimal DFA. minimal DFA for L starting from any other

Java II Finite Automata I

More on automata. Michael George. March 24 April 7, 2014

Intermediate Math Circles Wednesday, November 14, 2018 Finite Automata II. Nickolas Rollick a b b. a b 4

Lecture 7 notes Nodal Analysis

Hybrid Control and Switched Systems. Lecture #2 How to describe a hybrid system? Formal models for hybrid system

Search: The Core of Planning

CSE : Exam 3-ANSWERS, Spring 2011 Time: 50 minutes

Ordinary Differential Equations- Boundary Value Problem

Module 9: Tries and String Matching

Module 9: Tries and String Matching

Kleene s Theorem. Kleene s Theorem. Kleene s Theorem. Kleene s Theorem. Kleene s Theorem. Kleene s Theorem 2/16/15

Properties of Integrals, Indefinite Integrals. Goals: Definition of the Definite Integral Integral Calculations using Antiderivatives

Merge Sort. Outline and Reading. Divide-and-Conquer. Divide-and-conquer paradigm ( 4.1.1) Merge-sort ( 4.1.1)

Finite Automata. Informatics 2A: Lecture 3. John Longley. 22 September School of Informatics University of Edinburgh

3 Regular expressions

CS 301. Lecture 04 Regular Expressions. Stephen Checkoway. January 29, 2018

CS 314 Principles of Programming Languages

Non-deterministic Finite Automata

CS 373, Spring Solutions to Mock midterm 1 (Based on first midterm in CS 273, Fall 2008.)

Data Structures and Algorithms CMPSC 465

DIRECT CURRENT CIRCUITS

Riemann is the Mann! (But Lebesgue may besgue to differ.)

Improper Integrals. The First Fundamental Theorem of Calculus, as we ve discussed in class, goes as follows:

CAAM 453 NUMERICAL ANALYSIS I Examination There are four questions, plus a bonus. Do not look at them until you begin the exam.

Regular Language. Nonregular Languages The Pumping Lemma. The pumping lemma. Regular Language. The pumping lemma. Infinitely long words 3/17/15

Closure Properties of Regular Languages

Physics 1402: Lecture 7 Today s Agenda

Deterministic Finite-State Automata

Non-deterministic Finite Automata

Math 4310 Solutions to homework 1 Due 9/1/16

HW #4. (mostly by) Salim Sarımurat. 1) Insert 6 2) Insert 8 3) Insert 30. 4) Insert S a.

NFAs and Regular Expressions. NFA-ε, continued. Recall. Last class: Today: Fun:

Connected-components. Summary of lecture 9. Algorithms and Data Structures Disjoint sets. Example: connected components in graphs

a b b a pop push read unread

Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2018

Nondeterminism and Nodeterministic Automata

CS4800: Algorithms & Data Jonathan Ullman

SWEN 224 Formal Foundations of Programming WITH ANSWERS

ɛ-closure, Kleene s Theorem,

First Midterm Examination

The University of Nottingham SCHOOL OF COMPUTER SCIENCE A LEVEL 2 MODULE, SPRING SEMESTER LANGUAGES AND COMPUTATION ANSWERS

Lecture 08: Feb. 08, 2019

CS415 Compilers. Lexical Analysis and. These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University

Riemann Sums and Riemann Integrals

New Expansion and Infinite Series

Theory of Computation Regular Languages. (NTU EE) Regular Languages Fall / 38

CS/CE/SE 6367 Software Testing, Validation and Verification. Lecture 4 Code Coverage (II)

Riemann Sums and Riemann Integrals

CS 275 Automata and Formal Language Theory

1 Probability Density Functions

How do we solve these things, especially when they get complicated? How do we know when a system has a solution, and when is it unique?

Assignment 1 Automata, Languages, and Computability. 1 Finite State Automata and Regular Languages

7.1 Integral as Net Change and 7.2 Areas in the Plane Calculus

Finite Automata. Informatics 2A: Lecture 3. Mary Cryan. 21 September School of Informatics University of Edinburgh

Finite-State Automata: Recap

Lecture 9: LTL and Büchi Automata

NFA DFA Example 3 CMSC 330: Organization of Programming Languages. Equivalence of DFAs and NFAs. Equivalence of DFAs and NFAs (cont.

Myhill-Nerode Theorem

Theory of Computation Regular Languages

Hints for Exercise 1 on: Current and Resistance

The area under the graph of f and above the x-axis between a and b is denoted by. f(x) dx. π O

Global alignment. Genome Rearrangements Finding preserved genes. Lecture 18

Quadratic Forms. Quadratic Forms

4.5 JACOBI ITERATION FOR FINDING EIGENVALUES OF A REAL SYMMETRIC MATRIX. be a real symmetric matrix. ; (where we choose θ π for.

Finite Automata-cont d

CHAPTER 1 Regular Languages. Contents

CMSC 330: Organization of Programming Languages

CS 188 Introduction to Artificial Intelligence Fall 2018 Note 7

Algebra Readiness PLACEMENT 1 Fraction Basics 2 Percent Basics 3. Algebra Basics 9. CRS Algebra 1

Uses of transformations. 3D transformations. Review of vectors. Vectors in 3D. Points vs. vectors. Homogeneous coordinates S S [ H [ S \ H \ S ] H ]

Worked out examples Finite Automata

Regular expressions, Finite Automata, transition graphs are all the same!!

Types of Finite Automata. CMSC 330: Organization of Programming Languages. Comparing DFAs and NFAs. NFA for (a b)*abb.

Math Lecture 23

Types of Finite Automata. CMSC 330: Organization of Programming Languages. Comparing DFAs and NFAs. Comparing DFAs and NFAs (cont.) Finite Automata 2

Fingerprint idea. Assume:

Improper Integrals, and Differential Equations

XPath Node Selection over Grammar-Compressed Trees

Department of Chemical Engineering ChE-101: Approaches to Chemical Engineering Problem Solving MATLAB Tutorial VII

4. GREEDY ALGORITHMS I

CS375: Logic and Theory of Computing

Taylor Polynomial Inequalities

Transcription:

COMP 250 Lecture 20 tree trversl Oct. 25/26, 2017 1

2

Tree Trversl How to visit (enumerte, iterte through, trverse ) ll the nodes of tree? 3

depthfirst (root){ // preorder if (root is not empty){ visit root for ech child of root depthfirst( child ) 1 4

depthfirst (root){ // preorder if (root is not empty){ visit root for ech child of root depthfirst( child ) 2 1 3 5

depthfirst (root){ // preorder if (root is not empty){ visit root for ech child of root depthfirst( child ) 2 1 3 4 5 6

depthfirst (root){ // preorder if (root is not empty){ visit root for ech child of root depthfirst( child ) 1 2 7 8 3 4 9 10 11 5 6 7

Preorder Trversl e.g. Printing directory (visit = print) Eminem Lose Yourself My Documents Music Videos Wor She my sillies out Rffi Bby Belug COMP 250 Reserch Documents (directory) Music (directory) Eminem (directory) Lose Yourself (file) Rffi (directory) She My Sillies Out (file) Bby Belug (file) Videos (directory) : (file) Wor (directory) COMP250 (directory) : Reserch (directory) : 8

Visit implies tht you do something t tht node. Anlogy: you ren t visiting London UK if you just fly through Hethrow. 9

depthfirst (root){ // postorder if (root is not empty){ for ech child of root depthfirst( child ) visit root Q: Which node is visited first? 10

depthfirst (root){ // postorder if (root is not empty){ for ech child of root depthfirst( child ) visit root 1 11

depthfirst (root){ // postorder if (root is not empty){ for ech child of root depthfirst( child ) visit root 1 2 12

depthfirst (root){ // postorder if (root is not empty){ for ech child of root depthfirst( child ) visit root 5 1 4 2 3 13

depthfirst (root){ // postorder if (root is not empty){ for ech child of root depthfirst( child ) visit root 11 5 6 10 1 4 7 8 9 2 3 14

Exmple 1 postorder: recll lst lecture 4 2 1 3 1 0 0 2 1 0 0 0 1 0 0 0 height(v){ if (v is lef) return 0 else{ h = 0 for ech child w of v h = mx(h, height(w)) return 1 + h visit = return vlue of height 15

Exmple 2 Postorder: Wht is the totl number of bytes in ll files in directory? 16

numbytes(root){ if root is lef return number of bytes t root else { sum = 0 for ech child of root{ sum += numbytes(child) return sum By visit here, we men determining the number of bytes for node, e.g. If we were to store sum t the node. 17

NOTE: Sme cll sequence occurs for preorder vs postorder. Letter order corresponds to depthfirst() cll order b g h c d i j e f 18

Cll stc for depthfirst() b g h c d i j e f e f c d d d d d i j b b b b b b b b b g h h h h h h h 19

Cll stc for depthfirst() b g h c d i j e f e f c d d d d d i j b b b b b b b b b g h h h h h h h 20

Cll stc for depthfirst() b g h c d i j e f e f c d d d d d i j b b b b b b b b b g h h h h h h h 21

Cll stc for depthfirst() b g h c d i j e f e f c d d d d d i j b b b b b b b b b g h h h h h h h 22

Tree trversl Recursive depth first (pre- versus post-order) Non-Recursive using stc using queue 23

treetrverslusingstc(root){ initilize empty stc s s.push(root) while s is not empty { cur = s.pop() visit cur // moving visit cur to be for ech child of cur // fter for loop s.push(child) // chnges nothing 24

treetrverslusingstc(root){ initilize empty stc s s.push(root) while s is not empty { cur = s.pop() visit cur 25

treetrverslusingstc(root){ initilize empty stc s s.push(root) while s is not empty { cur = s.pop() visit cur for ech child of cur s.push(child) 26

Wht is the order of nodes visited? 27

treetrverslusingstc(root){ initilize empty stc s s.push(root) while s is not empty { cur = s.pop() visit cur for ech child of cur s.push(child) b g h c d i j e f j j j h i i i i i f g g g g g g g g g d e e e _ b b b b b b b b b b b _ c c c c c c c _ 28

treetrverslusingstc(root){ initilize empty stc s s.push(root) while s is not empty { cur = s.pop() visit cur for ech child of cur s.push(child) b g h c d i j e f j j j h i i i i i f g g g g g g g g g d e e e _ b b b b b b b b b b b _ c c c c c c c _ 29

treetrverslusingstc(root){ initilize empty stc s s.push(root) while s is not empty { cur = s.pop() visit cur for ech child of cur s.push(child) b g h c d i j e f j j j h i i i i i f g g g g g g g g g d e e e _ b b b b b b b b b b b _ c c c c c c c _ 30

treetrverslusingstc(root){ initilize empty stc s s.push(root) while s is not empty { cur = s.pop() visit cur for ech child of cur s.push(child) b g h c d i j e f j j j h i i i i i f g g g g g g g g g d e e e _ b b b b b b b b b b b _ c c c c c c c _ 31

treetrverslusingstc(root){ initilize empty stc s s.push(root) while s is not empty { cur = s.pop() visit cur for ech child of cur s.push(child) b g h c d i j e f j j j h i i i i i f g g g g g g g g g d e e e _ b b b b b b b b b b b _ c c c c c c c _ 32

treetrverslusingstc(root){ initilize empty stc s s.push(root) while s is not empty { cur = s.pop() visit cur for ech child of cur s.push(child) b g h c d i j e f j j j h i i i i i f g g g g g g g g g d e e e _ b b b b b b b b b b b _ c c c c c c c _ 33

treetrverslusingstc(root){ initilize empty stc s s.push(root) while s is not empty { cur = s.pop() visit cur for ech child of cur s.push(child) b g h c d i j e f j j j h i i i i i f g g g g g g g g g d e e e _ b b b b b b b b b b b _ c c c c c c c _ 34

treetrverslusingstc(root){ initilize empty stc s s.push(root) while s is not empty { cur = s.pop() visit cur for ech child of cur s.push(child) b g h c d i j e f j j j h i i i i i f g g g g g g g g g d e e e _ b b b b b b b b b b b _ c c c c c c c _ 35

treetrverslusingstc(root){ initilize empty stc s s.push(root) while s is not empty { cur = s.pop() visit cur for ech child of cur s.push(child) b g h c d i j e f j j j h i i i i i f g g g g g g g g g d e e e _ b b b b b b b b b b b _ c c c c c c c _ 36

treetrverslusingstc(root){ initilize empty stc s s.push(root) while s is not empty { cur = s.pop() visit cur for ech child of cur s.push(child) b g h c d i j e f j j j h i i i i i f g g g g g g g g g d e e e _ b b b b b b b b b b b _ c c c c c c c _ 37

Stc bsed method is depth first, but visits children from right to left b g h c d i j e f recursive non-recursive (stc) bcdefghij hjigbdfec 38

Pre- or post order? treetrverslusingstc(root){ initilize empty stc s s.push(root) while s is not empty { cur = s.pop() visit cur for ech child of cur s.push(child) visit cur Moving the visit does not me it post order. Why not? 39

Wht if we use queue insted? treetrverslusingstc(root){ initilize empty stc s s.push(root) while s is not empty { cur = s.pop() visit cur for ech child of cur s.push(child) treetrverslusingqueue(root){ initilize empty queue q q.enqueue(root) while q is not empty { cur = q.dequeue() visit cur for ech child of cur q.enqueue(child) 40

treetrverslusingqueue(root){ initilize empty queue q q.enqueue(root) while q is not empty { cur = q.dequeue() visit cur for ech child of cur q.enqueue(child) Queue stte t strt of the while loop b c d e f g h i j 41

treetrverslusingqueue(root){ initilize empty queue q q.enqueue(root) while q is not empty { cur = q.dequeue() visit cur for ech child of cur q.enqueue(child) Queue stte t strt of the while loop b c d b c d e f g h i j 42

treetrverslusingqueue(root){ initilize empty queue q q.enqueue(root) while q is not empty { cur = q.dequeue() visit cur for ech child of cur q.enqueue(child) Queue stte t strt of the while loop b c d c d e f b c d e f g h i j 43

treetrverslusingqueue(root){ initilize empty queue q q.enqueue(root) while q is not empty { cur = q.dequeue() visit cur for ech child of cur q.enqueue(child) Queue stte t strt of the while loop b c d c d e f d e f b c d e f g h i j 44

treetrverslusingqueue(root){ initilize empty queue q q.enqueue(root) while q is not empty { cur = q.dequeue() visit cur for ech child of cur q.enqueue(child) Queue stte t strt of the while loop b c d c d e f d e f e f g h i b c d e f g h i j 45

treetrverslusingqueue(root){ initilize empty queue q q.enqueue(root) while q is not empty { cur = q.dequeue() visit cur for ech child of cur q.enqueue(child) b c d e f g h j i b c d c d e f d e f e f g h i f g h i g h i j h i j i j j 46

bredth first trversl for ech level i visit ll nodes t level i b c d order visited: bcdefghij e f g h i j 47

Implementtion Detils Recll: first child, next sibling clss TreeNode<T>{ T element; TreeNode<T> firstchild; TreeNode<T> nextsibling; : : clss Tree<T>{ TreeNode<T> root; : : 48

for ech child{ mens: child = cur.firstchild while (child!= null){.. child = child.nextsibling 49