Trees (slides)

37
Trees

Transcript of Trees (slides)

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 .

Trees

Β© S. Turaev, CSC 1700 Discrete Mathematics 2

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.

Trees

Β© S. Turaev, CSC 1700 Discrete Mathematics 3

Levels, Parent-Offspring, Siblings

Β© S. Turaev, CSC 1700 Discrete Mathematics

2

1

5

3 4

6

7

Level 1 vertices: the vertices of the edges beginning at 𝑣𝑣0 (level 0)

Level π‘˜π‘˜ vertices: the vertices of the edges beginning at those level π‘˜π‘˜ βˆ’ 1vertices

Parent-offspring: for all pairs (π‘Žπ‘Ž, 𝑏𝑏) in 𝑇𝑇, π‘Žπ‘Ž is called the parent of 𝑏𝑏 and 𝑏𝑏 is called the offspring of π‘Žπ‘Ž

Siblings: the vertices that have the same parent

Height of a tree: the largest level number of a tree

Leaves: the vertices that have no offspring

4

Levels, Parent-Offspring, Siblings

Β© S. Turaev, CSC 1700 Discrete Mathematics

2

1

5

3 4

6

7

Level 0

Level 1

Level 2

Level 3

Height 3Leaf

Root

Child /Offspring

Parent

Siblings

5

Theorem: Let 𝑇𝑇, 𝑣𝑣0 be a rooted tree. Then

𝑇𝑇 is irreflexive

𝑇𝑇 is asymmetric

If π‘Žπ‘Ž, 𝑏𝑏 in 𝑇𝑇 and 𝑏𝑏, 𝑐𝑐 in 𝑇𝑇, then π‘Žπ‘Ž, 𝑐𝑐 is not in 𝑇𝑇, for all π‘Žπ‘Ž, 𝑏𝑏 and 𝑐𝑐 in 𝐴𝐴.

Trees

Β© S. Turaev, CSC 1700 Discrete Mathematics 6

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.

Trees

Β© S. Turaev, CSC 1700 Discrete Mathematics 7

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.

𝒏𝒏-trees

Β© S. Turaev, CSC 1700 Discrete Mathematics 8

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.

Subtrees

Β© S. Turaev, CSC 1700 Discrete Mathematics 9

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 𝑣𝑣.

Subtrees

Β© S. Turaev, CSC 1700 Discrete Mathematics

2 31

0

4 5

6

10

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 𝑣𝑣.

Subtrees

Β© S. Turaev, CSC 1700 Discrete Mathematics

2

0

4 5

6

11

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

Exercises

Β© S. Turaev, CSC 1700 Discrete Mathematics 12

Exercise 2: Consider the rooted tree 𝑇𝑇, 0 .

Exercises

Β© S. Turaev, CSC 1700 Discrete Mathematics

0

1 2

4 5 6 8 9

10

11 12

7

3

13 14

15

13

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 𝑇𝑇 26. Compute 𝑇𝑇 37. What is the height of 𝑇𝑇, 0 ?

Exercises

Β© S. Turaev, CSC 1700 Discrete Mathematics 14

Example: Use a tree to denote the following algebraic expression

3 βˆ’ 2 Γ— 𝑏𝑏 + 𝑏𝑏 βˆ’ 2 βˆ’ 3 + 𝑏𝑏

Labeled Trees

Β© S. Turaev, CSC 1700 Discrete Mathematics

+

- -

3

2 b

x -

b 2 3 b

+

15

Example: Use a tree to denote the following algebraic expression

3 Γ— 1 βˆ’ π‘Žπ‘Ž Γ· 4 + 7 βˆ’ 𝑏𝑏 + 2 Γ— 7 + π‘Žπ‘Ž Γ· 𝑏𝑏

Labeled Trees

Β© S. Turaev, CSC 1700 Discrete Mathematics 16

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

Labeled Trees

Β© S. Turaev, CSC 1700 Discrete Mathematics 17

Example: positional 3-tree:

Labeled Trees

Β© S. Turaev, CSC 1700 Discrete Mathematics

2

2

3

3

3

1 1

3

2 31

2 31

18

Example: positional 2-tree:

Labeled Trees

Β© S. Turaev, CSC 1700 Discrete Mathematics

L R

L

R

R L

L L R

R

19

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.

Tree Searching

Β© S. Turaev, CSC 1700 Discrete Mathematics 20

Algorithm PREORDER

Step 1: Visit 𝑣𝑣

Step 2: If 𝑣𝑣𝐿𝐿 exists, then apply this algorithm to 𝑇𝑇 𝑣𝑣𝐿𝐿 , 𝑣𝑣𝐿𝐿

