Trees: Make Money Fast!
Trees: Make Money Fast!
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
e number of n 2e 1
external nodes
h i
i number of internal
nodes h (n 1)2
h height e 2h
h log e
2
h log2 (n 1) 1
2 8
1 4 7 9
3 5
subtree
print “)“ after traversing right print(v.element ())
subtree
if isInternal (v)
inOrder (rightChild (v))
print (“)’’)
2 3 b
((2 (a 1)) (3 b))
a 1
12/07/21 16:36 Trees 14
Evaluate Arithmetic Expressions
Specialization of a postorder Algorithm evalExpr(v)
traversal if isExternal (v)
recursive method returning return v.element ()
the value of a subtree else
when visiting an internal x evalExpr(leftChild (v))
node, combine the values
y evalExpr(rightChild (v))
of the subtrees
operator stored at v
return x y
2 3 2
5 1
12/07/21 16:36 Trees 15
Euler Tour Traversal
Generic traversal of a binary tree
Includes a special cases the preorder, postorder and inorder traversals
Walk around the tree and visit each node three times:
on the left (preorder)
L R
B
2 3 2
5 1
A D F
B
A D F
C E
C E
12/07/21 16:36 Trees 19
Data Structure for Binary Trees
A node is represented by
an object storing
Element
Parent node
Left child node B
Right child node
Node objects implement
the Position ADT
B A D
A D
C E C E