expression trees Oct. 24, 2016 1 Binary tree: each node has at - - PowerPoint PPT Presentation

expression trees
SMART_READER_LITE
LIVE PREVIEW

expression trees Oct. 24, 2016 1 Binary tree: each node has at - - PowerPoint PPT Presentation

COMP 250 Lecture 19 binary trees, expression trees Oct. 24, 2016 1 Binary tree: each node has at most two children. 2 Maximum number of nodes in a binary tree? Height (e.g. 3) 3 Maximum number of nodes in a binary tree? Height


slide-1
SLIDE 1

1

COMP 250

Lecture 19

binary trees, expression trees

  • Oct. 24, 2016
slide-2
SLIDE 2

Binary tree:

each node has at most two children.

2

slide-3
SLIDE 3

Maximum number of nodes in a binary tree?

3

Height ℎ (e.g. 3)

slide-4
SLIDE 4

Maximum number of nodes in a binary tree?

4

Height ℎ (e.g. 3)

𝑜 = 1 + 2 + 4 + 8 + 2ℎ = 2ℎ+1 − 1

slide-5
SLIDE 5

Minimum number of nodes in a binary tree?

5

𝑜 = ℎ + 1

Height ℎ (e.g. 3)

slide-6
SLIDE 6

6

class BTree<T>{ BTNode<T> root; : class BTNode<T>{ T e; BTNode<T> leftchild; BTNode<T> rightchild; : } }

slide-7
SLIDE 7

Binary Tree Traversal (depth first)

7

preorder(root){ if (root is not empty){ visit root for each child of root preorder( child ) } }

Rooted tree Binary tree (last lecture)

slide-8
SLIDE 8

Binary Tree Traversal (depth first)

8

preorderBT (root){ if (root is not empty){ visit root preorderBT( root.left ) preorderBT( root.right ) } } preorder(root){ if (root is not empty){ visit root for each child of root preorder( child ) } }

Rooted tree Binary tree (last lecture)

slide-9
SLIDE 9

9

inorderBT (root){ if (root is not empty){ inorderBT(root.left) visit root inorderBT(root.right) } } 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 } }

slide-10
SLIDE 10

Example

10

a b c d e f g

Pre order: abdecfg In order: debafcg Post order: edbfgca

slide-11
SLIDE 11

Example of binary tree: “Syntax (sub)Tree” of Assignment 2

11

statement if boolean then statement else statement end

statement = if boolean then statement else statement end | assignment

slide-12
SLIDE 12

12 statement if boolean then statement else statement end if boolean then statement else statement end if boolean then statement else statement end

The statement nodes form a binary (sub)tree.

slide-13
SLIDE 13

Expression Tree

13

+ 3 * 4 2 * + 2 3 4

e.g. 3 + 4 * 2

(3 + 4) * 2 3 + (4 * 2)

slide-14
SLIDE 14

14

My Windows calculator says 3 + 4 * 2 = 14. Why? (3 + 4) * 2 = 14. Whereas…. if I google “3+4*2”, I get 11. 3 + (4*2) = 11.

slide-15
SLIDE 15

Example of expression tree

15

a – b / c + d * e ^ f ^ g

^ is exponentiation We consider binary operators only e.g. we don’t consider 3 + -4 = 3 + (-4) Precedence ordering makes brackets unnecessary. i.e. (a – (b / c)) + (d * (e ^ (f ^ g)))

slide-16
SLIDE 16

Expression Tree

16

a – b / c + d * e ^ f ^ g ≡ (a – (b / c)) + (d ∗ (e ^ (f ^ g))) e f g / * a d + b c

Internal nodes are operators. Leaves are numbers.

slide-17
SLIDE 17

Infix, prefix, postfix expressions

17

infix: a*b prefix: *ab postfix: ab*

* a b

slide-18
SLIDE 18

Infix, prefix, postfix expressions

18

baseExp = variable | integer

  • p = + | - | * | / | ^

inExp = baseExp | inExp op inExp preExp = baseExp | op preExp prefExp postExp = baseExp | postExp postExp

  • p

Use one.

slide-19
SLIDE 19

19

inorder traversal gives infix expression: a – b / c + d * e ^ f ^ g preorder traversal gives prefix expression: + – a / b c * d ^ e ^ f g postorder traversal gives postfix expression: a b c / - d e f g ^ ^ * +

e f g / * a d + b c If we traverse an expression tree, in which order do we ‘visit’ nodes ?

slide-20
SLIDE 20

20

If we were given an expression tree, then how would we evaluate the expression ?

e f g / * a d + b c

slide-21
SLIDE 21

21

If we were given an expression tree, then we could evaluate it using a postorder traversal:

evalExpTree(root){ if (root is a leaf) // root is a number return value else{ // the root is an operator firstOperand = evalExpTree( root.leftchild ) secondOperand = evalExpTree( root.rightchild ) return evaluate(firstOperand, root, secondOperand) } }

However, in practice we are not given an expression tree.

slide-22
SLIDE 22

How to evaluate expressions?

22

Infix expressions are awkward to evaluate because of precedence ordering.

ASIDE: One can convert an infix expression to a postfix expression: http://wcipeg.com/wiki/Shunting_yard_algorithm Details omitted here. For your interest only.

We next show how to evaluate a postfix expression using a stack.

slide-23
SLIDE 23

23

Use a stack to evaluate postfix expression: a b c / - d e f g ^ ^ * +

a ab abc a(bc/) ( a(bc/) - ) ( a(bc/) - ) d ( a(bc/) - ) d e ( a(bc/) - ) d e f ( a(bc/) - ) d e f g ( a(bc/) - ) d e (f g ^) ( a(bc/) - ) d (e (f g ^) ^) ( a(bc/) - ) (d (e (f g ^) ^) * ) (( a(bc/) - ) (d (e (f g ^) ^) * ) +) stack

  • ver

time

e f g / * a d + b c

slide-24
SLIDE 24

24

Algorithm: Use a stack to evaluate a postfix expression

Let expression be a list of elements. s = empty stack cur = head of expression list while (cur != null){ if ( cur.element is a base expression ) s.push( cur.element ) else{ // cur.element is an operator

  • perand2 = s.pop()
  • perand1 = s.pop()
  • perator = cur.element

// for clarity only s.push( evaluate( operand1, operator, operand2 ) ) } cur = cur.next }

slide-25
SLIDE 25

25

Prefix expressions called “Polish Notation”

(after Polish logician Jan Lucasewicz 1920’s)

Postfix expressions are called “Reverse Polish notation” (RPN)

Some calculators (esp. Hewlett Packard) require users to input expressions using RPN.

5*4+3 ? 5 <enter> 4 <enter> * <enter> 3 <enter> + <enter> No “=“ symbol needed

  • n keyboard.