DS Unit 4
DS Unit 4
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.
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.
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.
31. Sibling
❑ Siblings are nodes that share the same parent node.
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
Converse …
❑ 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.
❑ 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.
❑ 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:
Disadvantages:
❑ 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.
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
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
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.
❑ 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)