06.trees Bfs Dfs
06.trees Bfs Dfs
19 21 14
1 12 31 23 6
SoftUni Team
Technical Trainers
Software University
https://softuni.bg
Table of Contents
2
Trees and Related Terminology
Node, Edge, Root, Etc.
Tree Definition
▪ Tree is a widely used abstract data type (ADT)
7
that simulates a hierarchical tree structure:
▪ Value 19 21 14
▪ Parent – null or another tree reference
▪ Children – collection of trees 1 12 31 23 6
5
Tree Data Structure – Terminology
▪ Parent – an immediate ancestor
▪ The converse notion of a child Parent 17
Breadth: 3
9
Tree Data Structure – Terminology
▪ Forest – set of disjoint trees
▪ {17}, {9, 6, 5}, {14}, {15, 8} 17
6 5 8
Sub tree
10
Tree<int> Structure – Example
7 children
12 children
11
Tree<int> Structure – Example
▪ First, install the TreeNode<int> tree = new
SimpleTreeNode TreeNode<int>(7,
new TreeNode<int>(19,
NuGet package new TreeNode<int>(1),
new TreeNode<int>(12),
new TreeNode<int>(31)
),
new TreeNode<int>(21),
▪ Use the given code new TreeNode<int>(14,
to create a tree new TreeNode<int>(23),
new TreeNode<int>(6)
)
);
Console.WriteLine(tree);
12
Example: XML Tree in C#
XElement contacts =
new XElement("Contacts",
new XElement("Contact",
new XElement("Name", "Patrick Hines"),
new XElement("Phone", "206-555-0144"),
new XElement("Address",
new XElement("Street1", "123 Main St"),
new XElement("City", "Mercer Island"),
new XElement("State", "WA"),
new XElement("Postal", "68042")
)
)
);
Console.WriteLine(contacts);
13
Traversing Tree-Like Structures
DFS and BFS Traversals
Tree Traversal Algorithms
▪ Traversing a tree means to visit each of its nodes exactly once
▪ The order of visiting nodes may vary on the traversal algorithm
▪ Depth-First Search (DFS)
▪ Visit node's successors first
▪ Usually implemented by recursion
▪ Breadth-First Search (BFS)
▪ Nearest nodes visited first
▪ Implemented by a queue
15
Breadth-First Search (BFS)
▪ Breadth-First Search (BFS) first visits the neighbor nodes, then
the neighbors of neighbors, etc.
▪ BFS algorithm pseudo code: 1
7
BFS (node) {
queue node
while queue not empty 2 3 4
19 21 14
v queue
print v
for each node c of v 5 6 7 8 9
queue c 1 12 31 23 6
}
16
BFS in Action (Step 1)
▪ Queue: 7
Initially enqueue
▪ Output: the root node
7
19 21 14
1 12 31 23 6
17
BFS in Action (Step 2)
▪ Queue: 7
Remove from the
▪ Output: 7 queue the next
7 node and print it
19 21 14
1 12 31 23 6
18
BFS in Action (Step 3)
▪ Queue: 7, 19
Enqueue all
▪ Output: 7 children of the
7 current node
19 21 14
1 12 31 23 6
19
BFS in Action (Step 4)
▪ Queue: 7, 19, 21
Enqueue all
▪ Output: 7 children of the
7 current node
19 21 14
1 12 31 23 6
20
BFS in Action (Step 5)
▪ Queue: 7, 19, 21, 14
Enqueue all
▪ Output: 7 children of the
7 current node
19 21 14
1 12 31 23 6
21
BFS in Action (Step 6)
▪ Queue: 7, 19, 21, 14
Remove from the
▪ Output: 7, 19 queue the next
7 node and print it
19 21 14
1 12 31 23 6
22
BFS in Action (Step 7)
▪ Queue: 7, 19, 21, 14, 1
Enqueue all
▪ Output: 7, 19 children of the
7 current node
19 21 14
1 12 31 23 6
23
BFS in Action (Step 8)
▪ Queue: 7, 19, 21, 14, 1, 12
Enqueue all
▪ Output: 7, 19 children of the
7 current node
19 21 14
1 12 31 23 6
24
BFS in Action (Step 9)
▪ Queue: 7, 19, 21, 14, 1, 12, 31
Enqueue all
▪ Output: 7, 19 children of the
7 current node
19 21 14
1 12 31 23 6
25
BFS in Action (Step 10)
▪ Queue: 7, 19, 21, 14, 1, 12, 31
Remove from the
▪ Output: 7, 19, 21 queue the next
7 node and print it
19 21 14 No child nodes
to enqueue
1 12 31 23 6
26
BFS in Action (Step 11)
▪ Queue: 7, 19, 21, 14, 1, 12, 31
Remove from the
▪ Output: 7, 19, 21, 14 queue the next
7 node and print it
19 21 14
1 12 31 23 6
27
BFS in Action (Step 12)
▪ Queue: 7, 19, 21, 14, 1, 12, 31, 23
Enqueue all
▪ Output: 7, 19, 21, 14 children of the
7 current node
19 21 14
1 12 31 23 6
28
BFS in Action (Step 13)
▪ Queue: 7, 19, 21, 14, 1, 12, 31, 23, 6
Enqueue all
▪ Output: 7, 19, 21, 14 children of the
7 current node
19 21 14
1 12 31 23 6
29
BFS in Action (Step 14)
▪ Queue: 7, 19, 21, 14, 1, 12, 31, 23, 6
Remove from the
▪ Output: 7, 19, 21, 14, 1 queue the next
7 node and print it
19 21 14 No child nodes
to enqueue
1 12 31 23 6
30
BFS in Action (Step 15)
▪ Queue: 7, 19, 21, 14, 1, 12, 31, 23, 6
Remove from the
▪ Output: 7, 19, 21, 14, 1, queue the next
7 node and print it
12
19 21 14 No child nodes
to enqueue
1 12 31 23 6
31
BFS in Action (Step 16)
▪ Queue: 7, 19, 21, 14, 1, 12, 31, 23, 6
Remove from the
▪ Output: 7, 19, 21, 14, 1, queue the next
7 node and print it
12, 31
19 21 14 No child nodes
to enqueue
1 12 31 23 6
32
BFS in Action (Step 17)
▪ Queue: 7, 19, 21, 14, 1, 12, 31, 23, 6
Remove from the
▪ Output: 7, 19, 21, 14, 1, queue the next
7 node and print it
12, 31, 23
19 21 14 No child nodes
to enqueue
1 12 31 23 6
33
BFS in Action (Step 18)
▪ Queue: 7, 19, 21, 14, 1, 12, 31, 23, 6
Remove from the
▪ Output: 7, 19, 21, 14, 1, queue the next
7 node and print it
12, 31, 23, 6
19 21 14 No child nodes
to enqueue
1 12 31 23 6
34
BFS in Action (Step 19)
▪ Queue: 7, 19, 21, 14, 1, 12, 31, 23, 6
The queue is
▪ Output: 7, 19, 21, 14, 1, empty → stop
7
12, 31, 23, 6
19 21 14
1 12 31 23 6
35
BFS Example: Traverse Folders
DirectoryInfo class allows
static void TraverseDirBFS(string directoryPath) accessing
{ directories
var visitedDirsQueue = new Queue<DirectoryInfo>();
visitedDirsQueue.Enqueue(new DirectoryInfo(directoryPath));
while (visitedDirsQueue.Count > 0) { Store visited directories in a queue
DirectoryInfo currentDir = visitedDirsQueue.Dequeue();
Console.WriteLine(currentDir.FullName);
DirectoryInfo[] children = currentDir.GetDirectories();
foreach (DirectoryInfo child in children)
visitedDirsQueue.Enqueue(child);
} Use GetDirectories() method
} to get sub-directories
DFS (node) {
for each child c of node 4 5 8
DFS(c); 19 21 14
print node;
} 1 2 3 6 7
1 12 31 23 6
37
DFS in Action (Step 1)
▪ Stack: 7
Start DFS from
▪ Output: (empty) 7 the tree root
19 21 14
1 12 31 23 6
38
DFS in Action (Step 2)
▪ Stack: 7, 19
Enter recursively
▪ Output: (empty) 7 into the first child
19 21 14
1 12 31 23 6
39
DFS in Action (Step 3)
▪ Stack: 7, 19, 1
Enter recursively
▪ Output: (empty) 7 into the first child
19 21 14
1 12 31 23 6
40
DFS in Action (Step 4)
▪ Stack: 7, 19 Return back from
▪ Output: 1 7
recursion and print
the last visited node
19 21 14
1 12 31 23 6
41
DFS in Action (Step 5)
▪ Stack: 7, 19, 12
Enter recursively
▪ Output: 1 7 into the first child
19 21 14
1 12 31 23 6
42
DFS in Action (Step 6)
▪ Stack: 7, 19 Return back from
▪ Output: 1, 12 7
recursion and print
the last visited node
19 21 14
1 12 31 23 6
43
DFS in Action (Step 7)
▪ Stack: 7, 19, 31
Enter recursively
▪ Output: 1, 12 7 into the first child
19 21 14
1 12 31 23 6
44
DFS in Action (Step 8)
▪ Stack: 7, 19 Return back from
▪ Output: 1, 12, 31 7
recursion and print
the last visited node
19 21 14
1 12 31 23 6
45
DFS in Action (Step 9)
▪ Stack: 7 Return back from
▪ Output: 1, 12, 31, 19 7
recursion and print
the last visited node
19 21 14
1 12 31 23 6
46
DFS in Action (Step 10)
▪ Stack: 7, 21
Enter recursively
▪ Output: 1, 12, 31, 19 7 into the first child
19 21 14
1 12 31 23 6
47
DFS in Action (Step 11)
▪ Stack: 7 Return back from
▪ Output: 1, 12, 31, 19, 21 7
recursion and print
the last visited node
19 21 14
1 12 31 23 6
48
DFS in Action (Step 12)
▪ Stack: 7, 14
Enter recursively
▪ Output: 1, 12, 31, 19, 21 7 into the first child
19 21 14
1 12 31 23 6
49
DFS in Action (Step 13)
▪ Stack: 7, 14, 23
Enter recursively
▪ Output: 1, 12, 31, 19, 21 7 into the first child
19 21 14
1 12 31 23 6
50
DFS in Action (Step 14)
▪ Stack: 7, 14 Return back from
▪ Output: 1, 12, 31, 19, 21, 7
recursion and print
the last visited node
23
19 21 14
1 12 31 23 6
51
DFS in Action (Step 15)
▪ Stack: 7, 14, 6
Enter recursively
▪ Output: 1, 12, 31, 19, 21, 7 into the first child
23
19 21 14
1 12 31 23 6
52
DFS in Action (Step 16)
▪ Stack: 7, 14 Return back from
▪ Output: 1, 12, 31, 19, 21, 7
recursion and print
the last visited node
23, 6
19 21 14
1 12 31 23 6
53
DFS in Action (Step 17)
▪ Stack: 7 Return back from
▪ Output: 1, 12, 31, 19, 21, 7
recursion and print
the last visited node
23, 6, 14
19 21 14
1 12 31 23 6
54
DFS in Action (Step 18)
▪ Stack: (empty)
DFS traversal finished
▪ Output: 1, 12, 31, 19, 21, 7
23, 6, 14, 7
19 21 14
1 12 31 23 6
55
DFS Example: Traverse Folders
private static void TraverseDir(DirectoryInfo dir, string spaces)
{
Console.WriteLine(spaces + dir.FullName);
DirectoryInfo[] children = dir.GetDirectories();
foreach (DirectoryInfo child in children)
TraverseDir(child, spaces + " "); Use recursion to
} traverse folders
static void TraverseDir(string directoryPath)
{
TraverseDir(new DirectoryInfo(directoryPath), string.Empty);
}
static void Main()
{
TraverseDir(@"C:\Windows\assembly");
}
56
Summary
▪ …
▪ Trees are recursive data structures
▪ …
▪ A tree is a node holding a set of children (which are also
▪ …
nodes)
▪ Edges connect nodes
▪ DFS traversal → children first
▪ BFS traversal → root first
57
https://softuni.org
© SoftUni – https://softuni.org. Copyrighted document. Unauthorized copy, reproduction or use is not permitted.