11.1 Trees Introduction
11.1 Trees Introduction
2 5 9
1. Motivations 2. Methods References
6 7 8
3 4
2.1 Stock 2.2 Ponzi 2.3 Bank
1.1 Greed 1.2 Avidity
Fraud Scheme Robbery
8
3 7
todo.txt
homeworks/ programs/
1K
1 2 4 5 6
h1c.doc h1nc.doc DDR.java Stocks.java Robot.java
3K 2K 10K 25K 20K
× ×
2 − 3 b
a 1
n h ≥ log2 e
n h ≥ log2 (n + 1) − 1
2 8
1 4 7 9
3 5
2 − 3 b
((2 × (a − 1)) + (3 × b))
a 1
© 2013 Goodrich, Tamassia, Goldwasser Trees 17
Evaluate Arithmetic Expressions
q Specialization of a postorder Algorithm evalExpr(v)
traversal if is_leaf (v)
n recursive method returning return v.element ()
the value of a subtree else
n when visiting an internal x ← evalExpr(left (v))
node, combine the values
y ← evalExpr(right (v))
of the subtrees
◊ ← operator stored at v
+ return x ◊ y
× ×
2 − 3 2
5 1
© 2013 Goodrich, Tamassia, Goldwasser Trees 18
Euler Tour Traversal
q Generic traversal of a binary tree
q Includes a special cases the preorder, postorder and inorder traversals
q Walk around the tree and visit each node three times:
n on the left (preorder)
L × R ×
B
2 − 3 2
5 1
A D F
C E ∅ ∅
C E
© 2013 Goodrich, Tamassia, Goldwasser Trees 20
Linked Structure for Binary Trees
q A node is represented
by an object storing ∅
n Element
n Parent node
n Left child node B
n Right child node
q Node objects implement ∅ ∅
the Position ADT
B A D
A D ∅ ∅ ∅ ∅
C E C E
A B D … G H …
2 3
0 1 2 3 10 11 B D