Professional Documents
Culture Documents
Trees
Binary Trees
1/53
Outline
Trees
Binary Trees
Trees
Tree Definition
Tree ADT
Linked Structure for Trees
Traversals
Binary Trees
Binary Tree Definition
Binary Tree Examples
Binary Tree ADT
Binary Tree Properties
Linked Structure for Binary Trees
Array-Based Implementation of Binary Trees
Preorder Traversal of a Binary Tree
Postorder Traversal of a Binary Tree
Evaluate Arithmetic Expressions
Inorder Traversal of Binary Tree
2/53
Outline
Trees
Binary Trees
3/53
Outline
Trees
Binary Trees
Trees
4/53
Outline
Trees
Binary Trees
Tree Definition
Tree Definition
5/53
Outline
Trees
Binary Trees
Tree Definition
Tree Definition II
6/53
Outline
Trees
Binary Trees
Tree Definition
Tree Terminology
Root - node without parent (A)
Internal node - node with at least one child (A, B, C, F)
External node (a.k.a. leaf ) - node without children (E, I, J,
K, G, H, D)
Ancestors of a node - parent, grandparent,
grand-grandparent, etc.
Descendant of a node - child, grandchild, grand-grandchild,
etc.
Subtree - tree consisting of a node and its descendants
Edge - Pair of nodes (u,v) where u is the parent of v, or vice
versa
Path - Sequence of nodes such that any two consecutive
nodes in the sequence form an edge
Computer Science 3A - CSC3A10
7/53
Outline
Trees
Binary Trees
Tree Definition
Tree Terminology II
8/53
Outline
Trees
Binary Trees
Tree Definition
Depth of node v
9/53
Outline
Trees
Binary Trees
Tree Definition
Tree Terminology IV
Height of tree
10/53
Outline
Trees
Binary Trees
Tree ADT
Tree ADT
We use positions to abstract nodes, where Positions are defined
relative to neighbouring position.
Generic methods:
integer size()
boolean isEmpty()
Iterator elements()
Iterator positions()
Accessor methods:
position root()
position parent(p)
positionIterator children(p)
Computer Science 3A - CSC3A10
11/53
Outline
Trees
Binary Trees
Tree ADT
Tree ADT II
Query methods:
boolean isInternal(p)
boolean isExternal(p)
boolean isRoot(p)
Update methods:
Object replace(p,o)
Update methods may be defined by data structures implementing
the Tree ADT
12/53
Outline
Trees
Binary Trees
13/53
Outline
Trees
Binary Trees
14/53
Outline
Trees
Binary Trees
15/53
Outline
Trees
Binary Trees
Traversals
Preorder Traversal
16/53
Outline
Trees
Binary Trees
Traversals
Preorder Traversal II
1 Algorithm preOrder ( v )
2
visit (v)
3
f o r each c h i l d w of v
4
p r e o r d e r (w)
preOrder Traversal
17/53
Outline
Trees
Binary Trees
Traversals
18/53
Outline
Trees
Binary Trees
Traversals
Postorder Traversal
19/53
Outline
Trees
Binary Trees
Traversals
Postorder Traversal II
1 Algorithm postOrder ( v )
2 f o r each c h i l d w of v
3
p o s t O r d e r (w)
4 visit (v)
preOrder Traversal
20/53
Outline
Trees
Binary Trees
Traversals
21/53
Outline
Trees
Binary Trees
Binary Trees
22/53
Outline
Trees
Binary Trees
23/53
Outline
Trees
Binary Trees
24/53
Outline
Trees
Binary Trees
25/53
Outline
Trees
Binary Trees
26/53
Outline
Trees
Binary Trees
Decision Tree
27/53
Outline
Trees
Binary Trees
Decision Tree II
28/53
Outline
Trees
Binary Trees
The BinaryTree ADT extends the Tree ADT i.e. it inherits all the
methods of the Tree ADT. The additional methods include:
position left(p)
position right(p)
boolean hasLeft(p)
boolean hasRight(p)
Update methods may be defined by data structures implementing
the BinaryTree ADT
29/53
Outline
Trees
Binary Trees
Properties:
Notation:
n - number of nodes
e - number of external
nodes
i - number of internal
nodes
h - height
30/53
e =i +1
n = 2e 1
hi
h (n 1)/2
e 2h
h log2 e
h log2 (n + 1) 1
Outline
Trees
Binary Trees
31/53
Outline
Trees
Binary Trees
32/53
Outline
Trees
Binary Trees
33/53
Outline
Trees
Binary Trees
34/53
Outline
Trees
Binary Trees
35/53
Outline
Trees
Binary Trees
36/53
Outline
Trees
Binary Trees
1
2
3
4
5
6
A l g o r i t h m b i n a r y P r e O r d e r (T , v )
visit (v)
i f hasLeft (v)
b i n a r y P r e O r d e r (T , l e f t ( v ) )
i f hasRight ( v )
b i n a r y P r e O r d e r (T , r i g h t ( v ) )
37/53
Outline
Trees
Binary Trees
1
2
3
4
5
6
A l g o r i t h m b i n a r y P o s t O r d e r (T , v )
i f hasLeft (v)
b i n a r y P o s t O r d e r (T , l e f t ( v ) )
i f hasRight ( v )
b i n a r y P o s t O r d e r (T , r i g h t ( v ) )
visit (v)
38/53
Outline
Trees
Binary Trees
39/53
Outline
Trees
Binary Trees
40/53
Outline
Trees
Binary Trees
1
2
3
4
5
6
7
8
Algorithm evalExpr ( v )
if isExternal (v)
return v . element ()
else
x = evalExpr ( l e f t C h i l d (v) )
y = evalExpr ( rightChild (v) )
op = o p e r a t o r s t o r e d a t v
r e t u r n x op y
41/53
Outline
Trees
Binary Trees
42/53
Outline
Trees
Binary Trees
43/53
Outline
Trees
Binary Trees
1
2
3
4
5
6
Algorithm inOrder ( v )
i f hasLeft (v)
inOrder ( l e f t ( v ) )
visit (v)
i f hasRight ( v )
inOrder ( r i g h t ( v ) )
Inorder Traversal
44/53
Outline
Trees
Binary Trees
45/53
Outline
Trees
Binary Trees
46/53
Outline
Trees
Binary Trees
1
2
3
4
5
6
7
8
A l g o r i t h m E u l e r T o u r (T , v )
v i s i t L e f t (T , v )
i f T. hasLeft ( v )
E u l e r T o u r (T , l e f t ( v ) )
v i s i t B e l o w (T , v )
i f T. hasRight ( v )
E u l e r T o u r (T , r i g h t ( v ) )
v i s i t R i g h t (T , v )
47/53
Outline
Trees
Binary Trees
48/53
Outline
Trees
Binary Trees
49/53
(1)
Outline
Trees
Binary Trees
1
2
3
4
5
6
7
8
9
10
11
A l g o r i t h m p r i n t E x p r e s s i o n (T , v )
i f T. i s I n t e r n a l ( v )
print ( ( )
i f T. hasLeft ( v )
p r i n t E x p r e s s i o n (T , T . l e f t ( v ) )
i f T. i s I n t e r n a l ( v )
p r i n t the operator or value stored at v
i f T. hasRight ( v )
p r i n t E x p r e s s i o n (T , T . r i g h t ( v ) )
i f T. i s I n t e r n a l ( v )
print ( ) )
50/53
Outline
Trees
Binary Trees
51/53
Outline
Trees
Binary Trees
1
2
3
4
5
6
7
8
9
10
A l g o r i t h m t e m p l a t e E u l e r T o u r (T , v )
r = new o b j e c t o f t y p e T o u r R e s u l t
v i s i t L e f t (T , v , r )
i f T. hasLeft ( v )
r . l e f t = t e m p l a t e E u l e r T o u r (T , T . l e f t ( v ) )
v i s i t B e l o w (T , v , r )
i f T. hasRight ( v )
r . r i g h t = t e m p l a t e E u l e r T o u r (T , T . r i g h t ( v ) )
v i s i t R i g h t (T , v , r )
return r . out
52/53
Outline
Trees
Binary Trees
Exercises
Exercises
Reinforcement exercises:
R7.2
R7.10 - R7.14
R7.17
R7.20
Creativity exercises:
C7.1 - C7.7
C7.9
C7.12
C7.21
C7.24
C7.25
C7.28 - C7.30
Computer Science 3A - CSC3A10
53/53