0% found this document useful (0 votes)
9 views58 pages

06.trees Bfs Dfs

Uploaded by

alenko2007
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views58 pages

06.trees Bfs Dfs

Uploaded by

alenko2007
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 58

Trees Representation and Traversal (BFS, DFS)

Trees Related Terminology and Traversal Algorithms


7

19 21 14

1 12 31 23 6

SoftUni Team
Technical Trainers
Software University
https://softuni.bg
Table of Contents

1.Trees and Related Terminology


▪ Node, Edge, Root, etc.
2.Traversing Tree-Like Structures
▪ BFS traversal
▪ DFS traversal

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

▪ Recursive definition – a tree consists of a value and set of


child nodes, which are trees
▪ By working with trees you can actually work with:
▪ Hierarchical structures, markup languages, DFS and BFS
algorithms
4
Tree Data Structure – Terminology
▪ Node – a structure which may contain a
value or condition or represent a
Root
separate data structure
Edge 17
▪ Edge – the connection between
one node and another Node
9 14 15
▪ Root – the top node in a tree,
the prime ancestor 6 5 8

5
Tree Data Structure – Terminology
▪ Parent – an immediate ancestor
▪ The converse notion of a child Parent 17

▪ Child – an immediate descendant


9 14 15
▪ Node directly connected to Child
another node when moving away
6 5 8
from the root
▪ Siblings – a group of nodes with
the same parent Sibling Sibling
6
Tree Data Structure – Terminology
▪ Ancestor – node reachable by
Ancestor
repeated proceeding from child
to parent Descendant 17

▪ Descendant – node reachable by Branch 9 14 15


repeated proceeding from
parent to child 6 5 8
▪ Leaf – node with no children
▪ Branch – node with at least one Leaf
child
7
Tree Data Structure – Terminology
▪ Degree – number of children for node
zero for a leaf
Degree: 2 17
▪ Path – sequence of nodes and edges
connecting a node with Path of 9 14 15
a descendant distance: 2
▪ Distance – number of edges along the 6 5 8

shortest path between two nodes


▪ Depth – distance between a node and
the root
8
Tree Data Structure – Terminology
▪ Level – depth + 1 Height: 3
▪ Height – the maximum level in the tree 17
▪ The number of edges on the longest path
between a node and a descendant leaf 9 14 15
▪ Width – number of nodes in a level
▪ Breadth – number of leaves 6 5 8

Breadth: 3

9
Tree Data Structure – Terminology
▪ Forest – set of disjoint trees
▪ {17}, {9, 6, 5}, {14}, {15, 8} 17

▪ Sub tree – tree T is a tree consisting of


a node in T and all its descendants in T 9 14 15

6 5 8

Sub tree

10
Tree<int> Structure – Example

7 children

19 children 21 children 14 children

1 children 31 children 23 children 6 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

static void Main() {


TraverseDirBFS(@"C:\Windows\assembly");
}
36
Depth-First Search (DFS)
▪ Depth-First Search (DFS) first visits all descendants of given
node recursively, finally visits the node itself
9
▪ DFS algorithm pseudo code: 7

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.

You might also like