Sie sind auf Seite 1von 37

Trees

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 .

Definition: 0 is called the root of the tree, and T is


referred to as a rooted tree denoted by , 0 .

S. Turaev, CSC 1700 Discrete Mathematics 2


Trees
Theorem: Let , 0 be a rooted tree. Then

1. There are no cycle in .

2. Vertex 0 is the only root of .

3. Each vertex in , other than 0 ,


has in-degree one, and 0 has in-degree zero.

S. Turaev, CSC 1700 Discrete Mathematics 3


Levels, Parent-Offspring, Siblings
Level 1 vertices: the vertices of
the edges beginning at 0 (level 0)
1
Level vertices: the vertices of the
edges beginning at those level 1
vertices
2 3 4
Height of a tree: the largest level number
of a tree

6 Parent-offspring: for all pairs (, ) in ,


5
is called the parent of and is called
the offspring of

7 Siblings: the vertices that have the same


parent
Leaves: the vertices that have no
offspring
S. Turaev, CSC 1700 Discrete Mathematics 4
Levels, Parent-Offspring, Siblings

Root 1 Level 0

Parent
2 3 4 Level 1

Leaf Height 3
Siblings
5 6 Level 2

Child /
Offspring
7 Level 3

S. Turaev, CSC 1700 Discrete Mathematics 5


Trees
Theorem: Let , 0 be a rooted tree. Then

is irreflexive

is asymmetric

If , in and , in , then , is not


in , for all , and in .

S. Turaev, CSC 1700 Discrete Mathematics 6


Trees
Example: Let = 1 , 2 , 3 , , 10 and let

2 , 3 , 2 , 1 , 4 , 5 , 4 , 6 ,
=
5 , 8 , 6 , 7 , 4 , 2 , 7 , 9 , 7 , 10

Show that is a rooted tree and identify the root.

S. Turaev, CSC 1700 Discrete Mathematics 7


-trees
Definition:
If is a positive integer, we say that a tree is an -
tree if every vertex has at most offspring.
A 2-tree is called a binary tree.

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.

S. Turaev, CSC 1700 Discrete Mathematics 8


Subtrees
Let , 0 be a rooted tree on the set , and let
be a vertex of .

Let be the set consisting of and all its


descendants, i.e., all vertices of that can be reached
by a path beginning at .

Let be the restriction of the relation to ,


that is ( ).

Delete all vertices that are not descendants of and all


edges that do not begin and end at any such vertex.

S. Turaev, CSC 1700 Discrete Mathematics 9


Subtrees
Theorem: If , 0 is a rooted tree and in , then
is also a rooted tree with root .

We will say that is the subtree of beginning at .


0

1 2 3

4 5

S. Turaev, CSC 1700 Discrete Mathematics 10


Subtrees
Theorem: If , 0 is a rooted tree and in , then
is also a rooted tree with root .

We will say that is the subtree of beginning at .


0

4 5

S. Turaev, CSC 1700 Discrete Mathematics 11


Exercises
Exercise 1: Determine if is a tree and, if it is, find the
root.

= , , , ,
= , , , , , , ,

= 1, 2, 3, 4, 5, 6
= 2,1 , 3,4 , 5,2 , 6,5 , 6,3

S. Turaev, CSC 1700 Discrete Mathematics 12


Exercises
Exercise 2: Consider the rooted tree , 0 .

1 2 3

4 5 6 7 8 9

11 12 13 14

10 15

S. Turaev, CSC 1700 Discrete Mathematics 13


Exercises
Exercise 2: Consider the rooted tree , 0 .
1. List all level-3 vertices
2. List all leaves
3. What are the siblings of 8?
4. What are the descendants of 3?
5. Compute 2
6. Compute 3
7. What is the height of , 0 ?

S. Turaev, CSC 1700 Discrete Mathematics 14


Labeled Trees
Example: Use a tree to denote the following algebraic
expression
3 2 + 2 3 +
+

- -

x - +

2 b b 2 3 b

S. Turaev, CSC 1700 Discrete Mathematics 15


Labeled Trees
Example: Use a tree to denote the following algebraic
expression

3 1 4 + 7 + 2 7 +

S. Turaev, CSC 1700 Discrete Mathematics 16


