Trees - Yeasir Rayhanyeasirrayhanprince.github.io/cse106/slides/Lecture9.pdfOrdered Rooted Tree the...

Post on 16-Jul-2020

0 views 0 download

Transcript of Trees - Yeasir Rayhanyeasirrayhanprince.github.io/cse106/slides/Lecture9.pdfOrdered Rooted Tree the...

Trees

Introduction to Trees

a connected undirected graph with no simple

circuits

Connected Graph

0

2

4

3

1

5

0

2

4

3

1

5

Connected Graph

0

2

4

3

1

5 Not a tree

Simple Circuits

Simple path that begins and ends at the

same vertex.

Simple path: a sequence of vertices

without

repetition

02

4

3

1

5

Simple Circuit

Not a treea

b

d

f

e

c

Simple Circuit

Not a treea

b

d

f

e

c

Rooted Tree

one vertex is designated as the root and every edge is directed away from the root.

Rooted Tree

0

2

4

3

1

5

4

21

35 0

Parent 4

21

35

0

6

Parent of 0 =

Parent4

21

35

0

6

Parent of 0 = 2

Siblings4

21

35

0

6

Siblings of 0 =

Siblings4

21

35

0

6

Siblings of 0 =

Siblings4

21

35

0

6

Siblings of 0 = 3

Ancestor of a vertex4

21

35

0

6

Ancestor of 0 = vertices in the path from root vertex 0 Excluding 0

Ancestor of a vertex4

21

35

0

6

Ancestor of 0 = vertices in the path from root vertex 0 Excluding 0

Ancestor of a vertex4

21

35

0

6

Ancestor of 0 = vertices in the path from root vertex 0 Excluding 0

4 , 2

Descendant of a vertex4

21

35

0

6

Descendants of 2 = vertices whose ancestors are 2

Descendant of a vertex4

21

35

0

6

Descendants of 2 = 0, 3, 6

Leaf Nodes4

21

35

0

6

Vertices that have no children

Internal Nodes4

21

35

0

6

Vertices that have children

Subtree rooted at a vertex

4

21

35

0

6

2

30

6

M-ary tree4

21

35

0

6

No of children of every internal

vertex <= m

M-ary tree4

21

35

0

6

No of children of every internal

vertex <= m

M-ary tree

Vertex 4 = 2 Vertex 1 = 1Vertex 2 = 2Vertex 0 = 1

A 1- ary treeA 2- ary treeA 3- ary tree

No

Yes

Yes

4

21

35

0

6

Full M-ary tree4

21

35

0

6

No of children of every internal

vertex = m

M-ary tree4

21

35

0

6

Vertex 4 = 2 Vertex 1 = 1Vertex 2 = 2Vertex 0 = 1

A Full 1- ary treeA Full 2- ary tree

No

No

Ordered Rooted Tree

the children of each internal

vertex are shown in ordered from

left to right.

4

21

35

0

6

First child of 2 = 0 (Left child)

Second child of 2 = 3 (Right child)

Level of a vertex

the length of the unique path from

the root to this vertex.

4

21

35

0

6

Level of a vertex

the length of the unique path from

the root to this vertex.

LEVEL OF ROOT = 0

4

21

35

0

6

Level of a vertex

the length of the unique path from

the root to this vertex.

LEVEL OF 0 =

4

21

35

0

6

Level of a vertex

the length of the unique path from

the root to this vertex.

LEVEL OF 0 =

4

21

35

0

6

Level of a vertex

the length of the unique path from

the root to this vertex.

LEVEL OF 0 = 2

4

21

35

0

6

Height of a tree

= MAX (Level of vertices)4

21

35

0

6

Height of a tree

= MAX (Level of vertices)4

21

35

0

6

Level of

Vertex 4 = 0

Vertex 1 = 1

Vertex 2 = 1

Vertex 5 = 2

Vertex 0 = 2

Vertex 3 = 2

Vertex 6 = 3

Height = Max(0, 1 , 1, 2, 2, 3) = 3

Balanced Tree

If height = h, all the leaves are at

Level h or h-1

4

21

35

0

6

Level of

Vertex 4 = 0

Vertex 1 = 1

Vertex 2 = 1

Vertex 5 = 2

Vertex 0 = 2

Vertex 3 = 2

Vertex 6 = 3

Height = 3