Data Structures - 13. Introduction to Trees
-
Upload
emerson-ferreira -
Category
Education
-
view
148 -
download
1
description
Transcript of Data Structures - 13. Introduction to Trees
![Page 1: Data Structures - 13. Introduction to Trees](https://reader033.fdocuments.in/reader033/viewer/2022060118/55899f60d8b42aab2b8b470b/html5/thumbnails/1.jpg)
Universidade Federal da ParaíbaCentro de Informática
Introduction to TreesLecture 16
1107186 – Estrutura de Dados – Turma 02
Prof. Christian Azambuja PagotCI / UFPB
![Page 2: Data Structures - 13. Introduction to Trees](https://reader033.fdocuments.in/reader033/viewer/2022060118/55899f60d8b42aab2b8b470b/html5/thumbnails/2.jpg)
2Universidade Federal da ParaíbaCentro de Informática
What is a Tree?
● In graph theory, a tree is defined as a connected acyclic graph.
● Example:
3
5
6
4
1
2 However, one such graph may generate
different tree data structures!
Node
Edge
![Page 3: Data Structures - 13. Introduction to Trees](https://reader033.fdocuments.in/reader033/viewer/2022060118/55899f60d8b42aab2b8b470b/html5/thumbnails/3.jpg)
3Universidade Federal da ParaíbaCentro de Informática
What is a Tree?
● In graph theory, a tree is defined as a connected acyclic graph.
● Example:
3
5
6
4
1
2
4
1
2
3
4
5
Root node
![Page 4: Data Structures - 13. Introduction to Trees](https://reader033.fdocuments.in/reader033/viewer/2022060118/55899f60d8b42aab2b8b470b/html5/thumbnails/4.jpg)
4Universidade Federal da ParaíbaCentro de Informática
What is a Tree?
● In graph theory, a tree is defined as a connected acyclic graph.
● Example:
3
5
6
4
2
14
3
5 26
1
Root node
![Page 5: Data Structures - 13. Introduction to Trees](https://reader033.fdocuments.in/reader033/viewer/2022060118/55899f60d8b42aab2b8b470b/html5/thumbnails/5.jpg)
5Universidade Federal da ParaíbaCentro de Informática
Definitions
● Path Length– Number of edges on a path.
● Depth of a Node– Equal to the path length to the root.
● Height of a Node– Equal to the path length from the
node to its deepest descendant.
● Height of a Tree– Equal to the height of the root.
2
9
87
1
4
3
65
![Page 6: Data Structures - 13. Introduction to Trees](https://reader033.fdocuments.in/reader033/viewer/2022060118/55899f60d8b42aab2b8b470b/html5/thumbnails/6.jpg)
6Universidade Federal da ParaíbaCentro de Informática
Definitions
● Child Node– Each node can have n
child nodes.
● Parent Node– It is unique for each
node.
– The root does not have parent node.
2
9
87
1
4
3
65
![Page 7: Data Structures - 13. Introduction to Trees](https://reader033.fdocuments.in/reader033/viewer/2022060118/55899f60d8b42aab2b8b470b/html5/thumbnails/7.jpg)
7Universidade Federal da ParaíbaCentro de Informática
Definitions
● Leaf Node– Has no child node.
● Siblings– Nodes with the same parent.
● Ancestors– All nodes on the path to the root.
● Descendants of a node n– All nodes that share n as an
ancestor.
2
9
87
1
4
3
65
![Page 8: Data Structures - 13. Introduction to Trees](https://reader033.fdocuments.in/reader033/viewer/2022060118/55899f60d8b42aab2b8b470b/html5/thumbnails/8.jpg)
8Universidade Federal da ParaíbaCentro de Informática
Definitions
● Subtree– Tree formed by a node
and all its descendants.
2
9
87
1
4
3
65
![Page 9: Data Structures - 13. Introduction to Trees](https://reader033.fdocuments.in/reader033/viewer/2022060118/55899f60d8b42aab2b8b470b/html5/thumbnails/9.jpg)
9Universidade Federal da ParaíbaCentro de Informática
Definitions
● Tree Arity– n-ary.
– Binary. 2
9
87
4
3
65
1
n-ary tree
3
4
2
65
1
binary tree
![Page 10: Data Structures - 13. Introduction to Trees](https://reader033.fdocuments.in/reader033/viewer/2022060118/55899f60d8b42aab2b8b470b/html5/thumbnails/10.jpg)
10Universidade Federal da ParaíbaCentro de Informática
Binary Tree Implementation
● Node– Pointers to parent, left and right child.
parent
value
left right
Node
![Page 11: Data Structures - 13. Introduction to Trees](https://reader033.fdocuments.in/reader033/viewer/2022060118/55899f60d8b42aab2b8b470b/html5/thumbnails/11.jpg)
11Universidade Federal da ParaíbaCentro de Informática
Binary Tree Implementation
parent
value
null
left null right null
Node
root
parent
value
null
left null right null
Node
left child
![Page 12: Data Structures - 13. Introduction to Trees](https://reader033.fdocuments.in/reader033/viewer/2022060118/55899f60d8b42aab2b8b470b/html5/thumbnails/12.jpg)
12Universidade Federal da ParaíbaCentro de Informática
Binary Tree Implementation
parent
value
null
left right null
Node
root
parent
value
left null right null
Node
How about a n-ary tree?
![Page 13: Data Structures - 13. Introduction to Trees](https://reader033.fdocuments.in/reader033/viewer/2022060118/55899f60d8b42aab2b8b470b/html5/thumbnails/13.jpg)
13Universidade Federal da ParaíbaCentro de Informática
n-ary Tree Implementation
● Node– Pointers to parent, left child and right sibling.
parent
value
left rsibling
Node
According to “Introduction to Algorithms”, Cormen, Leiserson, Rivest, Stein.
![Page 14: Data Structures - 13. Introduction to Trees](https://reader033.fdocuments.in/reader033/viewer/2022060118/55899f60d8b42aab2b8b470b/html5/thumbnails/14.jpg)
14Universidade Federal da ParaíbaCentro de Informática
n-ary Tree Implementation
● Example
Node
root
p
left rsib
n
n n
![Page 15: Data Structures - 13. Introduction to Trees](https://reader033.fdocuments.in/reader033/viewer/2022060118/55899f60d8b42aab2b8b470b/html5/thumbnails/15.jpg)
15Universidade Federal da ParaíbaCentro de Informática
n-ary Tree Implementation
● Example
Node
root
p
left rsib
n
n
p
left rsibn n
![Page 16: Data Structures - 13. Introduction to Trees](https://reader033.fdocuments.in/reader033/viewer/2022060118/55899f60d8b42aab2b8b470b/html5/thumbnails/16.jpg)
16Universidade Federal da ParaíbaCentro de Informática
n-ary Tree Implementation
● Example
Node
root
p
left rsib
n
n
p
left rsibn
p
left rsibn n
![Page 17: Data Structures - 13. Introduction to Trees](https://reader033.fdocuments.in/reader033/viewer/2022060118/55899f60d8b42aab2b8b470b/html5/thumbnails/17.jpg)
17Universidade Federal da ParaíbaCentro de Informática
n-ary Tree Implementation
● Example
Node
root
p
left rsib
n
n
p
left rsibn
p
left rsibn
p
left rsibn n
![Page 18: Data Structures - 13. Introduction to Trees](https://reader033.fdocuments.in/reader033/viewer/2022060118/55899f60d8b42aab2b8b470b/html5/thumbnails/18.jpg)
18Universidade Federal da ParaíbaCentro de Informática
n-ary Tree Implementation
● Example
Node
root
p
left rsib
n
n
p
left rsibn
p
left rsibn
p
left rsibn
p
left rsibn n
![Page 19: Data Structures - 13. Introduction to Trees](https://reader033.fdocuments.in/reader033/viewer/2022060118/55899f60d8b42aab2b8b470b/html5/thumbnails/19.jpg)
19Universidade Federal da ParaíbaCentro de Informática
n-ary Tree Implementation
● Example
Node
root
p
left rsib
n
n
p
left rsibn
p
left rsibn
p
left rsib
p
left rsibn n
p
left rsibn n
![Page 20: Data Structures - 13. Introduction to Trees](https://reader033.fdocuments.in/reader033/viewer/2022060118/55899f60d8b42aab2b8b470b/html5/thumbnails/20.jpg)
20Universidade Federal da ParaíbaCentro de Informática
n-ary Tree Implementation
● Example
Node
root
p
left rsib
n
n
p
left rsibn
p
left rsibn
p
left rsib
p
left rsibn n
p
left rsibn
p
left rsibn n
![Page 21: Data Structures - 13. Introduction to Trees](https://reader033.fdocuments.in/reader033/viewer/2022060118/55899f60d8b42aab2b8b470b/html5/thumbnails/21.jpg)
21Universidade Federal da ParaíbaCentro de Informática
Tree Traversal
● There are two types of tree traversal algorithms:– Breadth-first.
– Depth-first.
![Page 22: Data Structures - 13. Introduction to Trees](https://reader033.fdocuments.in/reader033/viewer/2022060118/55899f60d8b42aab2b8b470b/html5/thumbnails/22.jpg)
22Universidade Federal da ParaíbaCentro de Informática
Tree Traversal: Breadth-first-search (BFS)
● Visits only once each node of a graph on a level-by-level basis.
3
4
2
65
1
![Page 23: Data Structures - 13. Introduction to Trees](https://reader033.fdocuments.in/reader033/viewer/2022060118/55899f60d8b42aab2b8b470b/html5/thumbnails/23.jpg)
23Universidade Federal da ParaíbaCentro de Informática
Tree Traversal: Breadth-first-search (BFS)
● Visits only once each node of a graph on a level-by-level basis.
3
4
2
65
1
![Page 24: Data Structures - 13. Introduction to Trees](https://reader033.fdocuments.in/reader033/viewer/2022060118/55899f60d8b42aab2b8b470b/html5/thumbnails/24.jpg)
24Universidade Federal da ParaíbaCentro de Informática
Tree Traversal: Breadth-first-search (BFS)
● Visits only once each node of a graph on a level-by-level basis.
3
4
2
65
1
![Page 25: Data Structures - 13. Introduction to Trees](https://reader033.fdocuments.in/reader033/viewer/2022060118/55899f60d8b42aab2b8b470b/html5/thumbnails/25.jpg)
25Universidade Federal da ParaíbaCentro de Informática
Tree Traversal: Breadth-first-search (BFS)
● Visits only once each node of a graph on a level-by-level basis.
3
4
2
65
1
![Page 26: Data Structures - 13. Introduction to Trees](https://reader033.fdocuments.in/reader033/viewer/2022060118/55899f60d8b42aab2b8b470b/html5/thumbnails/26.jpg)
26Universidade Federal da ParaíbaCentro de Informática
Tree Traversal: Breadth-first-search (BFS)
● Visits only once each node of a graph on a level-by-level basis.
3
4
2
65
1
![Page 27: Data Structures - 13. Introduction to Trees](https://reader033.fdocuments.in/reader033/viewer/2022060118/55899f60d8b42aab2b8b470b/html5/thumbnails/27.jpg)
27Universidade Federal da ParaíbaCentro de Informática
Tree Traversal: Breadth-first-search (BFS)
● Implementationstruct Node{
struct Node* parent;int value;struct Node* left;struct Node* right;
};
struct Queue{
int first;int last;struct Node* elements[10];
};
struct Queue q;... int main(...) ...
C code excerpt:
![Page 28: Data Structures - 13. Introduction to Trees](https://reader033.fdocuments.in/reader033/viewer/2022060118/55899f60d8b42aab2b8b470b/html5/thumbnails/28.jpg)
28Universidade Federal da ParaíbaCentro de Informática
Tree Traversal: Breadth-first-search (BFS)
● Implementationvoid Enqueue(struct Queue* q, struct Node* n){
q>elements[++q>last] = n;}
struct Node* Dequeue(struct Queue* q){
return q>elements[q>first++];}
int QueueNotEmpty(struct Queue* q){
return q>first <= q>last;}
... int main(...) ...
C code excerpt:
![Page 29: Data Structures - 13. Introduction to Trees](https://reader033.fdocuments.in/reader033/viewer/2022060118/55899f60d8b42aab2b8b470b/html5/thumbnails/29.jpg)
29Universidade Federal da ParaíbaCentro de Informática
Tree Traversal: Breadth-first-search (BFS)
● Implementationvoid BFS(struct Node* n){
if (n != NULL){
Enqueue(&q,n);
while(QueueNotEmpty(&q)){
struct Node* v = Dequeue(&q);
printf("node value: %i\n",v>value);
if (v>left != NULL)Enqueue(&q,v>left);
if (v>right != NULL)Enqueue(&q,v>right);
}}
}... int main(...) ...
C code excerpt:
![Page 30: Data Structures - 13. Introduction to Trees](https://reader033.fdocuments.in/reader033/viewer/2022060118/55899f60d8b42aab2b8b470b/html5/thumbnails/30.jpg)
30Universidade Federal da ParaíbaCentro de Informática
Tree Traversal: Depth-First-Search (DFS)
● Explores as far as possible each branch of the tree before backtracking.– Pre-order:
– In-order:
– Post-order:3
4
2
65
11, 2, 4, 5, 6, 3
5, 4, 6, 2, 1, 3
5, 6, 4, 2, 3, 1
![Page 31: Data Structures - 13. Introduction to Trees](https://reader033.fdocuments.in/reader033/viewer/2022060118/55899f60d8b42aab2b8b470b/html5/thumbnails/31.jpg)
31Universidade Federal da ParaíbaCentro de Informática
Tree Traversal: Pre-Order
● Implementation
void PreOrder(struct Node* n){
if (n != NULL){
printf("node value: %i\n", n>value);PreOrder(n>left);PreOrder(n>right);
}}
C code excerpt:
![Page 32: Data Structures - 13. Introduction to Trees](https://reader033.fdocuments.in/reader033/viewer/2022060118/55899f60d8b42aab2b8b470b/html5/thumbnails/32.jpg)
32Universidade Federal da ParaíbaCentro de Informática
Tree Traversal: In-Order
● Implementation
void InOrder(struct Node* n){
if (n != NULL){
InOrder(n>left);printf("node value: %i\n", n>value);InOrder(n>right);
}}
C code excerpt:
![Page 33: Data Structures - 13. Introduction to Trees](https://reader033.fdocuments.in/reader033/viewer/2022060118/55899f60d8b42aab2b8b470b/html5/thumbnails/33.jpg)
33Universidade Federal da ParaíbaCentro de Informática
Tree Traversal: Post-Order
● Implementation
void PostOrder(struct Node* n){
if (n != NULL){
PostOrder(n>left);PostOrder(n>right);printf("node value: %i\n", n>value);
}}
C code excerpt: