0% found this document useful (0 votes)
62 views76 pages

Chapter 5 Trees: Outline: - Introduction - Binary Trees - Binary Tree Traversals - Graph - Bfs - Dfs

This document outlines a chapter on trees and graphs. It begins by introducing representations of trees using lists, left-child right-sibling notation, and as degree-two trees. Binary trees are then discussed, including their properties and representations using arrays and links. The document covers the three tree traversal algorithms - inorder, preorder, and postorder. Finally, it compares the key differences between trees and graphs and provides examples of their applications.

Uploaded by

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

Chapter 5 Trees: Outline: - Introduction - Binary Trees - Binary Tree Traversals - Graph - Bfs - Dfs

This document outlines a chapter on trees and graphs. It begins by introducing representations of trees using lists, left-child right-sibling notation, and as degree-two trees. Binary trees are then discussed, including their properties and representations using arrays and links. The document covers the three tree traversal algorithms - inorder, preorder, and postorder. Finally, it compares the key differences between trees and graphs and provides examples of their applications.

Uploaded by

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

Chapter 5 Trees: Outline

• 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

• by left child-right sibling representation


Binary Trees (2/9)
• The abstract data type of binary tree
Binary Trees (4/9)
• Properties of binary trees
• Lemma 5.1 [Maximum number of nodes]:
1. The maximum number of nodes on level i of a binary tree
is 2i-1, i 1.
2. The maximum number of nodes in a binary tree of depth
k is 2k-1, k1.
• Lemma 5.2 [Relation between number of leaf nodes and
degree-2 nodes]:
For any nonempty binary tree, T, if n0 is the number of
leaf nodes and n2 is the number of nodes of degree 2,
then n0 = n2 + 1.
• These lemmas allow us to define full and complete
binary trees
Binary Trees (5/9)
• Definition:
• A full binary tree of depth k is a binary tree of depth k having
2k-1 nodes, k  0.
• A binary tree with n nodes and depth k is complete if its nodes
correspond to the nodes numbered from 1 to n in the full
binary tree of depth k.
• From Lemma 5.1, the
height of a complete
binary tree with n nodes
is log2(n+1)
Binary Trees (6/9)
• Binary tree representations (using array)
• Lemma 5.3: If a complete binary tree with n nodes is
represented sequentially, then for any node with index i,
1  i  n, we have
1. parent(i) is at i /2 if i  1.
If i = 1, i is at the root and has no parent.
2. LeftChild(i) is at 2i if 2i  n.
If 2i  n, then i has no left child.
3. RightChild(i) is at 2i+1 if 2i+1  n.
If 2i +1  n, then i has no right child A 1

[1] [2] [3] [4] [5] [6] [7] B 2 C 3


A B C — D — E
Level 1 D E
Level 2 Level 3 4 5 6 7
Binary Trees (9/9)
• Binary tree representations (using link)
Binary Tree Traversals (1/9)
• How to traverse a tree or visit each node in the tree
exactly once?
• There are six possible combinations of traversal
LVR, LRV, VLR, VRL, RVL, RLV
• Adopt convention that we traverse left before
right, only 3 traversals remain
LVR (inorder), LRV (postorder), VLR (preorder)

left_child data right_child


V
L: moving left : R: moving right
visiting
node
Binary Tree Traversals (2/9)
• Arithmetic Expression using binary tree
• inorder traversal (infix expression, LVR)
A/B*C*D+E
• preorder traversal (prefix expression, VLR)
+**/ABCDE
• postorder traversal
(postfix expression,LRV)
AB/C*D*E+
Binary Trees (8/9)
• Binary tree representations (using link)
struct node
{
int info;
node *left;
node *right;
}*root;
Binary Tree Traversals (3/9)
• Inorder traversal (LVR) (recursive version)

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.

Trees come in the category of DAG : Directed Acyclic


DAG Graphs is a kind of directed graph that have no cycles.
Graph can be Cyclic or Acyclic.

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.

Graph applications : Coloring of maps, in OR


Tree applications : sorting and searching like Tree Traversal
Applications & Binary Search. (PERT & CPM), algorithms, Graph coloring,
Definition
 A graph G consists of two sets
– a finite, nonempty set of vertices V(G)
– a finite, possible empty set of edges E(G)
– G(V,E) represents a graph
 An undirected graph is one in which the pair of
