0% found this document useful (0 votes)
19 views41 pages

DS Unit 4

The document provides an extensive overview of tree and graph data structures, detailing definitions, types, properties, and traversal methods. It covers concepts such as directed and undirected graphs, various types of trees (including binary and AVL trees), and their respective traversal algorithms. Additionally, it discusses the representation of graphs using adjacency matrices and the concept of path matrices.

Uploaded by

ramkarbhavik8
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)
19 views41 pages

DS Unit 4

The document provides an extensive overview of tree and graph data structures, detailing definitions, types, properties, and traversal methods. It covers concepts such as directed and undirected graphs, various types of trees (including binary and AVL trees), and their respective traversal algorithms. Additionally, it discusses the representation of graphs using adjacency matrices and the concept of path matrices.

Uploaded by

ramkarbhavik8
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/ 41

SILVER OAK COLLEGE OF COMPUTER APPLICATION

SUBJECT: DATA
STRUCTURE
UNIT-4 : Tree and Graph

-PROF. ANKIT
PATEL
1. Discuss following
1. Graph
❑ A graph G consist of a non-empty set V called the set of nodes (points, vertices)
of the graph, a set E which is the set of edges and a mapping from the set of
edges E to a set of pairs of elements of V.
❑ It is also convenient to write a graph as G=(V,E).
❑ Notice that definition of graph implies that to every edge of a graph G, we can
associate a pair of nodes of the graph. If an edge X Є E is thus associated with a
pair of nodes (u,v) where u, v Є V then we says that edge x connect u and v.

2. Adjacent Nodes
❑ Any two nodes which are connected by an edge in a graph are called adjacent
node.

3. Directed & Undirected Edge


❑ In a graph G=(V,E) an edge which is directed from one end to another end is
called a directed edge, while the edge which has no specific direction is called
undirected edge.

4. Directed graph (Digraph)


❑ A graph in which every edge is directed is called directed graph or digraph.
5. Undirected graph
❑ A graph in which every edge is undirected is called undirected graph.

6. Mixed Graph
❑ If some of the edges are directed and some are undirected in graph then the graph is
called mixed graph.

7. Loop (Sling)
❑ An edge of a graph which joins a node to itself is called a loop (sling).

8. Parallel Edges
❑ In some directed as well as undirected graphs, we may have certain pairs of nodes joined
by more than one edges, such edges are called Parallel edges.

9. Multi graph
❑ Any graph which contains some parallel edges is called multi graph.

10. Weighted Graph


❑ A graph in which weights are assigned to every edge is called weighted graph.

11. Isolated Node


❑ In a graph a node which is not adjacent to any other node is called isolated node.
12. Null Graph
❑ A graph containing only isolated nodes are called null graph. In other words set of
edges in null graph is empty.

13. Path of Graph


❑ Let G=(V, E) be a simple digraph such that the terminal node of any edge in the
sequence is the initial node of the edge, if any appearing next in the sequence
defined as path of the graph.

14. Length of Path


❑ The number of edges appearing in the sequence of the path is called length of path.

15. Degree of vertex


❑ The no of edges which have V as their terminal node is call as in degree of node V
❑ The no of edges which have V as their initial node is call as out degree of node V
❑ Sum of in degree and out degree of node V is called its Total Degree or Degree of
vertex.

16. Simple Path (Edge Simple)


❑ A path in a diagraph in which the edges are distinct is called simple path or edge
simple.
17. Elementary Path (Node Simple)
❑ A path in which all the nodes through which it traverses are distinct is called
elementary path.

18. Cycle (Circuit)


❑ A path which originates and ends in the same node is called cycle (circuit).

19. Directed Tree


❑ A directed tree is an acyclic digraph which has one node called its root with in
degree 0, while all other nodes have in degree 1.
❑ Every directed tree must have at least one node. · An isolated node is also a
directed tree.

20. Terminal Node (Leaf Node)


❑ In a directed tree, any node which has out degree 0 is called terminal node or
leaf node.

21. Level of Node


❑ The level of any node is the length of its path from the root.
22. Ordered Tree
❑ In a directed tree an ordering of the nodes at each level is prescribed then such
a tree is called ordered tree.

23. Forest
❑ If we delete the root and its edges connecting the nodes at level 1, we obtain a
set of disjoint tree. A set of disjoint tree is a forest.

24. M-ary Tree


❑ If in a directed tree the out degree of every node is less than or equal to m then
tree is called an m-ary tree.

25. Full or Complete M-ary Tree