Step 3: If 𝑣𝑣𝑅𝑅 exists, then apply this algorithm to 𝑇𝑇 𝑣𝑣𝑅𝑅 , 𝑣𝑣𝑅𝑅

Tree Searching

Β© S. Turaev, CSC 1700 Discrete Mathematics 21

Example 1

Tree Searching

Β© S. Turaev, CSC 1700 Discrete Mathematics

A

B

C

D F

E

G J

I

L

K

H

1

2

3

4

5 6

7

8

9

10

11

A B C D E F G H I J K L

22

Example 2: π‘Žπ‘Ž βˆ’ 𝑏𝑏 Γ— 𝑐𝑐 + 𝑑𝑑/𝑒𝑒

Tree Searching

Β© S. Turaev, CSC 1700 Discrete Mathematics

Γ—

-

a b

e

/

+

c

d

Γ— - a b + c / d e

1

2 3

4

5

7 86

23

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 since2 Γ— 6 = 12

Tree Searching

Β© S. Turaev, CSC 1700 Discrete Mathematics 24

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

Tree Searching

Β© S. Turaev, CSC 1700 Discrete Mathematics 25

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 𝑣𝑣

Tree Searching

Β© S. Turaev, CSC 1700 Discrete Mathematics 26

Example: Traveling the tree using INORDER and POSTORDER

π‘Žπ‘Ž βˆ’ 𝑏𝑏 Γ— 𝑐𝑐 + 𝑑𝑑/𝑒𝑒

Tree Searching

Β© S. Turaev, CSC 1700 Discrete Mathematics

Γ—

-

a b

e

/

+

c

d

INORDER: π‘Žπ‘Ž βˆ’ 𝑏𝑏 Γ— 𝑐𝑐 + 𝑑𝑑/𝑒𝑒

POSTORDER: π‘Žπ‘Ž 𝑏𝑏 βˆ’ 𝑐𝑐 𝑑𝑑 𝑒𝑒 / +Γ—

27

Infix notation: Algebraic symbols lie between their arguments

π‘Žπ‘Ž βˆ’ 𝑏𝑏 Γ— 𝑐𝑐 + 𝑑𝑑 / 𝑒𝑒

(π‘Žπ‘Ž βˆ’ 𝑏𝑏) Γ— (𝑐𝑐 + (𝑑𝑑/𝑒𝑒))

or

π‘Žπ‘Ž βˆ’ (𝑏𝑏 Γ— ( 𝑐𝑐 + 𝑑𝑑 /𝑒𝑒))

Tree Searching

Β© S. Turaev, CSC 1700 Discrete Mathematics 28

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

Tree Searching

Β© S. Turaev, CSC 1700 Discrete Mathematics 29

Show the result of performing a preorder search of the tree

Exercises

Β© S. Turaev, CSC 1700 Discrete Mathematics

x

y

s

z

t

u

v

30

Show the result of performing an inorder search of the tree

Exercises

Β© S. Turaev, CSC 1700 Discrete Mathematics

x

y

s

z

t

u

v

31

Show the result of performing a postorder search of the tree

Exercises

Β© S. Turaev, CSC 1700 Discrete Mathematics

x

y

s

z

t

u

v

32

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

𝑇𝑇 = π‘Žπ‘Ž, 𝑏𝑏 , π‘Žπ‘Ž,𝑑𝑑 , 𝑏𝑏, 𝑐𝑐 , 𝑏𝑏, 𝑖𝑖 , 𝑑𝑑, π‘˜π‘˜ , 𝑑𝑑, 𝑒𝑒 ,𝑐𝑐,𝑔𝑔 , 𝑐𝑐, β„Ž , 𝑒𝑒, 𝑗𝑗 , 𝑒𝑒, 𝑓𝑓

Exercises

Β© S. Turaev, CSC 1700 Discrete Mathematics 33

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

Exercises

Β© S. Turaev, CSC 1700 Discrete Mathematics 34

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

Exercises

Β© S. Turaev, CSC 1700 Discrete Mathematics 35

Evaluate the expressions, which are given in reversePolish, or postfix, notation

β€’ 4 3 2 Γ· βˆ’ 5 Γ— 4 2 Γ— 5 Γ— 3 Γ· Γ·

β€’ 3 7 Γ— 4 βˆ’ 9 Γ— 6 5 Γ— 2 + Γ·

Exercises

Β© S. Turaev, CSC 1700 Discrete Mathematics 36

Draw a binary tree whose preorder search produces

β€’ JBACDIHEGF

β€’ CATSANDDOGS

Draw a binary tree whose postorder search produces

β€’ SEARCHING

β€’ TREEHOUSE

Exercises

Β© S. Turaev, CSC 1700 Discrete Mathematics 37