Professional Documents
Culture Documents
Page 1 of 28
DATA STRUCTURES
Definition: Data may be organized in many different ways. The logical (or)
mathematical model of a particular organization of a data is called data
structure. The following are the various data structures:
1) Stacks, 2) Queues, 3) Linked List, 4) Trees, 5) Graphs
Stacks, queues and linked list are called linear data structures. Trees and
graphs are called non-linear data structures.
Stacks: A stack is a linear list (data structures) in which all insertions and
deletions of entries are made at same end. This end is called TOP. The other
end is called BOTTOM.
EX: A stack of dishes, a stack of books, coins etc.
In a stack an item may be added (or) removed only from the top of the stack
i.e. last element to be inserted into the stack will be the first to be removed.
For this reason stacks are called LAST IN FIRST OUT (LIFO) lists. The
following diagram shows a stack with five elements.
A pointer TOP is used to specify the top element of the stack. Initially
when the stack is empty, TOP has a value of -1. When the stack contains a
single element, top has a value of zero and so on. Each time a new element is
inserted into the stack the pointer is incremented by 1. The pointer is
decremented by 1 each time a deletion is made from the stack.
Stack Operations
Basically, there are two operations performed on the stack.
Push: Push is the term used to insert the element on the top of stack.
Pop: Pop is the term used to delete the elements from top of the stack.
Algorithm: PUSH(S, TOP, X): this algorithm inserts the elements.
Step: 1 [check for the stack over flow]
If (top>max) then write (stack over flow)
Return
Honey Creations
Unit-V-Data Structures
Page 2 of 28
top+1
Step: 4 [finished]
Return
Algorithm: pop(s, top, x): [this algorithm deletes the top element from the
stack]
Step: 1 [check for the stack under flow]
If (top=-1) then write (stack underflow)
Return
Step: 2 [assign top element to x]
X
s[top]
top-1
Step: 4 [finished]
Return
QUEUES:
A queue is a linear list in which additions and deletions take place at
different ends. The end at which new elements are added is called REAR and
from which old elements are deleted is called FRONT. Queues are also called
First in First out Lists (FIFO). The order in which an element is entered in a
queue is the order in which they leave queue. This is also called First Come
First Serve (FCFS) lists.
Ex: The people waiting in a line from a queue where the first person in a line is
the first person to be processed.
Queues may be represented in computer memory in various ways, usually by
means of one way lists (or) linear arrays. Each of the queues will be
maintained by a linear array queue and two variables. F containing the
location of the first element of the queue and R containing the last element
of the queue. The condition F=-1 will indicate that the queue is empty.
Whenever an element is deleted from the queue the value of F is incremented
by 1 and whenever an element is added into the queue the value of R is
incremented by 1.
Queue Operations:
There are mainly two operations that can be performed on queue. They are:
Honey Creations
Unit-V-Data Structures
Page 3 of 28
Circular Queue:
Let us have an array A that contains n elements in which A[1] comes just
after A[n]. When this technique is applied to construct a queue it is called
circular queue.
(Or)
Circular queue is a linear list represented in memory, in which additions
and deletions takes different ends (rear and front). The elements in a circular
queue cq[0],cq[1], . . . ,cq[n-1] are arranged in a circular fashion with cq[0]
followed by cq[n-1].
Consider an example of circular queue that contains a maximum of 5
elements. A diagram of circular queue contents is shown below:
Honey Creations
Unit-V-Data Structures
Page 4 of 28
Algorithm: 1:- CQ INSERT (Q, F, R, X):- [given F, R are pointer to the first and
last elements of a circular queue consisting of max+1 elements. This
algorithm inserts x at the rear (R) of the queue. Initially F,R are set to -1]
Step: 1 [check for overflow]
If ( ((R+1) % (MAX+1) ) )=F) then write ( C QUEUE overflow )
Return
Step: 2 [increment R]
R
( (R+1)%(MAX+1))
Step: 3 [insert element]
Q[R]
X
Step: 4 [if F properly set]
If(F=-1) then F
0
Step: 5 [finish]
Return
Algorithm: 2 CQ DELETE (Q, F, R, X):- [given F, R are pointers to the first
and last elements of a circular queue consisting of max+1 elements. This
algorithm deletes the front [F] element of the queue and places it into variable
x]
Step: 1 [check for underflow]
If (F=-1) then write (CQ underflow)
Return
Step: 2 [assign F element to X]
X
Q[F]
Step: 3 [update F pointer]
If (F==R) then F
R
-1
Step: 4 [finish]
Return
DEQUE (DOUBLE ENDED QUEUE):
A deque is a linear list in which the elements can be added (or) removed at
either ends but not in the middle.
Honey Creations
Unit-V-Data Structures
Page 5 of 28
Front [K] and Rear [K] contains the front and rear elements of row k of
queue, the row that maintains the queue of elements with priority number K.
Difference between stacks and queue
Stack
Queue
1) A stack is a linear list of elements In (1) queue is a linear list in which
which the elements are added (or) elements are added at one end called
removed at the same end Called top
rear and deleted at other end called
front
(2)
In stacks, the elements which is (2) In queues, the element which is to
Inserted last in first to be removed
inserted first
is to be removed.
(3)
Stacks are last in first out (LIFO)
(3) Queues are first in first out (FIFO)
(4)
In stacks, the elements are
Removed in the reversed order
Of
which they are inserted
(5)Suppose the elements A,B,C,D,E are
inserted into a stack, then the deletion
of these elements will be E,D,C,B,A
Honey Creations
Unit-V-Data Structures
Page 6 of 28
(7)In stacks, inserting and Deleting elements 7. In queues, inserting elements are
are known as push and pop known as q insert and q delete
operations.
LINKED LIST
A linked list is a linear collection of data elements called nodes. In linked
lists the linear order is given by means of pointers .i.e., each node is divided
into two parts. The first part contains the information of elements (data) and
the second part called the link field contains the address of the next node in
the list.
Node Structure:
There are several kinds of linked lists which are available. They are 1. Linear
linked list, 2. Circular linear list.
1. Linear linked list: In linear linked list, the nodes are linked together in
same sequential manner.
2. Circular linear list: In this lists, the first node address is placed in a linked
field of last node.
Linked list with multiple pointers (Double linked list): These list permits
forward and backward travellers through the lists.
Honey Creations
Unit-V-Data Structures
Page 7 of 28
collects all the deleted space into free storage list. This technique is called
garbage collection. Garbage collection usually takes place in two steps:
1) It marks all the unused cells.
2) It collects all the marked cells into free storage list.
Honey Creations
Unit-V-Data Structures
Page 8 of 28
a. N-LINK=START
b. START=N
c. Exit
Step: 5 [initialize PTR to search]
PTR=START
Step: 6 [find the previous node address]
Repeat for i=1 to pos-2 do
a.
b.
1.
2.
PTR=PTR-LINK
If(PTR=NULL) then
Write ( Invalid position )
Exit
DELETING AN ELEMENT FROM THE SINGLE LINKED LIST:The following algorithm will delete an element from single linked list in a
specified position.
Algorithm: DELETE (list, start, avail, pos)
[This algorithm deletes a node in the single linked list at a position given by
pos. list is a linked list in a memory whose nodes contains data and link
fields. start is a pointer variable, which points to the first node in the linked
list. avail is a pointer variable which points to the first free node in the
availability list. P,PTR are temporary variables of type node]
Step: 1 [under flow]
If (START=NULL) then
a. Write ( list empty-cannot delete)
b. Exit.
Honey Creations
Unit-V-Data Structures
Page 9 of 28
If(pos=1) then
a. P=START
b. START=START-LINK
c. Goto step7
Step: 3 [initialize PTR to search]
PTR=START
Step: 4 [find the previous node address of the node to be deleted]
Repeat for i=1 to pos-2, do
a. PTR=PTR link
b. If (PTTR=NULL) then
1. Write (invalid position)
2. Exit.
Step: 5 [save the deleted address in p]
P=PTR-link
Step: 6 [delete the node]
PTR-LINK=P-LINK
Step: 7 [return deleted node to avail list]
p-link=avail
avail=p
Step: 8 [exit]
Linked List
not
Honey Creations
Unit-V-Data Structures
Page 10 of 28
6. The linear relationship b/w the data data elements of a linked list is
elements of an array Is reflected by the reflected by the link of field of
the
physical relationship of the data in node.
memory.
7. In linked list there is no need of such
7. In array, declaring a block of memory thing.
space is reserved.
8. In linked list a pointer is stored
8. There is no need of storage for along with data elements.
pointer (or) links.
9. The conceptual view of linked list
9. The conceptual view (memory map)
of an array.
10. In arrays the address of the First 10. The linked list the address of the
element (base element) is stored in the first node is stored in another variable
called start
array variable.
Left pointer contains the address of the previous node, right pointer contains
the address of the next node and data contains the actual information of the
node.
In the above diagram, left end and right end are two pointer variables which
contain the address of the first node and last node respectively.
Inserting the node into double linked list (insertion):
Let list be a double liked list with successive nodes A & B as shown below:
Suppose a node N is to be inserted into a linked list between the nodes A and
B.
Honey Creations
Unit-V-Data Structures
Page 11 of 28
After insertion, in the new list the right pointer of a point to the node N, left
pointer of the node N points to the node A. The right pointer of the node N
points to the node B and left pointer of node B points to the node N.
Deleting a node from double linked list (deletion):
Let list be a double linked list that contains the node N between the nodes A
and B as shown below:
Suppose the node N is to be deleted from the list. DLL (double linked list) of
the deletion is as follows.
1. In SLL the list will be traversed 1. In DLL the list will be traversed in
in only one way i.e., in two ways either in forward (or)
forward
direction.
Backward directions.
2. In SLL the node contains only
one link field.
3. The conceptual view (a logical
organisation) of SLL is:
Honey Creations
Unit-V-Data Structures
Page 12 of 28
TREES
Definition: A tree is a finite set of one (or) more nodes such that,
1. There is a specially designated node called root.
2. The remaining nodes are partitioned into n>0 disjoint sets t1,t2.tn.
where each of these sets in a tree t1,t2..tn are called the sub-trees of
the root.
Node:
Honey Creations
Unit-V-Data Structures
Page 13 of 28
A node stands for the item of information on the branches to the other
items. Consider a tree in the above diagram which contains 13 nodes. Each
item of data is a single alphabet.
The root is A and the tree will be drawn from the root node.
(a) Degree: The no of sub-trees of a node is called its degree. The degree
of A is 3, degree of C is 1, degree of M is 0.
(b) Leaf (or) terminal nodes: The nodes that have degree zero are called
leaf (or) terminal nodes. K,L,F,G,I,J,M are leaf nodes.
(c) Non-terminal nodes: The nodes having degree non-zero are called
non-terminal nodes. A, B, C, D, E, H is non-terminal nodes.
(d)Children and parent: Every node in the tree can have some children
nodes. Each child node can also have children and so on. A node that has
a child is called the parent node. The children of D are H, I, J. the
parent of D is A.
(e) Siblings: Children of the same parent are called siblings.
For example H,I,J are siblings. The same terminology can be
extended i.e., the grand parent of M is D.
(f) Ancestors: Ancestors of a node are all the nodes in the path from the
root to that node. Ancestors of M are A, D, H.
(g) Degree of a tree: The degree of a tree is a maximum degree of the
node in a tree. The tree in the diagram has the degree 3.
(h) Level: The root node is at level 1. If a node is at level 1, then the
children are at L+1. The above diagram shows the levels of all nodes in
that degree.
Height and depth: The height or depth of a degree is defined to the
maximum level of any node in the tree.
Forests: The forest is the set of n>0 disjoint trees (if we remove a root of a
tree, we get forests). In the above diagram, if we remove A, we get forests
with 3 trees.
Binary trees
The binary tree is a finite set of nodes, which is either empty or consists of
a root and two disjoint binary trees called the left sub-tree and right subtree.
In the above example, these three binary trees are different. The second
one has an empty right sub-tree and third one has an empty left sub-tree.
In a binary tree, if all the right sub-trees are NULL then that binary tree is
called skewed to left. Similarly if all the left sub-trees are empty then it is
skewed to right.
Honey Creations
Unit-V-Data Structures
Page 14 of 28
Full binary tree: a full binary tree of depth K has 2k-1 nodes i.e., the tree
having maximum no of possible nodes.
Honey Creations
Unit-V-Data Structures
Page 15 of 28
Honey Creations
Unit-V-Data Structures
Page 16 of 28
Binary Search Tree: Suppose T is a binary tree, and then T is called a binary
search tree (or) binary-sorted tree if each node N of T has the following
properties:
The value of N is equal or less than every value in the right sub-tree of
N.
Hence the inorder of binary tree (T) will give a sorted list.
Inserting a Node Into a Binary Search Tree:A node will always be inserted into its appropriate position in the tree as a leaf.
The following example shows a series of insertions into a binary tree.
Honey Creations
Unit-V-Data Structures
Page 17 of 28
Method: Given the root of binary search tree and a value will be added to the
tree as follows.
(1) Create a new node for a new value and initialize it.
(2) Search for the insertion place in the tree.
(3) Fix pointers to insert the new node.
Algorithm:- INSERT (ROOT, VAL)
[ This algorithm inserts a new node containing the value VAL in the binary
search tree with the ROOT ]
Step (1):- [ create a new node and initialize it]
(a) Get a new node N from avail list.
(b)N. data=val
(c) N. left= NULL
(d)N. right=NULL
Step (2):- [search for the insertion place in the tree]
(a) P=root
(b)Back=NULL
(c) Repeat while (p!=NULL)
(1) Back = p
(2) If ( val < p. data ) then
P=p. data
Else
P=p. right
[ end if]
[end of the step(2) loop]
Step (3):- [ position found, fix pointers to the new node]
[ is the tree empty ]
If ( back==NULL) then
Root=N
Else
If ( val < back. Data ) then
Back. Left = N [insert as left node]
Else
Back. Right = N [ insert as right node]
[ end if ]
[ end if ]
Step (4):- exit
Deleting a Node From a Binary Search Tree:Deleting a node from binary search tree varies from the position of the
node in a tree.
Deleting a leaf node:- (No children)
Setting the appropriate link of its parent to null:-
Honey Creations
Unit-V-Data Structures
Page 18 of 28
Deleting a node with only one child:Make the pointer from the parent to the child of the node c by skipping
the node x.
Deleting a node with two children:Replace the node we wish to delete with the node which is closest value
to the deleted node.
Honey Creations
Unit-V-Data Structures
Page 19 of 28
GRAPHS
A graph is a collection of vertices (nodes), which are joined by edges. A
graph G consists of two sets called vertex V and edge E. V is a finite nonempty set of vertex (nodes or points) and E is a finite set of pairs of vertices.
Each pair in E is an edge of G. A graph can be written as G=(V, E).
Any two nodes, which are connected by an edge in a graph, are called
adjacent nodes.
In a graph G=(U,V) and edge which is directed from one node to another
is called a directed edge. An edge which has no specific direction is called an
un-directed edge. A graph in which every edge is undirected is called an
undirected graph and in which every edge is directed is called directed
graph. If some of the edges are directed and some are un-directed in a graph
then that graph is called mixed graph.
Honey Creations
Unit-V-Data Structures
Page 20 of 28
Ex:- In the following diagram the directed edges are known by means of
arrows, which also shows the direction.
In the above diagram (2) and (5) are directed graphs. (3) and (6) are undirected graphs, (4) is a mixed graphs, (1) is either a directed (or) un-directed
graph.
Let V, E be a graph and let XE directed edge associated with ordered
pair nodes then the edge X is said to be initially (or) originating at U and
terminating (or) ending at V. the node U and V are called the initial and
terminal nodes of the edge X.
An edge of a graph which joints a node to itself is called a loop.
Direction of a loop is of no significance. Hence it can be considered either a
directed (or) un-directed edge. If certain pairs of nodes are joined by more
than one edge then such edges are called parallel edges. Any graph which
contains some parallel edges is called a multi-graph. If there is no more than
one edge between pairs of nodes then such a graph is called a simple graph.
In a graph a node which is not adjusted adjacent to any another node is called
isolated node. A graph with isolated node is called a null graph i.e. the set of
edges in a null graph is empty.
Honey Creations
Unit-V-Data Structures
Page 21 of 28
In a directed graph, for any node V the no of edges which have V as the
initial node is called the out degree of the node V. The no. of edges which
have V as their terminal node is called the in degree of the node. The sum
of the out degree and in degree of a node V is called its total degree.
Let G=(V,E) be a simple di-graph, consider a sequence of edges of G
such that the terminal node of any edge in the sequence is the initial node of
next edge.
G= ( ( v1,v2), (v2,v3)..(vk-1,vk) ) can be written as
G= ( v1,v2,v3.vk-1,vk).
Path: - A path is said to be traverse through the node appearing in the
sequence, originating in the initial node of the first edge and ending in the
terminal node of the last edge in the sequence. The no of edges appearing in
the sequence of a path is called the length of a path.
Ex:- consider the graph some of the paths originating in the node 2 and ending
in the node 4 are
P1: (2,4)
P2: ((2,3),(3,4))=(2,3,4)
P3: ((2,1),(1,4))=(2,1,4)
P4: ((2,3),(3,1),(1,4))=(2,3,1,4)
P5: ((2,3),(3,2),(2,4))=(2,3,2,4)
P6: ((2,2),(2,4))=(2,2,4)
Representation of a graph: There are two common ways to represent a
graph. They are sequential representation and linked representation. The
sequential form uses a square table i.e., in matrix form.
Sequential Representation of a Graph ( Matrix Representation):Let G=(V,E) be a simple di-matrix in which V={ v1,v2.vn} are the
nodes ordered from v1 to vn.
An nxn matrix A whose element aij is given by,
If G is a di-graph,
Aij= 1, if vi,vj EE=0, otherwise
If G is a undirected graph,
Aij=1, if (vi,vj)EE (or) (vi,vj)EE=0, otherwise.
Then A is called adjacency matrix of the graph G.
Honey Creations
Unit-V-Data Structures
Page 22 of 28
Graph traversal:- In many applications all the vertices in the graphs are to
be visited. There are two types of graphs traversal methods. They are
1. Breadth first search (BFS)
2. Depth first search (DFS)
Breadth first search (or) breadth first traversal:In BFS we start at vertex and mark it has visited. All the unvisited vertices
adjacent from v are visited next, putting the vertices adjacent to these in a
waiting list to be traversed after all vertices adjacent to V.
Honey Creations
Unit-V-Data Structures
Page 23 of 28
Algorithm:- BFS(V)
[ A BFS of graph is carried out beginning at vertex v. all vertices are marked as
visited (i) =1. The graph G and array visited are global and visited is
initialized to zero. Q is a queue of unexplored vertices]
Step (1):- [initialize first node and mark it as visited]
U=V
Step (2):- [initialize Q to be an empty queue ]
Step (3):- [repeat for even
(a) Repeat for all vertices w adjacent U do.
(1)[ if w is not visited then make it has visited ]
If (visited [w]=0) then
(i.2) visited [u]=1
[ end if ]
[ end if ]
(b)[ all vertices are visited ]
If q is empty then exist.
(c) [ get first un-explored vertex]
Delete Q( )
[ end step (3) loop]
Step (4):- exit
If BFS is used on a connected undirected graph then all vertices in G
get visited and the graph is traversed. If G is not connected, then at least one
vertex of G is not visited. A complete traversal of G can be repeatedly calling
BFS, each time with a new unvisited starting vertex.
Algorithm:- BFT(G,N)
[breadth first traversal of a graph G with N vertices is an array of n elements]
Step(1):- [ may all vertices as unvisited]
VISITED[i]=0
Step(2):- [ repeatedly call BFS for all unvisited vertices]
Repeat for i=1 to n do.
(1)If ( VISITED [i]=0) then call BFS(i)
[ end of step (2) loop]
Step(3):- exit.
Depth first traversal (or) depth first search:In DFS, we start vertex V and mark it as visited let w1,w2..wk be the
vertices adjacent to V. then we shall next visit w1 and keep w2.w3.wk
Honey Creations
Unit-V-Data Structures
Page 24 of 28
Honey Creations
Unit-V-Data Structures
Page 25 of 28
Graphs
Minimum Spanning Tree:
Minimum spanning tree is related to the weighted graph.
Minimum spanning tree is defined as that the sum of all the weights of
all the edges in the tree is minimum.
There are several methods available for finding minimum
spanning tree of a graph. Out of them, two methods are known to be
efficient.
Kruskals algorithm
Prims algorithm
Kruskals Algorithm:
1. List all the edges of the graph G in the increasing order of weights.
Honey Creations
Unit-V-Data Structures
Page 26 of 28
2. Select the smallest edge from the list and add it into the spanning
tree (initially it is empty) if the inclusion of this edge does not
make a cycle.
3. If the selected edge with smallest weight forms a cycle, remove it
from the list.
4. Repeat steps 2-3 until tree contains n-1 edges of list are empty.
5. If the tree T contains less than n-1 edges and the list is empty.
Spanning tree is possible for the graph else return the minimum
spanning tree T.
SORTINGS:
Sorting means placing the collection of values in one order either
increasing order or decreasing order.
There are different types of sorting, they are:
1. BUBBLE SORT:
Bubble sorting is an algorithm in which we are comparing first
two values and put the larger one at higher index. Then we take next
two values and put these values and place larger vale at higher index.
This process is repeated till the largest value is reached at last index.
Then start again from zero index up to n-1 index. The algorithm follows
the same steps repeatedly till elements are sorted
2. SELECTION SORT:
Honey Creations
Unit-V-Data Structures
Page 27 of 28
Honey Creations
Unit-V-Data Structures
Page 28 of 28
b) After adding v2 into the ordering set and removing it from the graph.
next indegree(v5)=0.
c) After adding v5 into the ordering set and removing it from the graph.
next indegree(v7)=0.
d) After adding v7 into the ordering set and removing it from the graph.
next indegree(v4)=0
e) After adding v4 into the ordering set and removing it from graph next
indegree of both v1 and v6 is 0. v1 is selected for next.
f) After adding v1 into the ordering set and removing it from the graph.
next indegree(v6)=0
g) After adding v6 into the ordering set and removing it from the graph.
The graph becomes empty.
Honey Creations