Labeled Trees
Positional -tree:

-tree: every vertex has at most offspring

positional -tree: label the offspring of a given


vertex from left to right with numbers 1,2, ,

some of the offspring in the sequence may be


missing

S. Turaev, CSC 1700 Discrete Mathematics 17


Labeled Trees
Example: positional 3-tree:

2 3

1 2 1 3

3 3
1 2 3

1 2 3

S. Turaev, CSC 1700 Discrete Mathematics 18


Labeled Trees
Example: positional 2-tree:

L R

R L R

R L L R

S. Turaev, CSC 1700 Discrete Mathematics 19


Tree Searching
Visiting
Performing appropriate tasks at a vertex will be
called visiting the vertex.

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.

S. Turaev, CSC 1700 Discrete Mathematics 20


Tree Searching
Algorithm PREORDER

Step 1: Visit

Step 2: If exists, then apply this algorithm to


,

Step 3: If exists, then apply this algorithm to


,

S. Turaev, CSC 1700 Discrete Mathematics 21


Tree Searching
Example 1 A

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

S. Turaev, CSC 1700 Discrete Mathematics 22


Tree Searching
Example 2: + /

- +

/
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

Step 1: Search the left subtree , , if it exists

Step 2: Visit the root

Step 3: Search the right subtree , , if it exists

S. Turaev, CSC 1700 Discrete Mathematics 25


Tree Searching
Algorithm POSTORDER

Step 1: Search the left subtree , , if it exists

Step 2: Search the right subtree , , if it exists

Step 3: Visit the root

S. Turaev, CSC 1700 Discrete Mathematics 26


Tree Searching
Example: Traveling the tree using INORDER and
POSTORDER
+ /


INORDER: + /

- + POSTORDER: / +
/
a c
b

d e

S. Turaev, CSC 1700 Discrete Mathematics 27


Tree Searching
Infix notation: Algebraic symbols lie between their
arguments
+ /

( ) ( + (/))

or
( ( + /))

S. Turaev, CSC 1700 Discrete Mathematics 28


Tree Searching
Postfix or reverse Polish:
/ + ( = 2, = 1, = 3, = 4, = 2)

1. 2 1 3 4 2 / +

2. 1 3 4 2 / + replacing 2 1 with 1 since 2 1 = 1

3. 1 3 2 + replacing 4 2 / with 2 since 4/2 = 2

4. 1 5 replacing 3 2 + with 5 since 3 + 2 = 5

5. 5 replacing 1 5 with 5 since 1 5 =5


S. Turaev, CSC 1700 Discrete Mathematics 29
Exercises
Show the result of performing a preorder search of the
tree
x

y z

t
s
u

S. Turaev, CSC 1700 Discrete Mathematics 30


Exercises
Show the result of performing an inorder search of the
tree
x

y z

t
s
u

S. Turaev, CSC 1700 Discrete Mathematics 31


Exercises
Show the result of performing a postorder search of the
tree
x

y z

t
s
u

S. Turaev, CSC 1700 Discrete Mathematics 32


Exercises
Show the result of performing preorder, inorder and
postorder searches of the tree

, , , , , , , , , , , ,
=
, , , , , , ,

S. Turaev, CSC 1700 Discrete Mathematics 33


Exercises
Show the result of performing preorder, inorder and
postorder searches of the tree

= 1,2 , 1,3 , 2,4 , 3,5 , 4,6 , 5,7

S. Turaev, CSC 1700 Discrete Mathematics 34


Exercises
Evaluate the expressions, which are given in Polish, or
prefix, notation

+ 3 4 7 2 12 3 6 4

3 2 4 3 + 15 2 6 3

S. Turaev, CSC 1700 Discrete Mathematics 35


Exercises
Evaluate the expressions, which are given in reverse
Polish, or postfix, notation

43254253

374 9652+

S. Turaev, CSC 1700 Discrete Mathematics 36


Exercises
Draw a binary tree whose preorder search produces

JBACDIHEGF

CATSANDDOGS

Draw a binary tree whose postorder search produces

SEARCHING

TREEHOUSE

S. Turaev, CSC 1700 Discrete Mathematics 37

Das könnte Ihnen auch gefallen