vertices in a edge is unordered, (v 0, v1) = (v1,v0)
 A directed graph is one in which each edge is a
directed pair of vertices, <v 0, v1> != <v1,v0>
tail head

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

graph Adjacency list Adjacency matrix

ref. Introduction to Algorithms by


Thomas Cormen
Graph representation – directed

graph Adjacency list Adjacency matrix

ref. Introduction to Algorithms by


Thomas Cormen
Overview
• Breadth-first search starts
F C with given node
A
B D
H
0
G E

Task: Conduct a breadth-first search of


the graph starting with node 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
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

How is this accomplished? Simply replace the stack


with a queue! Rules: (1) Maintain an enqueued
array. (2) Visit node when dequeued.
Walk-Through
Enqueued Array
F C A
A B QD
B C
D
H D √
E
G E F
G
Nodes visited: H

Enqueue D. Notice, D not yet visited.


Walk-Through
Enqueued Array
F C A
A B QCEF
B C √
D
H D √
E √
G E F √
G
Nodes visited: D H

Dequeue D. Visit D. Enqueue unenqueued nodes


adjacent to D.
Walk-Through
Enqueued Array
F C A
A B QEF
B C √
D
H D √
E √
G E F √
G
Nodes visited: D, C H

Dequeue C. Visit C. Enqueue unenqueued nodes


adjacent to C.
Walk-Through
Enqueued Array
F C A
A B Q F G
B C √
D
H D √
E √
G E F √
G
Nodes visited: D, C, E H

Dequeue E. Visit E. Enqueue unenqueued nodes


adjacent to E.
Walk-Through
Enqueued Array
F C A
A B QG
B C √
D
H D √
E √
G E F √
G √
Nodes visited: D, C, E, F H

Dequeue F. Visit F. Enqueue unenqueued nodes


adjacent to F.
Walk-Through
Enqueued Array
F C A
A B QH
B C √
D
H D √
E √
G E F √
G √
Nodes visited: D, C, E, F, G H √

Dequeue G. Visit G. Enqueue unenqueued nodes


adjacent to G.
Walk-Through
Enqueued Array
F C A √
A B √ Q A B
B C √
D
H D √
E √
G E F √
G √
Nodes visited: D, C, E, F, G, H H √

Dequeue H. Visit H. Enqueue unenqueued nodes


adjacent to H.
Walk-Through
Enqueued Array
F C A √
A B √ QB
B C √
D
H D √
E √
G E F √
G √
Nodes visited: D, C, E, F, G, H, A H √

Dequeue A. Visit A. Enqueue unenqueued nodes


adjacent to A.
Walk-Through
Enqueued Array
F C A √
A B √ Q empty
B C √
D
H D √
E √
G E F √
G √
Nodes visited: D, C, E, F, G, H, H √
A, B
Dequeue B. Visit B. Enqueue unenqueued nodes
adjacent to B.
Walk-Through
Enqueued Array
F C A √
A B √ Q empty
B C √
D
H D √
E √
G E F √
G √
Nodes visited: D, C, E, F, G, H, H √
A, B
Q empty. Algorithm done.
Depth First search

1. We select a path in the maze and we follow it until we hit a dead


end or reach the finishing point of the maze.
2. If a given path doesn’t work, we backtrack and take an
alternative path from a past junction, and try that path.
3. Example : Maze
Solution (DFS) Solution (BFS)
Walk-Through
Visited Array
F C A
A B
B C
D
H D
E
G E F
G
H

Task: Conduct a depth-first search of the


graph starting with node D
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:
Visit D
D
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:
Consider nodes adjacent to D,
D decide to visit C first (Rule:
visit adjacent nodes in
alphabetical order)
Walk-Through
Visited Array
F C A
A B
B C √
D
H D √
E
G E F
G C
H D
The order nodes are visited:
Visit C
D, C
Walk-Through
Visited Array
F C A
A B
B C √
D
H D √
E
G E F
G C
H D
The order nodes are visited:
No nodes adjacent to C; cannot
D, C continue  backtrack, i.e.,
pop stack and restore
previous state
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:
Back to D – C has been visited,
D, C decide to visit E next
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:
Back to D – C has been visited,
D, C, E decide to visit E next
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:
Only G is adjacent to E
D, C, E
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:
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.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy