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

36
Trees

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

Page 1: Trees - Yeasir Rayhanyeasirrayhanprince.github.io/cse106/slides/Lecture9.pdfOrdered Rooted Tree the children of each internal vertex are shown in ordered from left to right. 4 2 1

Trees

Page 2: Trees - Yeasir Rayhanyeasirrayhanprince.github.io/cse106/slides/Lecture9.pdfOrdered Rooted Tree the children of each internal vertex are shown in ordered from left to right. 4 2 1

Introduction to Trees

a connected undirected graph with no simple

circuits

Page 3: Trees - Yeasir Rayhanyeasirrayhanprince.github.io/cse106/slides/Lecture9.pdfOrdered Rooted Tree the children of each internal vertex are shown in ordered from left to right. 4 2 1

Connected Graph

0

2

4

3

1

5

0

2

4

3

1

5

Page 4: Trees - Yeasir Rayhanyeasirrayhanprince.github.io/cse106/slides/Lecture9.pdfOrdered Rooted Tree the children of each internal vertex are shown in ordered from left to right. 4 2 1

Connected Graph

0

2

4

3

1

5 Not a tree

Page 5: Trees - Yeasir Rayhanyeasirrayhanprince.github.io/cse106/slides/Lecture9.pdfOrdered Rooted Tree the children of each internal vertex are shown in ordered from left to right. 4 2 1

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

Page 6: Trees - Yeasir Rayhanyeasirrayhanprince.github.io/cse106/slides/Lecture9.pdfOrdered Rooted Tree the children of each internal vertex are shown in ordered from left to right. 4 2 1

Simple Circuit

Not a treea

b

d

f

e

c

Page 7: Trees - Yeasir Rayhanyeasirrayhanprince.github.io/cse106/slides/Lecture9.pdfOrdered Rooted Tree the children of each internal vertex are shown in ordered from left to right. 4 2 1

Simple Circuit

Not a treea

b

d

f

e

c

Page 8: Trees - Yeasir Rayhanyeasirrayhanprince.github.io/cse106/slides/Lecture9.pdfOrdered Rooted Tree the children of each internal vertex are shown in ordered from left to right. 4 2 1

Rooted Tree

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

Page 9: Trees - Yeasir Rayhanyeasirrayhanprince.github.io/cse106/slides/Lecture9.pdfOrdered Rooted Tree the children of each internal vertex are shown in ordered from left to right. 4 2 1

Rooted Tree

0

2

4

3

1

5

4

21

35 0

Page 10: Trees - Yeasir Rayhanyeasirrayhanprince.github.io/cse106/slides/Lecture9.pdfOrdered Rooted Tree the children of each internal vertex are shown in ordered from left to right. 4 2 1

Parent 4

21

35

0

6

Parent of 0 =

Page 11: Trees - Yeasir Rayhanyeasirrayhanprince.github.io/cse106/slides/Lecture9.pdfOrdered Rooted Tree the children of each internal vertex are shown in ordered from left to right. 4 2 1

Parent4

21

35

0

6

Parent of 0 = 2

Page 12: Trees - Yeasir Rayhanyeasirrayhanprince.github.io/cse106/slides/Lecture9.pdfOrdered Rooted Tree the children of each internal vertex are shown in ordered from left to right. 4 2 1

Siblings4

21

35

0

6

Siblings of 0 =

Page 13: Trees - Yeasir Rayhanyeasirrayhanprince.github.io/cse106/slides/Lecture9.pdfOrdered Rooted Tree the children of each internal vertex are shown in ordered from left to right. 4 2 1

Siblings4

21

35

0

6

Siblings of 0 =

Page 14: Trees - Yeasir Rayhanyeasirrayhanprince.github.io/cse106/slides/Lecture9.pdfOrdered Rooted Tree the children of each internal vertex are shown in ordered from left to right. 4 2 1

Siblings4

21

35

0

6

Siblings of 0 = 3

Page 15: Trees - Yeasir Rayhanyeasirrayhanprince.github.io/cse106/slides/Lecture9.pdfOrdered Rooted Tree the children of each internal vertex are shown in ordered from left to right. 4 2 1

Ancestor of a vertex4

21

35

0

6

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

Page 16: Trees - Yeasir Rayhanyeasirrayhanprince.github.io/cse106/slides/Lecture9.pdfOrdered Rooted Tree the children of each internal vertex are shown in ordered from left to right. 4 2 1

Ancestor of a vertex4

21

35

0

6

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

Page 17: Trees - Yeasir Rayhanyeasirrayhanprince.github.io/cse106/slides/Lecture9.pdfOrdered Rooted Tree the children of each internal vertex are shown in ordered from left to right. 4 2 1

Ancestor of a vertex4

21

35

0

6

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

4 , 2

Page 18: Trees - Yeasir Rayhanyeasirrayhanprince.github.io/cse106/slides/Lecture9.pdfOrdered Rooted Tree the children of each internal vertex are shown in ordered from left to right. 4 2 1

Descendant of a vertex4

21

35

0

6

