binary trees, expression trees

Similar documents
COMP 250 Fall binary trees Oct. 27, 2017

COMP 423 lecture 11 Jan. 28, 2008

CS201 Discussion 10 DRAWTREE + TRIES

10/9/2012. Operator is an operation performed over data at runtime. Arithmetic, Logical, Comparison, Assignment, Etc. Operators have precedence

Formal Languages and Automata Theory, SS Project (due Week 14)

Phylogeny and Molecular Evolution

Section 3.1: Sequences and Series

Binary Trees, Binary Search Trees

Stack. A list whose end points are pointed by top and bottom

ASTs, Regex, Parsing, and Pretty Printing

Orthogonal line segment intersection

TREES. Trees - Introduction

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

If you are at the university, either physically or via the VPN, you can download the chapters of this book as PDFs.

Tries. Yufei Tao KAIST. April 9, Y. Tao, April 9, 2013 Tries

Homework. Context Free Languages III. Languages. Plan for today. Context Free Languages. CFLs and Regular Languages. Homework #5 (due 10/22)

March 20/2003 Jayakanth Srinivasan,

EECS 281: Homework #4 Due: Thursday, October 7, 2004

Lecture Notes 16 - Trees CSS 501 Data Structures and Object-Oriented Programming Professor Clark F. Olson

Unit #9 : Definite Integral Properties, Fundamental Theorem of Calculus

Functor (1A) Young Won Lim 8/2/17

CSCI1950 Z Computa4onal Methods for Biology Lecture 2. Ben Raphael January 26, hhp://cs.brown.edu/courses/csci1950 z/ Outline

SIMPLIFYING ALGEBRA PASSPORT.

MATH 25 CLASS 5 NOTES, SEP

Functor (1A) Young Won Lim 10/5/17

Math 464 Fall 2012 Notes on Marginal and Conditional Densities October 18, 2012

CS321 Languages and Compiler Design I. Winter 2012 Lecture 5

ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών

CPSC 213. Polymorphism. Introduction to Computer Systems. Readings for Next Two Lectures. Back to Procedure Calls

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

What do all those bits mean now? Number Systems and Arithmetic. Introduction to Binary Numbers. Questions About Numbers

Sample Midterm Solutions COMS W4115 Programming Languages and Translators Monday, October 12, 2009

Control-Flow Analysis and Loop Detection

Improper Integrals. October 4, 2017

such that the S i cover S, or equivalently S

Tree Data Structures CSC 221

Solving Problems by Searching. CS 486/686: Introduction to Artificial Intelligence Winter 2016

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.

Introduction to Computers and Programming. Concept Question

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

Definition of Regular Expression

Solving Problems by Searching. CS 486/686: Introduction to Artificial Intelligence

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

What do all those bits mean now? Number Systems and Arithmetic. Introduction to Binary Numbers. Questions About Numbers

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

Ma/CS 6b Class 1: Graph Recap

Representation of Numbers. Number Representation. Representation of Numbers. 32-bit Unsigned Integers 3/24/2014. Fixed point Integer Representation

CSE 401 Midterm Exam 11/5/10 Sample Solution

Presentation Martin Randers

Midterm 2 Sample solution

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

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

Questions About Numbers. Number Systems and Arithmetic. Introduction to Binary Numbers. Negative Numbers?

If f(x, y) is a surface that lies above r(t), we can think about the area between the surface and the curve.

Binary Trees and Binary Search Trees

Introduction to Integration

Very sad code. Abstraction, List, & Cons. CS61A Lecture 7. Happier Code. Goals. Constructors. Constructors 6/29/2011. Selectors.

INTRODUCTION TO SIMPLICIAL COMPLEXES

CS350: Data Structures Tree Traversal

2014 Haskell January Test Regular Expressions and Finite Automata

Dr. D.M. Akbar Hussain

CS311H: Discrete Mathematics. Graph Theory IV. A Non-planar Graph. Regions of a Planar Graph. Euler s Formula. Instructor: Işıl Dillig

Data Structures and Algorithms for Engineers

Lists in Lisp and Scheme

Suffix Tries. Slides adapted from the course by Ben Langmead

a < a+ x < a+2 x < < a+n x = b, n A i n f(x i ) x. i=1 i=1

LEX5: Regexps to NFA. Lexical Analysis. CMPT 379: Compilers Instructor: Anoop Sarkar. anoopsarkar.github.io/compilers-class

Lecture Overview. Knowledge-based systems in Bioinformatics, 1MB602. Procedural abstraction. The sum procedure. Integration as a procedure

