Vip Dewas Call Girls #9907093804 Contact Number Escorts Service Dewas
Unit 8
1. DATA STRUCTURE:
It is a method of representation of logical relationships between individual data
elements related to the solution of a given problem.
LINEAR:
In the linear data structure, values are arranged in a linear fashion. Linked list,
stacks and queues are the examples of linear data structure in which values are stored in
sequence.
NON LINEAR:
The data values in this structure are not arranged in order. Trees, graphs are
examples of non-linear data structure.
2. Trees are used extensively in computer science to represent algebraic formulas as an
efficient method for searching large, dynamic lists and for such diverse applications as
artificial intelligence systems and encoding algorithms.
A tree consists of a finite set of elements called nodes, and a finite set of directed
lines, called branches that called branches that connect the nodes.
DATA STRUCTURE
LINEAR NON-LINEAR
LINEAR
LINEAR LINKED LISTS
LINKED LISTS STACKS
STACKS QUEUES
QUEUES TREES
TREES GRAPHS
GRAPHS
3. The number of branches associated with a node is the degree of the node.
When the branch is directed towards the node, it is an in degree branch.
When the branch is directed away from the node, it is an out degree branch.
The sum of the in degree and out degree branches is degree of node.
If the tree is not empty, the first node is called the root.
TERMINOLOGY:
• A leaf is any node with an out degree of zero i.e., a node with no successors.
• A node is a parent if it has successor nodes i.e., if it has out degree > zero.
• A node with a predecessor is a child. A child node has an in degree of one.
• Two or more nodes with the same parent are siblings.
• An ancestor is any node in the path from the root to the node.
• A descendent is any node in the path below.
4. A tree may be defined as a set of certain elements called as nodes.
One node is named as root, while the remaining nodes may be grouped into
sub-sets, each of which is a tree in itself.
Each sub tree is referred to as child of the root, while the root is referred to as
the parent of each sub tree.
If a tree consists of a simple node, then that node is called a leaf.
A
B C D
E F G H I
In the above figure . A is root and E,F,G,H,I,D are leafs.
5. BINARY TREE:
A binary tree is a tree, which is either empty , or in which every node
a) has no children or
b) has just a left child or
c) has just a right child or
d) has just a left and right child.
LEVEL OF A NODE:
The level of a node refers to its distance from the root node.
1 LEVEL 0
2 LEVEL 1
3
4 5
LEVEL 2
6
LEVEL 3
6. COMPLETE BINARY TREE:
A complete binary tree may be defined as a tree in which , except
the last level, at each level n there should be 2^n nodes.
1 LEVEL 0
2
3 LEVEL 1
4 5
6 9 LEVEL 2
7 8
DEPTH( OR HEIGHT) OF A TREE: 1
1
Length of path from x to y is the total number of 2 3
branches lying along that path. 2
4 5
Max level number of a binary tree is known as its 3
height( or depth). 7
6
Depth of a complete binary tree with n nodes=log n 4
8
7. BINARY SEARCH TREE:
A binary search tree is a binary tree that is either empty or in
which every node contains a key and satisfies the conditions
1. The key in the left child of a node ( if exists) is less than the key in its
parent node.
2. The key in the right child of a node (if exists ) is greater than the key in
its parent node.
3. The left and right sub trees are again binary search trees.
d
The nodes of right sub
A binary b tree are greater than d and
f nodes of left sub tree are
search
tree. less than d.
a c c
Operations on binary search tree: The main operations performed on binary search trees are
a. Insertion
b. Deletion.
8. a. Inserting a new node in binary search tree
To insert a new key into binary search tree,
The following actions are performed:
1. If the tree is empty, the root pointer is assigned address of new node.
2. If the tree is not empty, then compare the key with one in the root. If it is less than
the key of root, then move to left sub tree otherwise, more to right sub tree. Of the
key is equal then it is a duplicate.
3. Repeat the step2 until, the node is inserted at any place. The node is placed at the
position when there are no nodes to compare after repeating the step2.
18 18 18
18
10 28 10 28 10 28
10 28
8 8 11 8 11 25
9. b. Deletion from a binary search tree:
g
We have to consider different cases for deletion:
Case1: If the node to be deleted is leaf b
i
Then replace the link to the deleted node by NULL
a c h
g
b i null
null a null null c null null h null
10. Delete c:
The binary search tree after deletion will be
g
g
b null i null
b
i
null a null null h null a h
11. Case 2: If the deleted node has only one sub tree
Then adjust the link from parent of the deleted
node to point to its right sub tree.
Example : delete ‘i’ from tree as shown in figure below
g
g
b
h
b h
a c
a c
12. Case 3 : If the deleted node has two sub trees:
In this case attach right sub tree is place of deleted node. Then
hang left sub tree onto an appropriate node of right sub tree.
Example: delete node ‘b’ from binary tree as shown in fig below
g
g
c i
c i
a a h
h
13. TRAVERSING INTO A BINARY SEARCH TREE:
In order traversing: 18
1) Traverse the left sub tree.
10 28
2) Visit the root.
3) Traverse the right sub tree. 8 11 25 8 10 11 18 25 28
Pre order traversing:
1) Visit the root.
18 10 8 11 28 25
2) Traverse the left sub tree.
3) Traverse the right sub tree.
Post order traversing:
1) Traverse the left sub tree. 8 11 10 25 28 18
2) Traverse the right sub tree.
3) Traverse the root.
14. GRAPHS
DEFINITION:
A graph is a collection of nodes, called vertices, and a collection of
segments, called lines, connecting pairs of vertices.
A graph consists of 2 sets, a set of vertices and a set of lines or A graph is a set of
nodes and arcs. The nodes are also termed as vertices and arcs are termed as
edges.
The set of nodes denoted as O P
{O,P,R,S,Q}
The set of arcs denoted as R
Q
{ (O,P),(O,R),(O,Q),(Q,S),(R,S)} S
Graphs may be either directed or undirected. A directed graph or digraph is a graph in
which each line has a direction ( arrow head) to its successor.
The lines in a directed graph are known as arcs.
15. An undirected graph is graph in which there is no direction (arrow head) on
any of the lines, which are known as edges.
In an undirected graph, the flow between 2 vertices can go in either direction.
A A
B E B E
F F
C D C D
a) Directed graph. b) Undirected graph.
•A path is a sequence of vertices in which each vertex is adjacent to the next one.
{A,B,C,E } is one path.
{ A,B,E,F} is another path.
•A cycle is a path consisting of at least three vertices that starts and ends with the
same vertex.
from above fig: B,C,D,E,B is a cycle
16. Loop is a special case id a cycle in which a single A B
are begins and ends with the same vertex.
C D
A graph is linked if there is a pathway between any 2 nodes of the graph, such a
graph is called a connected graph or else it is a non connected graph.
P Q P Q
R S R S
Connected graph Non connected graph
SUB GRAPH: A sub graph of a graph G=(V,E) is a graph G where V(G) is a
subset of V(G). E(G) consists of edges (V1,V2) in E(G), such that both V1 and V2
are in V(G).
17. a) A directed graph is strongly connected, if there is a path from each vertex to
every other vertex in the digraph.
A
G
B E
F
C D H I
A
b) A directed graph is weakly
connected if at least 2 vertices are not B E G
connected
F
C D
H I
Weakly connected
18. A
c) A graph is a disjoint graph B E
if it is not connected. G
F
C D H I
Strongly connected.
The degree of a vertex is the number of lines incident to it.
The out degree of a vertex is the number of arcs leaving the vertex.
In degree is the number of arcs entering the vertex.
From the above figure:
The degree of vertex B is 3. The maximum number of edges in an
The degree of vertex E is 4. undirected graph with n vertices is n(n-1)/2.
In degree of vertex E is 2 In a directed graph , it is n(n-1).
Out degree of vertex E is 2.
19. GRAPH REPRESENTATION:
Array representation:
S.NO 1 2 3 4
One way of representing a graph with
1 0 1 1 1
n vertices is to use an n^2 matrix. 1
(i.e., n rows and n columns) 2 1 0 1 1
2 4
If G is a directed graph, only e 3 1 1 0 1
entries would have been set to 1 in
the adjacency matrix. 1 1 1 0 3
4
Linked list representation:
Another way of representing a
graph G is to maintain a list for
2 3 4 NULL
every vertex containing all vertices 1
adjacent to that vertex . NULL
2 1 3 4
Struct edge
{ int num; 3 1 2 4 NULL
Sturuct edge *p;
4 1 2 3 NULL
};
20. Adjacency matrix:
It can be used to represent the graph. The 1
information of adjacency nodes can be stored
in the matrix. 2 5
Nodes [i] [k]-0 indicates absence 6
1 indicates the presence of edge
between two nodes i and k
3 4
1 2 3 4 5 6
In degree Out degree
1 0 0 0 0 0 0 1 3 0
2 1 0 0 1 0 0 2 2 2
3 0 1 0 0 0 1 3 1 2
4 0 0 1 0 1 1 4 1 3
5 1 0 0 0 0 0 5 2 1
6 1 1 0 0 1 0 2
6 3
21. Spanning tree:
It is an un directed tree, containing only those nodes that are
necessary to join all the nodes in the graph. The nodes of spanning trees
have only one path between them.
In spanning trees, the number of edges are less by one than the number
of nodes.
In other words, a tree that contains edges including all vertices is called
spanning tree.
Also, in a graph, when the edges are shown connected between the
nodes in the order in which they are to be visited, resulting sub graph is
called spanning tree.
22. DIGRAPH (DIRECTED GRAPH):
A digraph is a graph in which edges have a direction v1 E5
e=(u, v) E1
E8
Edge ‘e’ can be traversed only from u to v, v2 v3
E6
u- initial vertex. E2 E7 E4
v-terminal vertex. v4 v5
E3
SIMPLE DIRECTED GRAPH:
V1
E4
Simple directed graph is a simple graph in E1
E5
which edges have a direction. E6
V2 V4
E2
V3 E3
23. WEIGHTED GRAPH:
In a weighted graph, each edge is assigned some weight i.e., a real positive number
is associated with each edge.
PATH:
In a non- weighted graph, a path P of length n from u to v can be defined as follows:
P={ V0, V1, V2, …….., Vn}
Where u=V0 and v=Vn and there is an edge from Vi-1 to Vi (0<i<n).
In a directed graph Vi-1 is taken as initial vertex and Vi is taken as terminal vertex.
A simple path from u to v is a path where no vertex appears more than once.
CYCLE:
A path P={ V1, V2,………….. Vn} is said to be a cycle, when all its vertices are
distinct except V0 and Vn , meaning thereby V0=Vn then a simple path P is called a
cycle.
24. v1
CONNECTED GRAPH:
A graph G is called a connected graph, if there v2 v3
is at least one path between every pair of
vertices.
v4 v5
COMPLETE GRAPH:
v1
A graph with an edge between each pair of
vertices is said to be a complete graph.
|E| =n*(n-1)/2 v2 v5
|E|- total number of edges
n- number of vertices.
0<= |E|<= n(n-1)/2. v3 v4
25. DEPTH FIRST TRAVERSAL:
In this method, we start from a given vertex v in the
graph. We mark this vertex is visited. Now any of the unvisited vertex
adjacent to v is visited. Then neighbor of v is visited. The process is
continued until no new node can be visited. Now we bock track from the path
to visit unvisited vertices if any left. Stack is used to keep track of all nodes
adjacent to a vertex.
NOTE:
There may be several depth first
traversals and depth first spanning trees for A B C
a given graph. It mainly depends on how
the graph is represented (adjacency matrix
or adjacency list), on how the nodes are D E
numbered, on the starting node and on how
the traversal technique is implemented Here i) D is first successor of A
(i.e., using first successor, next successor and (B is next successor).
etc.)
ii) C is first successor of B.
26. ALGORITHM DEPTH FIRST[V]:
1. [Initialize all the nodes to ready state and stack to empty]
state [v]=1 ( 1 indicates ready state)
2. [Begin with any arbitrary node s in graph and push it on to stack and
change its state to waiting]
state [s]=2 (2 indicates waiting state)
3. Repeat through step5 while stack not empty
4. [ pop node N of stack and mark the status of node to be visited]
state [N]= 3 (3 indicates visited)
5. [push all nodes w adjacent to N to stack and mark their status as waiting]
state [W]=2
6. If the graph still contains nodes which are in ready state go to step2
7. Return.
27. BREADTH FIRST TRAVERSAL:
In this we start with given vertexV and visit all the nodes adjacent to V from left
to right. Data structure queue is use to keep track of all the adjacent nodes.
The first node visited is the first node whose successors are visited.
ALGORITHM FOR BREADTH FIRST [V]:
1. [Initialize all nodes to ready state]
state[V]=1 [ here V represents all nodes of graph]
2. [Place starting node ‘S’ in queue and change its state to waiting]
state [S]=2
3. Repeat through step5 until queue not empty.
4. [Remove a node N from queue and change status of N to visited state]
state [N]=3.
5. [ add to queue all neighbors W of ‘N’ which are in ready state and change their
states to waiting state]
state [W]= 2
6. End.