Beruflich Dokumente
Kultur Dokumente
Trees
Definition: Let be a set, and let be a relation on .
We say that is a tree if there is a vertex 0 in with
the property that
1) there exists a unique path in from 0 to every
other vertex in ,
2) no path from 0 to 0 .
Root 1 Level 0
Parent
2 3 4 Level 1
Leaf Height 3
Siblings
5 6 Level 2
Child /
Offspring
7 Level 3
is irreflexive
is asymmetric
2 , 3 , 2 , 1 , 4 , 5 , 4 , 6 ,
=
5 , 8 , 6 , 7 , 4 , 2 , 7 , 9 , 7 , 10
Definition:
If all vertices of , other than the leaves, have
exactly offspring, we say that is a complete
-tree.
A complete 2-tree is called a completed binary tree.
1 2 3
4 5
4 5
= , , , ,
= , , , , , , ,
= 1, 2, 3, 4, 5, 6
= 2,1 , 3,4 , 5,2 , 6,5 , 6,3
1 2 3
4 5 6 7 8 9
11 12 13 14
10 15
- -
x - +
2 b b 2 3 b
3 1 4 + 7 + 2 7 +
2 3
1 2 1 3
3 3
1 2 3
1 2 3
L R
R L R
R L L R
Tree search
The process of visiting each vertex of a tree in some
specific order will be called searching the tree or
performing a tree search.
Step 1: Visit
B H
C E I K
3 5 6 9 11
2 D F G 10 L
4 8 J
1 7
A B C D E F G H I J K L
- +
/
a c
b
2 3 5
d e
1 7 8
6
4
- a b + c / d e
S. Turaev, CSC 1700 Discrete Mathematics 23
Tree Searching
Prefix or Polish form:
+ / ( = 6, = 4, = 5, = 2, = 2)
1. 6 4 + 5 / 2 2
2. 2 + 5 / 2 2 replacing 6 4 by 2 since 6 4 = 2
3. 2 + 5 1 replacing / 2 2 by 1 since 2/2 = 1
4. 2 6 replacing + 5 1 by 6 since 5 + 1 = 6
5. 12 replacing 2 6 by 12 since
2 6 = 12
S. Turaev, CSC 1700 Discrete Mathematics 24
Tree Searching
Algorithm INORDER
INORDER: + /
- + POSTORDER: / +
/
a c
b
d e
( ) ( + (/))
or
( ( + /))
1. 2 1 3 4 2 / +
y z
t
s
u
y z
t
s
u
y z
t
s
u
, , , , , , , , , , , ,
=
, , , , , , ,
+ 3 4 7 2 12 3 6 4
3 2 4 3 + 15 2 6 3
43254253
374 9652+
JBACDIHEGF
CATSANDDOGS
SEARCHING
TREEHOUSE