COMP 250 Fall recurrences 2 Oct. 13, 2017

Chapter 4 Trees. Theorem A graph G has a spanning tree if and only if G is connected.

Unit 5 Vocabulary. A function is a special relationship where each input has a single output.

1. SEQUENCES INVOLVING EXPONENTIAL GROWTH (GEOMETRIC SEQUENCES)

7.1 Introduction. A (free) tree T is A simple graph such that for every pair of vertices v and w there is a unique path from v to w

09 STACK APPLICATION DATA STRUCTURES AND ALGORITHMS REVERSE POLISH NOTATION

Stacks, Queues and Hierarchical Collections

F453 Module 7: Programming Techniques. 7.2: Methods for defining syntax

Friday, March 30. Last time we were talking about traversal of a rooted ordered tree, having defined preorder traversal. We will continue from there.

From Dependencies to Evaluation Strategies

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

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

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

Binary Trees Fall 2018 Margaret Reid-Miller

ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών. Lecture 3b Lexical Analysis Elias Athanasopoulos

Suffix trees, suffix arrays, BWT

What are suffix trees?

Stacks, Queues and Hierarchical Collections. 2501ICT Logan

Section Summary. Introduction to Trees Rooted Trees Trees as Models Properties of Trees

Alignment of Long Sequences. BMI/CS Spring 2012 Colin Dewey

Looking up objects in Pastry

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

Tree. Virendra Singh Indian Institute of Science Bangalore Lecture 11. Courtesy: Prof. Sartaj Sahni. Sep 3,2010

Math 142, Exam 1 Information.

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

A Tautology Checker loosely related to Stålmarck s Algorithm by Martin Richards

12-B FRACTIONS AND DECIMALS

Ma/CS 6b Class 1: Graph Recap

a(e, x) = x. Diagrammatically, this is encoded as the following commutative diagrams / X

P(r)dr = probability of generating a random number in the interval dr near r. For this probability idea to make sense we must have

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

Fig.25: the Role of LEX

Transcription:

COMP 250 Lecture 21 binry trees, expression trees Oct. 27, 2017 1

Binry tree: ech node hs t most two children. 2

Mximum number of nodes in binry tree? Height h (e.g. 3) 3

Mximum number of nodes in binry tree? Height h (e.g. 3) n = 1 + 2 + 4 + 8 + 2 h = 2 h+1 1 4

Minimum number of nodes in binry tree? Height h (e.g. 3) n = h + 1 5