❑ If the out degree of each and every node is exactly equal to m or 0 and their
number of nodes at level i is m(i-1) then the tree is called a full or complete
m-ary tree.

26. Positional M-ary Tree


❑ If we consider m-ary trees in which the m children of any node are assumed to
have m distinct positions, if such positions are taken into account, then tree is
called positional m-ary tree.
27. Height of the tree
❑ The height of a tree is the length of the path from the root to the deepest node in
the tree.

28. Binary tree


❑ If in a directed tree the out degree of every node is less than or equal to 2 then tree
is called binary tree.

29. Strictly binary tree


❑ A strictly binary tree (sometimes proper binary tree or 2-tree or full binary tree) is a
tree in which every node other than the leaves has two children.

30. Complete binary tree


❑ If the out degree of each and every node is exactly equal to 2 or 0 and their number
of nodes at level i is 2(i-1) then the tree is called a full or complete binary tree.

31. Sibling
❑ Siblings are nodes that share the same parent node.

32. Binary search tree


❑ A binary search tree is a binary tree in which each node possessed a key that satisfy
the following conditions
8
Preorder

❑ Preorder traversal of a binary tree is defined as follow


o Process the root node
o Traverse the left sub tree in preorder
o Traverse the right sub tree in preorder
❑ If particular sub tree is empty (i.e., node has no left or right descendant) the
traversal is performed by doing nothing, In other words, a null sub tree is
considered to be fully traversed when it is encountered.
❑ The preorder traversal of a tree (Fig. 1.1) is given by A B C D E F G

In order

❑ The In order traversal of a binary tree is given by following steps, o Traverse the
left sub tree in In order
o Process the root node
o Traverse the right sub tree in In order
❑ The In order traversal of a tree (Fig. 1.1) is given by C B A E F D G
Post order

❑ The post order traversal is given by


o Traverse the left sub tree in post order
o Traverse the right sub tree in post order
o Process the root node
❑ The Post order traversal of a tree (Fig. 1.1) is given by C B F E G D A

Converse …

❑ If we interchange left and right words in the preceding definitions, we obtain


three new traversal orders which are called
o Converse Pre order (A D G E F B C)
o Converse In order (G D F E A B C)
o Converse Post order (G F E D C B A)
Algorithm of Preorder, In order and Post order traversal
techniques of the binary tree.
Procedure : RPREORDER(T)
❑ Given a binary tree whose root node address is given by pointer variable T and
whose node structure is same as described below. This procedure traverses the
tree in preorder, in a recursive manner.
1. [Check for empty Tree]
If T = NULL
then write (‘Empty Tree’)
return
else write (DATA(T))
2. [Process the Left Subtree]
If LPTR (T) ≠ NULL
then RPREORDER (LPTR (T))
3. [Process the Right Subtree]
If RPTR (T) ≠ NULL
the RPREORDER (RPTR (T))
4. [Finished]
return
Procedure : RINORDER(T)

❑ Given a binary tree whose root node address is given by pointer variable T and
whose node structure is same as described below. This procedure traverses the
tree in in order, in a recursive manner.

1. [Check for empty Tree]


If T = NULL
then write (‘Empty Tree’)
return
2. [Process the Left Subtree]
If LPTR (T) ≠ NULL
then RINORDER (LPTR (T))
3. [Process the root node]
write (DATA(T))
4. [Process the Right Subtree]
If RPTR (T) ≠ NULL
then RINORDER (RPTR (T))
5. [Finished]
return
Procedure : RPOSTORDER(T)

❑ Given a binary tree whose root node address is given by pointer variable T and
whose node structure is same as described below. This procedure traverses the
tree in post order, in a recursive manner.

1. [Check for empty Tree]


If T = NULL
then write (‘Empty Tree’)
return
2. [Process the Left Sub tree]
If LPTR (T) ≠ NULL
then RPOSTORDER (LPTR (T))
3. [Process the Right Sub tree]
If RPTR (T) ≠ NULL
then RPOSTORDER (RPTR (T))
4. [Process the root node]
write (DATA(T))
5. [Finished] return
Traversal order of following tree into In order, Preorder and Post
order.
Construct a tree for the given In order and Post order traversals
Construct a tree for the given In order and Post order traversals
In order : D G B A H E I C F
Post order : G D B H I E F C A
Construct a tree for the given In order and Preorder traversal
Preorder : G B Q A C K F P D E R H
In order : Q B K C F A G P E D H R
Create a binary search tree for the following data : 50 ,25 ,75,
22,40,60,80,90,15,30
Construct binary search tree for the following data and find its In
order, Preorder and Post order traversal
10,3,15,22,6,45,65,23,78,34,5
Write a short note on threaded binary tree

