Trees - Yeasir Rayhanyeasirrayhanprince.github.io/cse106/slides/Lecture9.pdfOrdered Rooted Tree the...
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](https://reader034.fdocuments.us/reader034/viewer/2022050118/5f4eeb9f71e25a792a76da8c/html5/thumbnails/1.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022050118/5f4eeb9f71e25a792a76da8c/html5/thumbnails/2.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022050118/5f4eeb9f71e25a792a76da8c/html5/thumbnails/3.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022050118/5f4eeb9f71e25a792a76da8c/html5/thumbnails/4.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022050118/5f4eeb9f71e25a792a76da8c/html5/thumbnails/5.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022050118/5f4eeb9f71e25a792a76da8c/html5/thumbnails/6.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022050118/5f4eeb9f71e25a792a76da8c/html5/thumbnails/7.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022050118/5f4eeb9f71e25a792a76da8c/html5/thumbnails/8.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022050118/5f4eeb9f71e25a792a76da8c/html5/thumbnails/9.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022050118/5f4eeb9f71e25a792a76da8c/html5/thumbnails/10.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022050118/5f4eeb9f71e25a792a76da8c/html5/thumbnails/11.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022050118/5f4eeb9f71e25a792a76da8c/html5/thumbnails/12.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022050118/5f4eeb9f71e25a792a76da8c/html5/thumbnails/13.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022050118/5f4eeb9f71e25a792a76da8c/html5/thumbnails/14.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022050118/5f4eeb9f71e25a792a76da8c/html5/thumbnails/15.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022050118/5f4eeb9f71e25a792a76da8c/html5/thumbnails/16.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022050118/5f4eeb9f71e25a792a76da8c/html5/thumbnails/17.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022050118/5f4eeb9f71e25a792a76da8c/html5/thumbnails/18.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022050118/5f4eeb9f71e25a792a76da8c/html5/thumbnails/19.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022050118/5f4eeb9f71e25a792a76da8c/html5/thumbnails/20.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022050118/5f4eeb9f71e25a792a76da8c/html5/thumbnails/21.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022050118/5f4eeb9f71e25a792a76da8c/html5/thumbnails/22.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022050118/5f4eeb9f71e25a792a76da8c/html5/thumbnails/23.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022050118/5f4eeb9f71e25a792a76da8c/html5/thumbnails/24.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022050118/5f4eeb9f71e25a792a76da8c/html5/thumbnails/25.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022050118/5f4eeb9f71e25a792a76da8c/html5/thumbnails/26.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022050118/5f4eeb9f71e25a792a76da8c/html5/thumbnails/27.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022050118/5f4eeb9f71e25a792a76da8c/html5/thumbnails/28.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022050118/5f4eeb9f71e25a792a76da8c/html5/thumbnails/29.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022050118/5f4eeb9f71e25a792a76da8c/html5/thumbnails/30.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022050118/5f4eeb9f71e25a792a76da8c/html5/thumbnails/31.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022050118/5f4eeb9f71e25a792a76da8c/html5/thumbnails/32.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022050118/5f4eeb9f71e25a792a76da8c/html5/thumbnails/33.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022050118/5f4eeb9f71e25a792a76da8c/html5/thumbnails/34.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022050118/5f4eeb9f71e25a792a76da8c/html5/thumbnails/35.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022050118/5f4eeb9f71e25a792a76da8c/html5/thumbnails/36.jpg)
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