Chapter 5 Trees: Outline: - Introduction - Binary Trees - Binary Tree Traversals - Graph - Bfs - Dfs
Chapter 5 Trees: Outline: - Introduction - Binary Trees - Binary Tree Traversals - Graph - Bfs - Dfs
• Introduction
• Representation Of Trees
• Binary Trees
• Binary Tree Traversals
• Graph
• BFS
• DFS
Introduction (1/8)
• A tree structure means that the data are organized so that
items of information are related by branches
• Examples:
Introduction (2/8)
• Definition (recursively): A tree is a finite set of one or
more nodes such that
• There is a specially designated node called root.
• The remaining nodes are partitioned into n>=0 disjoint set
T1,…,Tn, where each of these sets is a tree. T1,…,Tn are called
the subtrees of the root.
• Every node in the tree is the root of some subtree
Introduction (3/8)
• Some Terminology
• node: the item of information plus the branches to each node.
• degree: the number of subtrees of a node
• degree of a tree: the maximum of the degree of the nodes in
the tree.
• terminal nodes (or leaf): nodes that have degree zero
• nonterminal nodes: nodes that don’t belong to terminal nodes.
• children: the roots of the subtrees of a node X are the children
of X
• parent: X is the parent of its children.
Introduction (4/8)
• Some Terminology (cont’d)
• siblings: children of the same parent are said to be siblings.
• Ancestors of a node: all the nodes along the path from the
root to that node.
• The level of a node: defined by letting the root be at level
one. If a node is at level l, then it children are at level l+1.
• Height (or depth): the maximum level of any node in the
tree
Introduction (5/8)
• Example
A is the root node Property: (# edges) = (#nodes) - 1
B is the parent of D and E
C is the sibling of B
D and E are the children of B
D, E, F, G, I are external nodes, or leaves
A, B, C, H are internal nodes
The level of E is 3 Level
The height (depth) of the tree is 4
A 1
The degree of node B is 2
The degree of the c is 3
The ancestors of node I is A, C, H B C
The descendants of node C is F, G, H, I 2
H
3
D E F G
I
4
Introduction (6/8)
• Representation Of Trees
• List Representation
• we can write of Figure 5.2 as a list in which each of the subtrees is
also a list
( A ( B ( E ( K, L ), F ), C ( G ), D ( H ( M ), I, J ) ) )
• The root comes first,
followed by a list of sub-trees
Introduction (7/8)
• Representation Of
Trees (cont’d)
• Left Child-
Right Sibling
Representation
Introduction (8/8)
• Representation Of Trees (cont’d)
• Representation
As A Degree
Two Tree
Binary Trees (1/9)
• Binary trees are characterized by the fact that any
node can have at most two branches
• Definition (recursive):
• A binary tree is a finite set of nodes that is either empty or
consists of a root and two disjoint binary trees called the left
subtree and the right subtree
• Thus the left subtree and the right subtree are
distinguished
A A
B
• Any tree can be transformed into binary tree B
Algorithm Inorder(tree)
1. Traverse the left subtree, i.e., call Inorder(left-
subtree)
2. Visit the root.
3. Traverse the right subtree, i.e., call Inorder(right-
subtree)
Binary Tree Traversals (4/9)
• Preorder traversal (VLR) (recursive version)
Algorithm Preorder(tree)
1.Visit the root.
2. Traverse the left subtree, i.e., call Preorder(left-subtree)
3. Traverse the right subtree, i.e., call Preorder(right-subtree)
Binary Tree Traversals (5/9)
• Postorder traversal (LRV) (recursive version)
Algorithm Postorder(tree)
1.Traverse the left subtree, i.e., call Postorder(left-subtree)
2. Traverse the right subtree, i.e., call Postorder(right-subtree
3. Visit the root
TREE GRAPH
Tree is special form of graph i.e. minimally connected graph In graph there can be more than one path i.e.
Path and having only one path between any two vertices. graph can have uni-directional or bi-
directional paths (edges) between nodes
Tree is a special case of graph having no loops, no circuits Graph can have loops, circuits as well as can
Loops and no self-loops. have self-loops.
In tree there is exactly one root node and every child have In graph there is no such concept of root
Root Node only one parent. node.
Trees are less complex then graphs as having no cycles, no Graphs are more complex in compare to trees
Complexity self-loops and still connected. as it can have cycles, loops etc
Tree traversal is a kind of special case of traversal of graph. Graph is traversed by DFS: Depth First Search
Types of Traversal Tree is traversed in Pre-Order, In-Order and Post-Order (all and in BFS : Breadth First Search algorithm
three in DFS or in BFS algorithm)
In trees, there are many rules / restrictions for making In graphs no such rules/ restrictions are there
Connection Rules connections between nodes through edges. for connecting the nodes through edges.
Different types of trees are : Binary Tree , Binary Search There are mainly two types of Graphs :
Different Types Tree, AVL tree, Heaps. Directed and Undirected graphs.
CHAPTER 6 23
Examples for Graph
0 0 0
1 2 1 2
1
3
3 4 5 6
G1 2
G2
complete graph incomplete graph G3
V(G1)={0,1,2,3} E(G1)={(0,1),(0,2),(0,3),(1,2),(1,3),(2,3)}
V(G2)={0,1,2,3,4,5,6} E(G2)={(0,1),(0,2),(1,3),(1,4),(2,5),(2,6)}
V(G3)={0,1,2} E(G3)={<0,1>,<1,0>,<1,2>}
complete undirected graph: n(n-1)/2 edges
complete directed graph: n(n-1) edges
CHAPTER 6 24
Graph representation – undirected
Nodes visited: D
Overview
• Breadth-first search starts
F C with given node
A
• Then visits nodes adjacent
B D in some specified order
H (e.g., alphabetical)
0
G E • Like ripples in a pond
1
Nodes visited: D, C
Overview
• Breadth-first search starts
F C with given node
A
• Then visits nodes adjacent
B D in some specified order
H (e.g., alphabetical)
0
G E • Like ripples in a pond
1
Nodes visited: D, C, E
Overview
• Breadth-first search starts
F C with given node
A
• Then visits nodes adjacent
B D in some specified order
H (e.g., alphabetical)
0
G E • Like ripples in a pond
1
Nodes visited: D, C, E, F
Overview
• When all nodes in ripple
F C are visited, visit nodes in
A next ripples
B D
H
0
G E
2 1
Nodes visited: D, C, E, F, G
Overview
• When all nodes in ripple
F C are visited, visit nodes in
A next ripples
B D
H
0
3 G E
2 1
Nodes visited: D, C, E, F, G, H
Overview
4 • When all nodes in ripple
F C are visited, visit nodes in
A next ripples
B D
H
0
3
G E
2 1
Nodes visited: D, C, E, F, G, H, A
Overview
4 • When all nodes in ripple
F C are visited, visit nodes in
A next ripples
B D
H
0
3
G E
2 1
Nodes visited: D, C, E, F, G, H, A, B
Walk-Through
Enqueued Array
F C A
A B Q
B C
D
H D
E
G E F
G
H
G √ E
H D
The order nodes are visited:
Visit G
D, C, E, G
Walk-Through
Visited Array
F C A
A B
B C √
D
H D √
E √
G E F G
G √ E
H D
The order nodes are visited:
Nodes D and H are adjacent to
D, C, E, G G. D has already been
visited. Decide to visit H.
Walk-Through
Visited Array
F C A
A B
B C √
D
H D √
H
E √
G E F G
G √ E
H √ D
The order nodes are visited:
Visit H
D, C, E, G, H
Walk-Through
Visited Array
F C A
A B
B C √
D
H D √
H
E √
G E F G
G √ E
H √ D
The order nodes are visited:
Nodes A and B are adjacent to F.
D, C, E, G, H Decide to visit A next.
Walk-Through
Visited Array
F C A √
A B
B C √
D A
H D √
H
E √
G E F G
G √ E
H √ D
The order nodes are visited:
Visit A
D, C, E, G, H, A
Walk-Through
Visited Array
F C A √
A B
B C √
D A
H D √
H
E √
G E F G
G √ E
H √ D
The order nodes are visited:
Only Node B is adjacent to A.
D, C, E, G, H, A Decide to visit B next.
Walk-Through
Visited Array
F C A √
A B √ B
B C √
D A
H D √
H
E √
G E F G
G √ E
H √ D
The order nodes are visited:
Visit B
D, C, E, G, H, A, B
Walk-Through
Visited Array
F C A √
A B √
B C √
D A
H D √
H
E √
G E F G
G √ E
H √ D
The order nodes are visited:
No unvisited nodes adjacent to
D, C, E, G, H, A, B B. Backtrack (pop the stack).
Walk-Through
Visited Array
F C A √
A B √
B C √
D
H D √
H
E √
G E F G
G √ E
H √ D
The order nodes are visited:
No unvisited nodes adjacent to
D, C, E, G, H, A, B A. Backtrack (pop the stack).
Walk-Through
Visited Array
F C A √
A B √
B C √
D
H D √
E √
G E F G
G √ E
H √ D
The order nodes are visited:
No unvisited nodes adjacent to
D, C, E, G, H, A, B H. Backtrack (pop the
stack).
Walk-Through
Visited Array
F C A √
A B √
B C √
D
H D √
E √
G E F
G √ E
H √ D
The order nodes are visited:
No unvisited nodes adjacent to
D, C, E, G, H, A, B G. Backtrack (pop the
stack).
Walk-Through
Visited Array
F C A √
A B √
B C √
D
H D √
E √
G E F
G √
H √ D
The order nodes are visited:
No unvisited nodes adjacent to
D, C, E, G, H, A, B E. Backtrack (pop the stack).
Walk-Through
Visited Array
F C A √
A B √
B C √
D
H D √
E √
G E F
G √
H √ D
The order nodes are visited:
F is unvisited and is adjacent to
D, C, E, G, H, A, B D. Decide to visit F next.
Walk-Through
Visited Array
F C A √
A B √
B C √
D
H D √
E √
G E F √
G √ F
H √ D
The order nodes are visited:
Visit F
D, C, E, G, H, A, B, F
Walk-Through
Visited Array
F C A √
A B √
B C √
D
H D √
E √
G E F √
G √
H √ D
The order nodes are visited:
No unvisited nodes adjacent to
D, C, E, G, H, A, B, F F. Backtrack.
Walk-Through
Visited Array
F C A √
A B √
B C √
D
H D √
E √
G E F √
G √
H √
The order nodes are visited:
No unvisited nodes adjacent to
D, C, E, G, H, A, B, F D. Backtrack.
Walk-Through
Visited Array
F C A √
A B √
B C √
D
H D √
E √
G E F √
G √
H √
The order nodes are visited:
Stack is empty. Depth-first
D, C, E, G, H, A, B, F traversal is done.
DFS
Advantages:
•The advantage of depth-first Search is that memory
requirement is only linear with respect to the search
graph.
•Depth-first search finds solution without exploring much
in a path and the time and space it takes will be very less.
Disadvantages:
•Depth-First Search is not guaranteed to find the solution.
•And there is no guarantee to find a minimal solution, if
more than one solution exists.
BFS
Advantages:
•Breadth first search will never get trapped exploring the
useless path forever.
•If there is a solution, BFS will definitely find it out.
•If there is more than one solution then BFS can find the
minimal one that requires less number of steps.
Disadvantages:
•The main drawback of Breadth first search is its memory
requirement. Since each level of the tree must be saved in
order to generate the next level.
•If the solution is farther away from the root, breath first
search will consume lot of time.