10-Trees
10-Trees
Trees
Mamma
l
Do Pi Ca
g g t
I J K
the parent of v
E F G H depth
2
I J K depth
3
I J K
A D F
C E
© 2014 Goodrich, Tamassia, C E
Goldwasser Trees 12
Java Interface
Methods for a Tree interface:
1 Make Money
Fast!
2 5 9
1. 2. Reference
Motivations Methods s
6 7 8
3 4 2.1 2.2 2.3
1.1 1.2
Stock Ponzi Bank
Greed Avidity
Fraud Scheme Robbery
© 2014 Goodrich, Tamassia,
Goldwasser Trees 15
Preorder Traversal
Application: Print a structured
document
Algorithm preOrderPrint(v,indent) Output of
2 5 9
1. 2. Reference
Motivations Methods s
6 7 8
3 4 2.1 2.2 2.3
1.1 1.2
Stock Ponzi Bank
Greed Avidity
Fraud Scheme Robbery
© 2010 Goodrich, Tamassia Trees 16
Postorder Traversal
Runtime requirements?? Algorithm postOrder(v)
If visit(v) is O(1), then for each child w of v
preorder(v) is O(n).
Application: compute postOrder (w)
space used by files in a visit(v)
directory and its
subdirectories 9 cs16
/
8
3 7 todo.tx
homeworks programs
t
/ /
1K
1 2 4 5 6
h1c.do h1nc.do DDR.jav Stocks.jav Robot.jav
c c a a a
3K 2K 10K 25K 20K
© 2014 Goodrich, Tamassia,
Goldwasser Trees 17
Binary Trees
A binary tree is a tree with
the following properties:
Each internal node has at
most two children (exactly
A
two for proper binary trees)
The children of a node are
an ordered pair B C
A D
C E C E
© 2014 Goodrich, Tamassia,
Goldwasser Trees 24
Arithmetic Expression Tree
Binary tree associated with an arithmetic
expression
internal nodes: operators
external nodes: operands
left subtree ≡ left operand
right subtree ≡ right operand
Example: arithmetic expression tree for
+ the
expression (2 (a - 1) + (3 b))
2 - 3 b
+ * 2 – a 1 * 3 b
Preorder traversal:
+
2 a 1 - * 3 b * +
Postorder traversal:
2 - 3 b
2 * a – 1 + 3 * b
Inorder traversal:
a 1
© 2010 Goodrich, Tamassia Trees 26
Useful Facts
Let T be a binary tree.
For every k ≥ 0, there are
no more than 2k nodes in
level k.
A level 0:
Let T be a binary tree n20
with λ levels.
level 1: n21
Then T has no more than B C
2λ – 1 nodes.
Let T be a binary tree level 2:
E F G H n22
with N nodes.
Then the number of levels
is at least log (N + 1). level 3:
I K
n23
27
Properties of Proper Binary
Trees
#nodes = 2 * #leaves - 1
2 8
1 4 7 9
3 5
left subtree
print “)“ after traversing print(v.element ())
right subtree
if right(v) ≠ null
+ printExpression(right(v))
print (“)’’)
2 - 3 b
((2 (a - 1) + (3 b))
a 1
© 2014 Goodrich, Tamassia,
Goldwasser Trees 31
Evaluate Arithmetic
Expressions
Specialization of a Algorithm evalExpr(v)
postorder traversal if isExternal (v)
recursive method return v.element ()
returning the value of a else
subtree x evalExpr(left(v))
when visiting an y evalExpr(right(v))
internal node, combine
operator stored at v
the values of the
subtrees return x y
+
2 - 3 2
5 1
© 2014 Goodrich, Tamassia,
Goldwasser Trees 32
© 2014 Goodrich, Tamassia,
Goldwasser Trees 33
© 2014 Goodrich, Tamassia,
Goldwasser Trees 34
© 2014 Goodrich, Tamassia,
Goldwasser Trees 35
© 2014 Goodrich, Tamassia,
Goldwasser Trees 36
Structure Tree
Implementation
Operation Time
size, isEmpty
O(1)
iterator, positions
O(n)
replace
O(1)
root, parent, children, left, right
O(1)
isInternal, isExternal, isRoot
O(1)
0 1 2 3 4 5 6 7 8 9 10
1 2
B D
rank(node) = 2 rank(parent(node)) + 1
if node is the right child of parent(node),
9 10
rank(node) = 2 rank(parent(node)) + 2
G H