clss BTree<T>{ BTNode<T> root; : clss BTNode<T>{ T e; BTNode<T> leftchild; BTNode<T> rightchild; : 6

Binry Tree Trversl (depth first) Rooted tree (lst lecture) Binry tree depthfirst(root){ if (root is not empty){ visit root for ech child of root depthfirst( child ) 7

Binry Tree Trversl (depth first) Rooted tree (lst lecture) Binry tree preorder(root){ if (root is not empty){ visit root for ech child of root preorder( child ) 8

Binry Tree Trversl (depth first) Rooted tree (lst lecture) Binry tree preorder(root){ if (root is not empty){ visit root for ech child of root preorder( child ) preorderbt (root){ if (root is not empty){ visit root preorderbt( root.left ) preorderbt( root.right ) 9

preorderbt (root){ if (root is not empty){ visit root preorderbt( root.left ) preorderbt( root.right ) postorderbt (root){ 10

preorderbt (root){ if (root is not empty){ visit root preorderbt( root.left ) preorderbt( root.right ) postorderbt (root){ if (root is not empty){ postorderbt(root.left) postorderbt(root.right) visit root 11

preorderbt (root){ if (root is not empty){ visit root preorderbt( root.left ) preorderbt( root.right ) postorderbt (root){ if (root is not empty){ postorderbt(root.left) postorderbt(root.right) visit root inorderbt (root){ if (root is not empty){ inorderbt(root.left) visit root inorderbt(root.right) 12

preorderbt (root){ if (root is not empty){ visit root preorderbt( root.left ) preorderbt( root.right ) postorderbt (root){ if (root is not empty){ postorderbt(root.left) postorderbt(root.right) visit root inorderbt (root){ if (root is not empty){ inorderbt(root.left) visit root inorderbt(root.right) 13

Exmple Pre order: b c In order: d f g Post order: e 14

Exmple Pre order: b d e c f g b c In order: d f g Post order: e 15

Exmple Pre order: b d e c f g b c In order: d e b f c g d f g Post order: e 16

Exmple Pre order: b d e c f g b c In order: d e b f c g d f g Post order: e d b f g c e 17

Expression Tree e.g. 3 + 4 * 2 3 + (4 * 2) + 3 * 4 2 18

Expression Tree e.g. 3 + 4 * 2 (3 + 4) * 2 3 + (4 * 2) * + 2 3 4 + 3 * 4 2 19

My Windows clcultor sys 3 + 4 * 2 = 14. Why? (3 + 4) * 2 = 14. Wheres. if I google 3+4*2, I get 11. 3 + (4*2) = 11. 20

We cn mke expressions using binry opertors +, -, *, /, ^ e.g. b / c + d * e ^ f ^ g ^ is exponentition: e ^ f ^ g = e ^ (f ^ g) We don t consider unry opertors e.g. 3 + -4 = 3 + (-4) Opertor precedence ordering mkes brckets unnecessry. ( (b / c)) + (d * (e ^ (f ^ g))) 21

Expression Tree b / c + d * e ^ f ^ g ( (b / c)) + (d (e ^ (f ^ g))) + Internl nodes re opertors. Leves re opernds. * / d b c e f g 22

An expression tree cn be wy of thinking bout the ordering of opertions used when evluting n expression. But to be concrete, let s ssume we hve binry tree dt structure. + * / d b c e f g 23

If we trverse n expression tree, nd print out the node lbel, wht is the expression printed out? / + d * b c e f g preorder trversl gives 24

If we trverse n expression tree, nd print out the node lbel, wht is the expression printed out? / + d * b c e f g preorder trversl gives : + / b c * d ^ e ^ f g 25

If we trverse n expression tree, nd print out the node lbel, wht is the expression printed out? / + d * b c e f g preorder trversl gives : + / b c * d ^ e ^ f g inorder trversl gives : 26

If we trverse n expression tree, nd print out the node lbel, wht is the expression printed out? / + d * b c e f g preorder trversl gives : inorder trversl gives : + / b c * d ^ e ^ f g b / c + d * e ^ f ^ g 27

If we trverse n expression tree, nd print out the node lbel, wht is the expression printed out? / + d * b c e f g preorder trversl gives : inorder trversl gives : + / b c * d ^ e ^ f g b / c + d * e ^ f ^ g postorder trversl gives : 28

If we trverse n expression tree, nd print out the node lbel, wht is the expression printed out? / + d * b c e f g preorder trversl gives : inorder trversl gives : + / b c * d ^ e ^ f g b / c + d * e ^ f ^ g postorder trversl gives : b c / - d e f g ^ ^ * + 29

Prefix, infix, postfix expressions * b prefix: infix: * b * b postfix: b * 30

Infix, prefix, postfix expressions bseexp = vrible integer op = + - * / ^ preexp = bseexp op preexp preexp 31

Infix, prefix, postfix expressions bseexp = vrible integer op = + - * / ^ preexp = bseexp op preexp prefexp inexp = bseexp inexp op inexp postexp = bseexp postexp postexp op Use only one. 32

If we trverse n expression tree, nd print out the node lbel, wht is the expression printed out? (sme question s four slides go) b / c + d * e f g preorder trversl gives prefix expression: inorder trversl gives infix expression: + / b c * d ^ e ^ f g b / c + d * e ^ f ^ g postorder trversl gives postfix expression: b c / - d e f g ^ ^ * + 33

Prefix expressions clled Polish Nottion (fter Polish logicin Jn Lucsewicz 1920 s) Postfix expressions re clled Reverse Polish nottion (RPN) Some clcultors (esp. Hewlett Pckrd) require users to input expressions using RPN. 34

Prefix expressions clled Polish Nottion (fter Polish logicin Jn Lucsewicz 1920 s) Postfix expressions re clled Reverse Polish nottion (RPN) Some clcultors (esp. Hewlett Pckrd) require users to input expressions using RPN. Clculte 5 * 4 + 3 : 5 <enter> 4 <enter> * <enter> 3 <enter> + <enter> No = symbol on keybord. 35

Suppose we re given n expression tree. How cn we evlute the expression? + * / d b c e f g 36

We use postorder trversl (recursive lgorithm): evlexptree(root){ if (root is lef) // root is number return vlue else{ // the root is n opertor firstopernd = evlexptree( root.leftchild ) secondopernd = evlexptree( root.rightchild ) return evlute(firstopernd, root, secondopernd) 37

Wht if we re not given n expression tree? Infix expressions re wkwrd to evlute becuse of precedence ordering. Infix expressions with brckets re reltively esy to evlute e.g. Assignment 2. 38

Assignment 2 (ignore cse of ++, --) for ech token in expression { if token is number vluestck.push(token) else if token is ) { // then you hve binry expression opertor = opstck.pop() opernd2 = vluestck.pop() opernd1 = vluestck.pop() numstck.push( opernd1 opertor opernd2) return vluestck.pop() 39

Assignment 2 (ignore cse of ++, --) for ech token in expression { if token is number vluestck.push(token) else if token is ) { // then you hve binry expression opertor = opstck.pop() opernd2 = vluestck.pop() opernd1 = vluestck.pop() numstck.push( opernd1 opertor opernd2) return vluestck.pop() 40

Infix expressions with brckets re reltively esy to evlute e.g. with two stcks s in Assignment 2. Postfix expressions without brckets re esy to evlute. Use one stck, nmely for vlues (not opertors). 41

Exmple: Use stck to evlute postfix expression: b c / - d e f g ^ ^ * + + * / d stck over time b c This expression tree is not given. It is shown here so tht you cn visulize the expression more esily. e f g 42

Exmple: Use stck to evlute postfix expression: b c / - d e f g ^ ^ * + b b c / + d * stck over time b c This expression tree is not given. It is shown here so tht you cn visulize the expression more esily. e f g 43

Use stck to evlute postfix expression: b c / - d e f g ^ ^ * + stck over time b b c (b c / ) We don t push opertor onto stck. Insted we pop vlue twice, evlute, nd push. b / c + d * e f g 44

Useon: b c / - d e f g ^ ^ * + stck over time b b c (b c / ) ( ( b c / ) - ) Now there is one vlue on the stck. b / c + d * e f g 45

tck to evl: b c / - d e f g ^ ^ * + stck over time b b c (b c / ) ( ( b c / ) - ) : ( ( b c / ) - ) d e f g b / c + d * e f g Now there re five vlues on the stck. 46

Use stck to evlute postfix expression: b c / - d e f g ^ ^ * + stck over time b b c ( b c / ) ( ( b c / ) - ) : ( ( b c / ) - ) d e f g ( ( b c / ) - ) d e (f g ^) b / c + d * e f g Now there re four vlues on the stck. 47

Use stck to evlute postfix expression: b c / - d e f g ^ ^ * + stck over time b b c ( b c / ) ( ( b c / ) - ) : ( ( b c / ) - ) d e f g ( ( b c / ) - ) d e (f g ^) ( ( b c / ) - ) d ( e (f g ^) ^ ) b / c + d * e f g Three vlues on the stck. 48

Use stck to evlute postfix expression: b c / - d e f g ^ ^ * + stck over time b b c ( b c / ) ( ( b c / ) - ) : ( ( b c / ) - ) d e f g ( ( b c / ) - ) d e (f g ^) ( ( b c / ) - ) d ( e (f g ^) ^ ) ( ( b c / ) - ) (d ( e (f g ^) ^ ) * ) b / c + d * e f g Two vlues on the stck. 49

Use stck to evlute postfix expression: b c / - d e f g ^ ^ * + stck over time b b c ( b c / ) / ( ( b c / ) - ) b : ( ( b c / ) - ) d e f g ( ( b c / ) - ) d e (f g ^) ( ( b c / ) - ) d ( e (f g ^) ^ ) ( ( b c / ) - ) (d ( e (f g ^) ^ ) * ) ( ( ( b c / ) - ) (d ( e (f g ^) ^ ) * ) + ) c + d * e f g One vlue on the stck. 50

Algorithm: Use stck to evlute postfix expression Let expression be list of elements. s = empty stck cur = first element of expression list while (cur!= null){ if ( cur.element is bse expression ) s.push( cur.element ) else{ // cur.element is n opertor opernd2 = s.pop() opernd1 = s.pop() opertor = cur.element // for clrity only s.push( evlute( opernd1, opertor, opernd2 ) ) cur = cur.next 51

Algorithm: Use stck to evlute postfix expression Let expression be list of elements. s = empty stck cur = first element of expression list while (cur!= null){ if ( cur.element is bse expression ) s.push( cur.element ) else{ // cur.element is n opertor opernd2 = s.pop() opernd1 = s.pop() opertor = cur.element // for clrity only s.push( evlute( opernd1, opertor, opernd2 ) ) cur = cur.next 52

ASIDE There re mny vritions of expression tree problems. e.g. Define n lgorithm tht computes postfix expression directly from n infix expression with no brckets. This is not so obvious if you hve to respect precedence ordering e.g. +, -, *, /, ^ http://wcipeg.com/wiki/shunting_yrd_lgorithm 53