Академический Документы
Профессиональный Документы
Культура Документы
Data are simply values or set of values related with some entity ( object ).
DATA STRUCTURE :
Data may be organized in different ways in computer memory. The logical or mathematical model
of particular organization of data in computers memory is called as “ Data Structure ”.
The choice of particular data structure depends on two considerations.
1 It must be rich enough in a structure to mirror actual relationship of data with real world.
2 The structure must be simple enough so that one can effectively process the data.
ALGORITHMIC NOTATIONS :
An algorithm is well-defined list of steps use for solving particular program. Following are some
notations used while writing an algorithm.
LAGEST : This algorithm find largest element from vector A which contains N number of elements
and place result in Max, ‘I’ is used as subscript.
1) [ Is vector empty? ]
If N < = 0 then
Print, “Array is empty.” & Exit.
2) [ Initialization ]
MAX A [ I ]
I 2
6) Print MAX
7) STOP
2] Introductory Comment :
The algorithm name is followed other type of analysis the simplicity of algorithm.
Algorithm can be express in simple way so that it is easy to implement it and perform analysis but the
simplest and most straightforward algorithm sometimes is no best one. Usually this occurs when the
simplest and most straightforward algorithm requires more time and space. Therefore we may prefer a
complicated algorithm, which requires minimum time and space. Thus, it is necessary to perform time
and space analysis of algorithm.
ALGORITHM ANALYSIS :
It is necessary to learn how to perform analysis of algorithm. The first step of analysis is the
“correctness” of an algorithm. This analysis can be done by manual execution of algorithm on some data
values.
Isolating the instructions of two types “Active instruction” and “Book keeping instruction”. The
instruction which are executed by computer or contains arithmetic operations are called active
instruction. The instructions, which are used for initialization or for maintaining the values of variable,
are called “Book keeping instruction”.
In above algorithm there is only one active instruction, which is
SUM SUM + A [ I ]
Suppose, this instruction requires t1 amount of time and if get executed N number of times.
Therefore, the approximate time require for above algorithm is t1 * N. Here, bookkeeping instructions
are not taken in consideration because they require minimum time for their execution.
Space Analysis :
It is very difficult to calculate the space analysis of a algorithm because it requires
lot of factors to be considered and therefore generally space analysis of algorithm is
not perform.
Loc( Ai ) = L0 + C * ( I - 1 )
Suppose 1st element of array is stored from memory location 700 and 2 bytes are required to store
1 int value then the address of third element in an array can be calculated as
The two dimensional array can be represented by equivalent one-dimensional array in memory.
The two dimensional array requires m rows and n columns. The elements of two-dimensional matrix can
be stored either row wise or column wise.
If the element of two dimensional matrix are stored column wise then the address of ( i,j )th
element is given by
Loc( Ai,j ) = L0 + ( ( j – 1 ) * m + ( i – 1 ) ) * C
Similarly, to compute ( calculate ) the address of particular element in two dimensional array
which is stored row wise is
Loc( Ai,j ) = L0 + ( ( i - 1 ) * n + ( j – 1 ) ) C
STACK :
Stack is list of elements in which an element may be inserted or deleted only at one end, called top
of stack. This means, in particular, that elements are removed from a stack in the reverse order of that in
which they are inserted into a stack.
It is also known as FILO ( First in Last out ) type of memory because the element which is inserted
first is the element which is deleted at last. It is also called as LIFO ( Last in First out ).
A special terminology can be used to describe the operation of insertion and deletion. The operation
of insertion is called “PUSH” and the operation of deletion is called “POP”.
REPRESENTATION OF STACK :
A stack can be represented by two different ways in computer’s memory, either using “Array” or
using “Link List”.
If stack is represented using array, you require one array called “Stack[ ]”. Also you require
pointer variable called “TOP”, this pointer variable gives the address where you can insert or delete the
element.
If the stack memory is full and you are trying to insert the element on stack then the condition of
“OVER FLOW” occurs. If the stack is empty and you are trying delete the element from stack then the
condition of “UNDER FLOW” occurs.
You also required third variable called “MAXSTK”. This variable gives the maximum number of
elements that can be inserted in stack. Therefore, the condition TOP = 0 => stack is empty and TOP =
MAXSTK => stack is full.
PUSH : This algorithm insert element ITEM on TOP of the stack. The variable
MAXSTK gives maximum number of elements that can be stored in stack.
1) [ Is stack full ? ]
If TOP = MAXSTK
Then write “OVERFLOW” & Exit
3) [ Insert element ]
STACK [ TOP ] = ITEM
4) STOP
POP : This algorithm deletes element (TOP) from stack and store it in variable ITEM.
1) [ Is stack empty ? ]
If TOP = 0
Then print “UNDERFLOW” and Exit
4) STOP
1) [ Is stack empty ? ]
If TOP = 0
Then print “UNDERFLOW” and Exit
2) [ Print elements ]
Repeat for I=0 to TOP
Print “STK [ I ]”
3) STOP
APLLICATION OF STACK :
The data structure stack useful in many ways for the computer system. The main reason behind
learning data structure stack is that the computer system itself uses this data structure in many
situations. A stack memory can be used in many different applications such as
1) Polish Notations
2) Quick Sort Method
3) Recursive function
4) Tower of Hanoi
POLISH NOTATIONS :
E.g. : a + b
a*b
Polish notation refers to the notation in which operator symbol is placed before its operands. It is
also known as prefix notation.
E.g. : +ab
*ab
The fundamental property of polish notation is that the orders in which operator are evaluated is
completely determined by position of operator and not by priority. We never requires parenthesis while
writing expression using polish notation. The expression in polish notation is fast for execution as
compare to infix notation.
E.g. : A+(B*C)
= A + ( * BC )
= A+X * BC = X
= + AX
= + A * BC
Reverse polish notation is also called postfix notation. In this notation the operator is placed ofter
the operands.
E.g. : ab+
ab*
ALGORITHM FOR INFIX TO POSTFIX EXPRESSION :
POLISH ( Q, P )
Suppose Q is an arithmetic expression written in infix notation. This algorithm finds the
equivalent postfix expression P.
a) Repeatedly pop from STACK and add to P each operator ( on the top of
STACK ) which has the same precedence as or higher precedence than (*).
b) Add (*) to STACK.
[ End of If structure ]
a) Repeatedly pop from STACK and add to P each operator (on the top of STACK) until
a left parenthesis is encountered.
b) Remove the left parenthesis. [ Do not add the left parenthesis to P ]
[ End of If structure ]
[ End of Step 2 loop ]
7) Exit.
[ The terminology used for Step 5 is that (*) will “sink” to its own level. ]
RECURSIVE FUNCTION :
Another application of stack is process of recursion in this process of recursion actually uses. The
data structure stack for maintaining the value of previous function call. Suppose we have given program
with recursion to convert it into without recursion we have to use. The data structure stack to element
recursion statement use the stack is necessary.
Recursive is a program in which a function calls itself. Such function is called recursive function.
E.g. : Consider following program for calculate factorial of a number using recursive function.
void main( )
{
int n=4, f;
f = Fact (n);
printf (“ \n FACTORIAL IS : %d”,f);
}
The function control is transferred from main function to fact function the value 4 is passed as an
argument to the function fact when the statement :
F = n * F( n – 1)
Is executed again the function Fact ( ) called with an argument value three before calling the second
Fact( ) with argument the there the value of n will get push( ) in stack. Regularly in different function call
to Fact the value 3,2 are push in stack. The recursive process stop when value of n = 1 transfers the
control the previous control will value 1 at this stage function POP( ) will get called relative values of n.
The value 2 will get poped and whole statement is executed. Successively the POP( ) are executed to
relative top most value in stack.
QUEUE :
A queue is a linear list of elements in which deletions can take place only at one end, called the
“Front”, and insertions can take place only at the other end, called the “Rear”. The terms “Front” and
“Rear” are used in describing a linear list only when it is implemented as a queue.
Queue a are also called “First in First out” ( FIFO ) lists, since the first element in a queue will be
the first element out of the queue. In other words, the order in which elements enter a queue is the order
in which they leave. This contrasts with stacks, which are “Last in First out” ( LIFO ) lists.
REPRESENTATION OF QUEUE :
Queue can be represented in computer’s memory by two different ways, either by using array
or using link list.
When queue is represented using array, you required one array called QUEUE and two pointer
variables REAR and FRONT. The variable REAR gives the position where you can insert the element
and the variable FRONT gives the position from where you can delete elements.
Along with this you require a variable MAX, which give maximum number of elements that,
can be inserted in a queue.
Following fig shows how you can perform the operation of insertion and deletion in queue
1 2 3 4 5 6 7 F=1
A B C D R=4
F=2 Deletion
B C D R=4
F=2 Insertion
B C D E F R=6
Observe that whenever an element inserted in queue the value of “REAR” is increased by 1.
Similarly, whenever the element id deleted from queue the value of “FRONT” is increased by 1.
The condition FRONT = 0 indicates that queue is EMPTY. Similarly the condition REAR = N
indicates that queue is FULL is greater than I and REAR = n then queue is not FULL such a queue in
which the condition of overflow occurs when REAR = n is called “Simple Queue”. In such a queue the
memory is not used effectively.
CIRCULAR QUEUE :
In simple queue there is one problem/drawback related to the overflow of queue. When REAR
reaches to the last location of queue and if we delete first two elements in queue, even though there are
empty locations, we will get message, as “Queue is full”. This means that whenever REAR reaches at the
last location and there are some empty locations it is not possible for us to insert new element in queue.
To overcome this problem concept of circular queue is introduced. In circular queue when
FRONT & REAR reaches at the last location of queue, we will transfer them at starting location.
In simple queue the assumption is that the location at which FRONT is pointing the element at that
location is deleted whereas in circular queue assumption that the location to which FRONT is pointing
the element at that location will delete.
4) Exit
3) Exit
4) Exit
3) Exit
1) [ Is Queue is Empty ? ]
If FRONT = -1 && REAR = - 1 then
Print “Circular Queue is Empty” and Exit
3) Exit
In linear queue insertion & deletion operation is performed at a specific end i.e. insertion takes
place from rear end and deletion takes place from front end.
There are some situations in which we want to perform insertion or deletion operation from both
the ends but in restricted manner. A queue which allows such operation is known as double ended queue (
Dqueue ) the insertion & deletion operation can be performed from both he ends. There are two different
types of Dqueue
a) Input Restricted Dqueue :
In input restricted Dqueue the insertion take place from one-end only but
deletion take place from both ends.
PRIORITY QUEUE :
A priority queue is collection of elements such that each element has been assigned a priority.
The order in which the elements are deleted or process comes from following rules :
The priority queue is represented with help of “Link List” is computer’s memory. The structure
array is not suitable to represent a priority queue.
The array representation of a priority queue is more time-efficient than the one-way list. This is
because when adding an element to a one-way list, one must perform a linear search on the list. On the
other hand, the one-way list representation of the priority queue may be more space-efficient than the
array representation. This is because in using the array representation, overflow occurs when the number
of elements in any single priority level exceeds the capacity for that level, but in using the one-way list,
overflow occurs only when the total number of elements exceeds the total capacity. Another alternative is
to use a linked list for each priority level.
LINK LIST :
“Link List” is non-sequential data structure because elements of link list are not store in sequence
like array. It is also known as Dynamic data structure.
Definition :
“Link List” is collection of NODES where each node is divided into two parts. The first
part is called INFO, which contains actual information or data values, and second part is LINK, which
contains address of next node.
The address of first node is given by a pointer variable “START” and the link part of each node
gives the address of next node. The link part of last node contains null value, which indicates end of link
list.
Traversing a link list is processing the information part of a node until a last node is
encountered. To traverse a link list the variable PTR is used. Initially PTR is initialized to START and
then INFO [ PTR ] is process. The value of PTR is updated using PTR = link [ PTR ].
TRAVERSE : This algorithm traverse a link list and print information part of each node.
1) [ Initialize PTR ]
PTR = START
3) [ Apply process ]
ITEM = INFO [ PTR ]
Print ITEM
4) [ Update PTR ]
PTR = LINK [ PTR ]
5) Exit
Searching a link list means finding the location of a node where given information is present. If
given information is present in a link list then we will get the address of a node and we say that the
searching is successful but when given item is not present in a link list then we say that searching is
unsuccessful.
There are two types of searching algorithms. In first algorithm it is assume that link list is
unsorted and in second algorithm the link list is sorted.
Following algorithm is used to search ITEM in an unsorted list.
SEARCH : This algorithm find location Loc of the node where given ITEM is present. If ITEM is
not present then LOC = NULL.
1) [ Initialize PTR ]
PTR = STRAT
2) Repeat step 3
While PTR != NULL
3) [ Search ITEM ]
If ITEM = INFO [ PTR ]
set LOC = PTR and Exit
Else
PTR = LINK [ PTR ]
The maintenance of link list in memory assumes the possibility of inserting new nodes in a
link list. It also provides some mechanism where the deleted nodes are stored. Together with a link list,
computer maintains a special type of list, which consists of unused memory locations. This list has its own
pointer called “AVAIL” and it is called “Garbage collection” or “Avail List” or “Free Storage”.
Suppose, our link list is maintained in memory by two parallel array and insert, delete
operations are to be performing on our link list. These operations are possible with AVAIL list only. To
perform the operation of insertion, the firs node of AVAIL list can be use. Similarly after deleting a
particular node it is stored as first node of AVAIL list.
In this algorithms INFO and LINK are linear array, which stores the information and the link
part of each node. While START & AVAIL are the pointer variables to indicate the starting
address of field and empty list and ITEM is those elements that is to be insert.
INFIRST : This algorithm insert a given item as a first node in a link list
1) [ Is overflow ? ]
If AVAIL = NULL then
Print “OVERFLOW” & Exit
6) Exit
INLAST : This algorithm insert a given item as a last node in a link list
1) [ Is Overflow ? ]
If AVAIL = NULL
Print “Overflow” and Exit.
5) Exit
C) Write algorithm to insert node between any two given nodes.
Suppose, we are given value of LOC where LOC is address of a node in the link list. If LOC !=
NULL, insert node as first node, but if LOC != NULL, insert node after location.
INLOC : This algorithm insert a item so that it is followed by LOC. If LOC = NULL then
ITEM is inserted as first node.
1) [ Is Overflow ? ]
If AVAIL = NULL
Print “OVERFLOW” and Exit
3) [ Insert ITEM ]
INFO [ NEW ] = ITEM
5) Exit
We can delete a node from link list. The node, which we want to delete, may be first node,
last node or a node after given location. Therefore, we have 3 algorithms. Following algorithm delete a
node
A) Delete first node
B) Delete Last node
C) Delete node between any two nodes
In this algorithms INFO and LINK are linear array, which stores the information and the link
part of each node. While START & END is the pointer variables to indicates the starting and ending
addresses of fields.
1) [ Is List Empty ? ]
If START = NULL then
Print “ List is Empty.” & Exit
4) Exit
1) [ If list is empty ? ]
If START = NULL then
Print “List is Empty” & Exit
4) Exit
DELMID : This algorithm delete the middle node from link list.
1) [ Is list is empty ? ]
If START = NULL then
Print :List is Empty.” & Exit
2) Set TEMP = START
1) [ Is list is empty ? ]
If START = NULL then
Print “List is Empty.” & Exit
4) Exit
A Header link list is a list, which always contains a special node known as “Header Node” at
the beginning of link list. This header node is use to store some specific information about a link list. The
actual information or data values are not stored in the info part of header node and therefore the node,
which is followed, by header node is the first node in header link list. The pointer variable “START”
contains the address of header node and hence the “PTR” is not initialize to “START” but it is initialize
as PTR = LINK [ START ] so that it points to first node.
There are two types of widely used header link list
1) Grounded Header link list
2) Circular Header link list
In simple link list any node contains only the address of next node. Therefore we can traverse a
list only in one direction and hence it is called “One Way List” or “Singly Link List”. A doubly link list,
which allows user to traverse in both directions either in forward direction or in reverse direction.
A two-way list is a collection of nodes where each node is divided into 3 parts
2) FORW : This is a pointer field similar to link which contains the address of next node.
3) BACK : It is also a pointer field which contains the address of previous node.
You can represent doubly link list in computer’s memory using three parallel arrays as
INFO [ ], FORW [ ] & BACK [ ]
It also required two pointers “First” and “Last”. The pointer FIRST gives the address of first node
and the pointer LAST gives the address of last node.
If LOC contains address of a particular node then the FORW [ LOC ] gives address of next node
and BACK [ LOC ] gives address of previous node.
The advantage of doubly link list over a single link list is that if the address of particular node is
given, one has immediate access to the next node as well as previous node. All the operations that can be
perform in one-way list can be easily perform in two-way list.
1) POLYNOMIAL EQUATION :
Header link list is frequently used for storing polynomials in computer’s memory. The
header node plays an important part in this representation, since it is need to represent the zero
polynomial. This representation of polynomials will be presented in the context of a specific example.
Let p(x)denote the following polynomial in one variable ( containing four non zero terms ) :
p(x) = 2x8 - 5x7 - 3x2 + 4
In this example, 2, 5 & 3 are the coefficients and 8, 7 & 2 are the exponential of x.
BINARY TREE :
TERMINOLOGY :
The terminology describes family relationship between nodes. Suppose N is any node in a tree
and S1 & S2 are left and right successors of it, then N is called parent ( father ) S1 is called left child ( left
son ) and S2 is called right child ( right son ) & S1, S2 are called siblings ( brothers )
Every node in binary tree except also used to describe a binary tree. A line drawn from a node
to its successor is known as “Edge”, and sequence of consecutive edges is known as “Path”. A terminal
node is known as “Leaf node” and the path ending at leaf node is called “Branch”.
Consider any binary tree T. Each node of T can have maximum two child’s and therefore, one
can show that level n can have at most second nodes.
The tree T is said to be compiler binary tree if all its levels except last one have max number of
nodes and all nodes in last level appears as far as left as possible.
To represent binary tree using link list you requires 3 parallel arrays INFO, LEFT and RIGHT.
In array INFO actual information is stored and the array left is used to stored the address of left child
and array RIGHT is used to stored the address of RIGHT child. Along with this it requires one pointer
variable called “Root” which gives the address of root node. If value of root is NULL, the binary tree is
empty.
You can represent binary tree using sequential data structure. This representation requires only
one array called “TREE”. The elements of a binary tree are stored as follows :
1) The ROOT of binary tree is stored at TREE [1].
2) If node N occupied the position k in an array then its left child is stored at TREE [2*k] & right
child is stored at TREE [(2*k)+1].
The drawback of this representation it requires more number of memory location as computer to the
nodes in a tree. Also operation of insertion and deletion becomes difficult. Therefore, this representation
is not efficient and that’s why it is not used.
There are three standard methods to traverse a binary tree and these are :
1) Pre-ordered method
2) In-ordered method
3) Post-ordered method
1) PRE-ORDERED METHOD :
In this method following steps are used
a) Process the root
b) Traverse the left sub tree in pre-order
c) Traverse the right sub tree in pre-order
2) IN-ORDERED METHOD :
In this method following steps are used
a) Traverse the left sub tree in pre-order
b) Process the node
c) Traverse the right sub tree in in-ordered method
3) POST-ORDERED METHOD :
In this method following steps are used
a) Traverse left sub tree in post order
b) Traverse right sub tree in post order
c) Process the node
BINARY SEARCH TREE [ B.S.T. ] :
It is one of the most important data structure in computer science. This data structure enables us
to search a particular element with average running time
F (n) = O ( log n )
It is enables to inert or deletes an element in a tree. This data structure contrast with following data
structure
1] Sorted Array :
In this data structure one easily search the element with average running time
F (n) = O ( log n )
But operation of insertion and deletion is very expensive
2] Link List :
One can easily perform the operation of insertion and deletion easily to the link list. But to
search the element in a link list you require average running time
Fn = O (n)
Binary search tree contains advantages of sorted array and link list. This means that you can
easily perform the operation of searching and operation of insertion and deletion in a binary search tree (
B.S.T. )
Defination :
Suppose, ‘T’ is a binary tree then it is called binary search tree if each node N of T has following
properties
1) The value of node ‘N’ is greater than every value of in left sub tree
2) The value of ‘N’ is less than every values in right sub tree
B ] Following us algorithm which is used to find the location of given “ITEM” and its parent. The
procedure traverse down the tree-using pointer PTR and the painter SAVE.
FIND ( INFO, LEFT, RIGHT, ROOT, LOC, PAR ) :
This algorithm finds location LOC of given ITEM and location of its parent in PAR
1) [ Is tree empty ? ]
If ROOT = NULL
Set LOC = NULL & PAR = NULL & Exit
5) [ Is ITEM found ? ]
If ITEM = INFO [ PTR ] then
LOC = PTR and
PAR = SAVE and Exit
7) [ Search unsuccessful ]
PTR = NULL
PAR = SAVE
8) STOP
C ] An algorithm to search given ITEM and if ITEM is not present insert it in binary search tree
2) If LOC != NULL
Write “Element is present” and Exit
b) NEW= AVAIL
AVAIL = left [ NEW ]
INFO [ NEW ] = ITEM
LEFT [ NEW] = NULL
RIGHT [ NEW ] =NULL
5) EXIT
Suppose, T is a given binary search tree and ITEM of information is given. There are three
different cases.
1) Node N has no child. Then N is deleted simply replacing location of parent of N with NULL value
2) Suppose, N has exactly one child, then N is deleted by simply replacing location of N in its parent
by address of its child
3) Suppose, N has exactly two child, S(N) in-order successor of N, then N is deleted by first deleting
S(N) using either method 1 or 2 and then replace
1) [ Initialization of CHILD ]
If LEFT[ LOC ] = NULL & RIGHT [ LOC ] = NULL
CHILD = NULL
Else
If LEFT [ LOC ] != NULL
CHILD = LEFT [ LOC ]
Else
CHILD = RIGHT [ LOC ]
2) [ Is node ROOT ? ]
If PAR != NULL then
If LOC = LEFT[ PAR ] then
LEFT [PAR ] = CHILD
Else
RIGHT [ PAR ] = CHILD
Else
ROOT = NULL
4) EXIT
Suppose T is a given binary tree. The terminal nodes in left and right part of this node the null
values are sorted. These NULL values are used to indicate the end of the particular branch. This NULL
value occupies the memory space unnecessary and therefore instead of storing a NULL value we can store
some useful information in pointer field. The pointer field in which useful information is stored is called
threads and tree is called binary threaded tree.
These are two types of binary threaded trees
a) One-Way Threaded Binary Tree :
In one-way threaded binary tree only right part of node used to store the address
of in-order successor