Descendants of 2 = vertices whose ancestors are 2

Page 19: Trees - Yeasir Rayhanyeasirrayhanprince.github.io/cse106/slides/Lecture9.pdfOrdered Rooted Tree the children of each internal vertex are shown in ordered from left to right. 4 2 1

Descendant of a vertex4

21

35

0

6

Descendants of 2 = 0, 3, 6

Page 20: Trees - Yeasir Rayhanyeasirrayhanprince.github.io/cse106/slides/Lecture9.pdfOrdered Rooted Tree the children of each internal vertex are shown in ordered from left to right. 4 2 1

Leaf Nodes4

21

35

0

6

Vertices that have no children

Page 21: Trees - Yeasir Rayhanyeasirrayhanprince.github.io/cse106/slides/Lecture9.pdfOrdered Rooted Tree the children of each internal vertex are shown in ordered from left to right. 4 2 1

Internal Nodes4

21

35

0

6

Vertices that have children

Page 22: Trees - Yeasir Rayhanyeasirrayhanprince.github.io/cse106/slides/Lecture9.pdfOrdered Rooted Tree the children of each internal vertex are shown in ordered from left to right. 4 2 1

Subtree rooted at a vertex

4

21

35

0

6

2

30

6

Page 23: Trees - Yeasir Rayhanyeasirrayhanprince.github.io/cse106/slides/Lecture9.pdfOrdered Rooted Tree the children of each internal vertex are shown in ordered from left to right. 4 2 1

M-ary tree4

21

35

0

6

No of children of every internal

vertex <= m

Page 24: Trees - Yeasir Rayhanyeasirrayhanprince.github.io/cse106/slides/Lecture9.pdfOrdered Rooted Tree the children of each internal vertex are shown in ordered from left to right. 4 2 1

M-ary tree4

21

35

0

6

No of children of every internal

vertex <= m

Page 25: Trees - Yeasir Rayhanyeasirrayhanprince.github.io/cse106/slides/Lecture9.pdfOrdered Rooted Tree the children of each internal vertex are shown in ordered from left to right. 4 2 1

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

Page 26: Trees - Yeasir Rayhanyeasirrayhanprince.github.io/cse106/slides/Lecture9.pdfOrdered Rooted Tree the children of each internal vertex are shown in ordered from left to right. 4 2 1

Full M-ary tree4

21

35

0

6

No of children of every internal

vertex = m

Page 27: Trees - Yeasir Rayhanyeasirrayhanprince.github.io/cse106/slides/Lecture9.pdfOrdered Rooted Tree the children of each internal vertex are shown in ordered from left to right. 4 2 1

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

Page 28: Trees - Yeasir Rayhanyeasirrayhanprince.github.io/cse106/slides/Lecture9.pdfOrdered Rooted Tree the children of each internal vertex are shown in ordered from left to right. 4 2 1

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)

Page 29: Trees - Yeasir Rayhanyeasirrayhanprince.github.io/cse106/slides/Lecture9.pdfOrdered Rooted Tree the children of each internal vertex are shown in ordered from left to right. 4 2 1

Level of a vertex

the length of the unique path from

the root to this vertex.

4

21

35

0

6

Page 30: Trees - Yeasir Rayhanyeasirrayhanprince.github.io/cse106/slides/Lecture9.pdfOrdered Rooted Tree the children of each internal vertex are shown in ordered from left to right. 4 2 1

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

Page 31: Trees - Yeasir Rayhanyeasirrayhanprince.github.io/cse106/slides/Lecture9.pdfOrdered Rooted Tree the children of each internal vertex are shown in ordered from left to right. 4 2 1

Level of a vertex

the length of the unique path from

the root to this vertex.

LEVEL OF 0 =

4

21

35

0

6

Page 32: Trees - Yeasir Rayhanyeasirrayhanprince.github.io/cse106/slides/Lecture9.pdfOrdered Rooted Tree the children of each internal vertex are shown in ordered from left to right. 4 2 1

Level of a vertex

the length of the unique path from

the root to this vertex.

LEVEL OF 0 =

4

21

35

0

6

Page 33: Trees - Yeasir Rayhanyeasirrayhanprince.github.io/cse106/slides/Lecture9.pdfOrdered Rooted Tree the children of each internal vertex are shown in ordered from left to right. 4 2 1

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

Page 34: Trees - Yeasir Rayhanyeasirrayhanprince.github.io/cse106/slides/Lecture9.pdfOrdered Rooted Tree the children of each internal vertex are shown in ordered from left to right. 4 2 1

Height of a tree

= MAX (Level of vertices)4

21

35

0

6

Page 35: Trees - Yeasir Rayhanyeasirrayhanprince.github.io/cse106/slides/Lecture9.pdfOrdered Rooted Tree the children of each internal vertex are shown in ordered from left to right. 4 2 1

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

Page 36: Trees - Yeasir Rayhanyeasirrayhanprince.github.io/cse106/slides/Lecture9.pdfOrdered Rooted Tree the children of each internal vertex are shown in ordered from left to right. 4 2 1

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