❑ The wasted NULL links in the binary tree storage representation can be replaced
by threads.
❑ A binary tree is threaded according to particular traversal order. e.g.: Threads for
the in order traversals of tree are pointers to its higher nodes, for this traversal
order.
o If left link of node P is null, then this link is replaced by the address of its
predecessor.
o If right link of node P is null, then it is replaced by the address of its
successor
❑ Because the left or right link of a node can denote either structural link or a
thread, we must somehow be able to distinguish them
❑ Method 1:- Represent thread a –ve address.
❑ Method 2:- To have a separate Boolean flag for each of left and right pointers,
node structure for this is given below,
❑ Head node is simply another node which serves as the predecessor and
successor of first and last tree nodes. Tree is attached to the left branch of the
head node
Advantages:

❑ In order traversal is faster than unthreaded version as stack is not required.


❑ Effectively determines the predecessor and successor for in order traversal, for
unthreaded tree this task is more difficult.
❑ A stack is required to provide upward pointing information in tree which
threading provides.
❑ It is possible to generate successor or predecessor of any node without having
over head of stack with the help of threading.

Disadvantages:

❑ Threaded trees are unable to share common sub trees.


❑ If –ve addressing is not permitted in programming language, two additional
fields are required.
❑ Insertion into and deletion from threaded binary tree are more time consuming
because both thread and structural link must be maintained.
Draw a right in threaded binary tree for the given tree
What is the meaning of height balanced tree? How rebalancing is done in
height balanced tree.

❑ A tree is called AVL (height balance binary tree), if each node possesses one of
the following properties

1. A node is called left heavy if the longest path in its left sub tree is one longer
then the longest path of its right sub tree.
2. A node is called right heavy if the longest path in the right sub tree is one
longer than path in its left sub tree.
3. A node is called balanced, if the longest path in both the right and left sub tree
are equal.

❑ If tree becomes unbalanced by inserting any node, then based on position of


insertion, we need to rotate the unbalanced node. Rotation is the process to
make tree balanced

1) Insertion into Left sub-tree of nodes Left child – Single Right Rotation
2) Insertion into Right sub-tree of node’s Left child – Left Right Rotation
3) Insertion into Left sub-tree of node’s Right child – Right Left Rotation
4) Insertion into Right sub-tree of node’s Right child – Single Left Rotation
1) Insertion into Left sub-tree of nodes Left child – Single Right Rotation

❑ If node becomes unbalanced after insertion of new node at Left sub-tree of


nodes Left child, then we need to perform Single Right Rotation for unbalanced
node.

Right Rotation
a. Detach leaf child’s right sub-tree
b. Consider leaf child to be the new parent
c. Attach old parent onto right of new parent
d. Attach old leaf child’s old right sub-tree as leaf sub-tree of new right child
2) Insertion into Right sub-tree of node’s Left child – Left Right Rotation

❑ If node becomes unbalanced after insertion of new node at Right sub-tree of


node’s Left child, then we need to perform Left Right Rotation for unbalanced
node.

Leaf rotation of leaf child followed by right rotation of parent


3) Insertion into Left sub-tree of node’s Right child – Right Left Rotation

❑ If node becomes unbalanced after insertion of new node at Left sub-tree of


node’s Right child, then we need to perform Right Left Rotation for unbalanced
node.

Single right rotation of right child followed by left rotation of parent


Leaf rotation of leaf child followed by right rotation of parent
4) Insertion into Right sub-tree of node’s Right child – Single Left Rotation

❑ If node becomes unbalanced after insertion of new node at Right sub-tree of


nodes Right child, then we need to perform Single Left Rotation for unbalanced
node.
Left Rotation
a. Detach right child’s leaf sub-tree
b. Consider right child to be new parent
c. Attach old parent onto left of new parent
d. Attach old right child’s old left sub-tree as right sub-tree of new left child
Construct AVL Search tree by inserting following elements in order of their occurrence 6,
5, 4, 3, 2, 1

Assignment:
❑ Define height of the binary tree. Define height balanced tree with its advantages.
Construct a height balanced binary tree (AVL tree) for the following data
42,06,54,62,88,50,22,32,12,33
❑ Construct the AVL search tree by inserting the following elements in the order of their
occurrence. 64, 1, 44, 26, 13, 110, 98, 85
What is graph? How it can be represented using adjacency matrix,
what is path matrix? How path matrix can be found out using
adjacency matrix .

Graph:

❑ A graph G consist of a non empty set V called the set of nodes (points, vertices)
of the graph, a set E which is the set of edges and a mapping from the set of
edges E to a set of pairs of elements of V.
❑ It is also convenient to write a graph as G=(V,E).
❑ Notice that definition of graph implies that to every edge of a graph G, we can
associate a pair of nodes of the graph. If an edge X Є E is thus associated with a
pair of nodes (u,v) where u, v Є V then we says that edge x connect U and V.

Adjacency matrix:

Let G = (V, E) be a simple diagraph in which V = {v1, v2,…., vn} and the nodes are
assumed to be ordered from v1 to vn. An n x n matrix A whose elements are aij
are given by
is called adjacency matrix of the graph G.
o Any element of the adjacency matrix is either 0 or 1.
o For a given graph G =m (V, E), an adjacency matrix depends upon the ordering of the
elements of V.
o For different ordering of the elements of V we get different adjacency matrices.

We can extend the idea of matrix representation to multi graph and weighted graphs. In
the case of multi graph or weighted graph we write aji = w, where aij denotes either the
multiplicity or the weight of the edge.
Which are the basic traversing techniques of the Graph? Write the
algorithm of them.

❑ Most graph problems involve traversal of a graph. Traversal of a


graph means visit each node exactly once.
❑ Two commonly used graphs traversal techniques are
1. Depth First Search (DFS)
2. Breadth First Search (BFS)
Depth First Search (DFS)

❑ It is like preorder traversal of tree.


❑ Traversal can start from any vertex vi
❑ Vi is visited and then all vertices adjacent to vi are traversed recursively using
DFS

❑ Since graph can have cycles, we must avoid re-visiting a node. To do this when
we visit a vertex V, we marks it visited as visited should not be selected for
traversal.
Procedure : DFS (vertecx V)

❑ This procedure traverse the graph G in DFS manner. V is a starting vertex to be


explored. S is a Stack, visited[] is an array which tells you whether particular
vertex is visited or not. W is a adjacent node of vertex V. PUSH and POP are
functions to insert and remove from stack respectively.
1. [Initialize TOP and Visited]
visited[] ← 0
TOP ← 0
2. [Push vertex into stack]
PUSH (V)
3. [Repeat while stack is not empty]
Repeat step 3 while stack is not empty
v ← POP()
if visited[v] is 0
then visited [v] ← 1
for all W adjacent to v
if visited [w] is 0
then PUSH (W)
end for
end if
Breadth First Search (BFS)
❑ This methods starts from vertex v0
❑ V0 is marked as visited. All vertices adjacent to v0 are visited next, Let vertices adjacent
to v0 are v1, v2, v3, v4. v1, v2, v3 and v4 are marked visited.
❑ All unvisited vertices adjacent to v1, v2, v3, v4 are visited next.
❑ The method continuous until all vertices are visited
❑ The algorithm for BFS has to maintain a list of vertices which have been visited but not
explored for adjacent vertices.
❑ The vertices which have been visited but not explored for adjacent vertices can be stored
in queue, Initially the queue contains the starting vertex.
❑ In every iteration, a vertex is removed from the queue and its adjacent vertices which are
not visited as yet are added to the queue.
❑ The algorithm terminates when the queue becomes empty.
Procedure : BFS (Vertex V)

❑ This procedure traverse the graph G in BFS manner. V is a starting vertex to be


explored. Q is a queue, visited[] is an array which tells you whether particular
vertex is visited or not. W is a adjacent node of vertex V.
1. Initialize Q
2. [Marks visited of V as 1]
visited [v] ← 1
3. [Add vertex v to Q]
Insert Queue(V)
4. [Repeat while Q is not empty]
Repeat while Q is not empty
v ← RemoveFromQueue()
For all vertices W adjacent to v
if visited[w] is 0
then visited[w] ← 1
InsertQueue(w)
what is spanning tree?

❑ A Spanning tree of a graph is an undirected tree consisting of only those edges


necessary to connect all the nodes in the original graph
❑ A spanning tree has the properties that
o For any pair of nodes there exists only one path between them
o Insertion of any edge to a spanning tree forms a unique cycle
❑ The particular Spanning for a graph depends on the criteria used to generate it.
❑ If DFS search is use, those edges traversed by the algorithm forms the edges of
tree, referred to as Depth First Spanning Tree.
❑ If BFS Search is used, the spanning tree is formed from those edges traversed
during the search, producing Breadth First Search Spanning tree.
Define spanning tree and minimum spanning tree. Find the minimum spanning
tree of the graph shown in Fig.

You might also like