Вы находитесь на странице: 1из 28

 DATA :

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.

 TYPES OF DATA STRUCTURE :


The data structure can be classified into different data types using different criteria’s. Following
are some types of data structure.

 According to Data Organization :


a) Linear Data Structure :
The data structure in which memory locations are allocated in continuous memory
locations is called “Linear data structure”.
E.g. : Array, Stack, Queue etc.

b) Non Linear Data Structure :


The data structure in which the elements are not stored in continuously fashion is
called a “Non linear data structure”. This means that the elements are stored randomly in
computer’s memory.
E.g. : Tree, Graph etc.

 According to Memory Allocation Time Data Structure :


a) Static Data Structure :
The data structure in which memory space is reserve during the process of
compilation and it can not be increased or decreased during the process of execution is called
“Static data structure”.

b) Dynamic Data Structure :


The data structure in which the memory space is allocated at the execution time of a
program is called as “Dynamic data structure”.

 According to Data Types Data Structure :


a) Primitive Data Structure :
The most elementary data types are called as “Primitive data structure”. They are
directly operated by machine language instruction.
The structure used to represent int, char, float etc are primitive data structure.
b) Non Primitive Data Structure :
The data structure that are made up of data types are known as “Non primitive
data structure”. They are not directly operated by machine language instruction.
The data structure used to represent array, structure, file etc are known as non
primitive data structure.

 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

3) [ Examine all elements ]


Repeat step 4 and 5 until
I < =5

4) [ Is MAX less than any?]


If MAX < A [ I ]
MAX = A [ I ]

5) [ Prepare for next ]


I I+1

6) Print MAX

7) STOP

Following are some notations use while writing an algorithm.


1] Name of Algorithm :
Each algorithm must start with name written in capital letters. This name must be
meaningful.

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.

 REPRESENTATION OF AN ARRAY : ( Storage )


The simplest data structure that makes use of computed address method is array. Normally the
elements of an array are store sequentially in computers memory and array size is fixed. If ‘L0’ is starting
address of array and ‘C’ is number of memory location required to store one array element then the
address of jth element is given by

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

Loc( A3 ) = 700 + 2 * ( 3 – 1 ) L0 = 700


= 704 C=2
i=3

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.

 ALGORITHM FOR “PUSH” OPERATION :

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

2) [ Increase value of TOP ]


TOP = TOP + 1

3) [ Insert element ]
STACK [ TOP ] = ITEM

4) STOP

 ALGORITHM FOR “POP” OPERATION :

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

2) [ Delete TOP element ]


ITEM = TOP

3) [ Decrease the TOP ]


TOP = TOP – 1

4) STOP

 ALGORITHM FOR “SHOW” OPERATION :

SHOW : This algorithm display all elements in stack.

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 :

Suppose Q is an expression without parenthesis. When this expression is evaluated, it is evaluated


according to rules of priority. If you want to override the rules of priority parenthesis are used.
In most of the expression operator symbol appears in between two operands. An expression in
which the operator symbol appears in between two operands is called infix expression.

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.

1) Push “(” onto STACK, and add “)” to the end of Q.


2) Scan Q from left to right and repeat Steps 3 to 6 foe each element of
Q until the STACK is empty :
3) If an operand is encountered, add it to P.
4) If a left parenthesis is encountered, push it onto STACK.
5) If an operator (*) is encountered, then :

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 ]

6) If a right parenthesis is encountered, then :

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);
}

int fact (n)


{
int f;
if (n = = 1)
return 1;
else
f = n* Fact (n – 1)
return ( 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.

 ALGORITHM FOR INSERTION OPERATION :

A) INSERT ELEMENT IN SIMLE QUEUE :


This algorithm used to insert an element in queue represented by array “QUEUE” having
number of elements given by variable “MAX”. The variable “REAR” used to insert an elements having
initial value 0 and N is temporary variable.

1) [ check Queue is Full ? ]


If REAR = MAX
Then write “Queue is full” and Exit
2) [ Read element & store it in queue ]
Read N
REAR := REAR + 1
Queue[ REAR ] := N
3) [ Finish ]
Return

B) INSERT ELEMENT IN CIRCULAR QUEUE :

This algorithm insert an element called ITEM in circular queue.

1) [ Is Queue already full ? ]


If FRONT =1 && REAR = N or FRONT = REAR + 1
Then write “Queue is full” and Exit

2) [ Find new value of REAR ]


If FRONT = NULL
FRONT = REAR = 1
Else
If REAR = N then
REAR = 1
Else
REAR = REAR + 1

3) [ Insert the element ]


Queue[ REAR ] = ITEM

4) Exit

 ALGORITHM FOR DELETION OPERATION :

A) DELTE ELEMENT FROM SIMPLE QUEUE :


This algorithm removes an element from queue data structure represented by an array
“QUEUE” having number of elements given by “MAX”. The variable “FRONT” used to remove
element having initial value 0. N is temporary variable.

1) [check queue is Empty ? ]


If FRONT = REAR then
Write “Queue is empty.” And Exit

2) [ Remove element from queue ]


FRONT = FRONT + 1
Queue[ FRONT ] = N
Write N

3) Exit

B) DELETE ELEMENT FROM CIRCULAR QUEUE :

This algorithm delete an element from circular queue.

1) [ Is queue already empty? ]


If FRONT =0 then
Write “UNDERFLOW” and Exit

2) [ Delete the element ]


ITEM = Queue[ FRONT ]

3) [ Find new value of Front ]


If FRONT = REAR then
FRONT = REAR = 0
Else
If FRONT = N then
FRONT = 1
Else
FRONT = FRONT + 1

4) Exit

 ALGORITHM FOR SHOW OPERATION :

A) SHOW ELEMENTS IN SIMPLE QUEUE :

This algorithm displays all elements in simple Queue.


1) [ check Queue is Empty ? ]
If FRONT = REAR then
Print “Queue is Empty.” And Exit.

2) [ Print all elements in queue ]


Repeat for I = FRONT to REAR
Print Queue[ I ]
[ End of for loop structure ]

3) Exit

B) SHOW ALL ELEMENTS IN CIRCULAR QUEUE :

This algorithm displays all elements in circular Queue.

1) [ Is Queue is Empty ? ]
If FRONT = -1 && REAR = - 1 then
Print “Circular Queue is Empty” and Exit

2) [ Print all elements in circular queue ]


If REAR > FRONT then
Repeat For I = FRONT to REAR
Print Queue[ I ]
[ End of for loop structure ]
Else
Repeat For I = FRONT to MAX
Print Queue[ I ]
[ End of for loop structure ]
Repeat For I = 0 to REAR
Print queue[ I ]
[ End of for loop structure ]

3) Exit

 DOUBLE ENDED QUEUE : [ DQueue ]

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.

b) Output Restricted Dqueue :


In output restricted Dqueue the deletion takes place from one end only but
insertion takes 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 :

i) An element of higher priority is process before any element of lower priority.


ii) Two elements with some priority are process according to the order in which they inserted in
queue.

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.

 REPRESENTATION OF LINK LIST IN COMPUTER’S MEMORY :


To represent a link list in computer’s memory you required two linear array INFO and LINK
3which are parallel to each other. The values and the array LINK contains address of next node. You also
required two pointer variables “START” and “PTR”. The pointer variable “START” gives the address of
first node and pointer variable “PTR” gives the address of currently processing node.

 TRAVERSING A 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 ].

Write an Algorithm to Display Information Part of a Link List :

TRAVERSE : This algorithm traverse a link list and print information part of each node.

1) [ Initialize PTR ]
PTR = START

2) [ Repeat steps 3 and 4 ]


while PTR ! = NULL

3) [ Apply process ]
ITEM = INFO [ PTR ]
Print ITEM

4) [ Update PTR ]
PTR = LINK [ PTR ]

5) Exit

 SEARCHING A LINK LIST :

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 ]

4) Set LOC = NULL


5) Exit

 GARBAGE COLLECTION ( AVAIL LIST ) :

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.

 INSERTION INTO A LINK LIST :

There are 3 different algorithms are insert a node in link list.


A) A node is inserted as a first node in a list.
B) A node is inserted as a last node in a list.
C) Node is inserted between any two given nodes.

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.

A) Write algorithm to insert a first node in a link list

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

2) [ Remove first node from AVAIL list ]


set TEMP := AVAIL & AVAIL := NEXT [ AVAIL ]

3) Set INFO [ TEMP ] := ITEM


[ Copies new data into new node ]

4) Set NEXT [ TEMP ] := START


[ new node now points to original first node ]

5) Set START := TEMP


[ changes START so it points to new node ]

6) Exit

B) Write algorithm to insert a node as last node in a link list

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.

2) [ Remove first node from avail ]


NEW = AVAIL
AVAIL = LINK [ AVAIL ]

3) [ Insert ITEM in node ]


INFO [ NEW ] = ITEM

4) [ Insert the node as last node ]


PTR = START
While PTR != NULL
SAVE = PTR
PTR = LINK [ PTR ]
LINK [ SAVE ] = NEW
LINK [ NEW ] = NULL

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

2) [ Remove first node from AVAIL ]


NEW = AVAIL
AVAIL = LINK [ AVAIL ]

3) [ Insert ITEM ]
INFO [ NEW ] = ITEM

4) [ Insert node in a List ]


If LOC = NULL
START = NEW
Else
LINK [ NEW ] = LINK [ LOC ]
LINK [ LOC ] = NEW

5) Exit

 DELETION FROM LINK LIST :

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.

A) ALGORITHM FOR DELETE FIRST NODE :


DELFIRST : This algorithm delete the first node from a link list.

1) [ Is List Empty ? ]
If START = NULL then
Print “ List is Empty.” & Exit

2) [ Only one node present ]


Print INFO [ START ]
START = END = NULL

3) [ More than two nodes are presents ]


Print INFO [ START ]
START = NEXT [ START ]

4) Exit

B) ALGORITHM FOR DELETE LAST NODE :

DELLAST : This algorithm delete last node from link list.

1) [ If list is empty ? ]
If START = NULL then
Print “List is Empty” & Exit

2) Set TEMP = START

3) [ Delete Last node ]


Print value of INFO [ END ]
Repeat for I = 1 to ( LOC – 1 )
TEMP = NEXT [ TEMP ]
NEXT [ TEMP ] = NULL
END = TEMP

4) Exit

C) ALGORITHM FOR DELETE MIDDLE NODE :

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

3) [ Delete middle node ]


Repeat for I = 1 to ( LOC – 1 )
TEMP = NEXT [TEMP ]
Print value of INFO [ NEXT] [ TEMP ]
TEMP = NEXT [ TEMP ] [TEMP ]
4) Exit

 ALGORITHM FOR SHOW ELMENTS IN A LINK LIST :

SHOW ( START, INFO, NEXT, MOVE )


This algorithm shows element of a link list.

1) [ Is list is empty ? ]
If START = NULL then
Print “List is Empty.” & Exit

2) Set TEMP = START

3) Repeat while NEXT [ TEMP ] != NULL [ Traverse list up to last node ]


a) Print INFO [ TEMP ] [ Print INFO part of a list ]
b) Set TEMP = NEXT [ TEMP ] [ Point move to next node ]

3) Print INFO [ TEMP ]

4) Exit

 HEADER LINK LIST :

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

1) Grounded Header Link List :


A fronded header link list is a list where the link list part of the last node contains NULL
pointer and it is represented using electrical ground symbol.
2) Circular Header Link List :
It is a list in which the link part of last node points back to the header node. In this link list
the NULL pointer is not present. It is called circular because the header node appears after the last
node a list.

 DOUBLY 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

1) INFO : This part is used to stored actual information or data values.

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.

APPLICATION OF LINK 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 :

A binary tree T is defined as a finite set of nodes such that


1) T is empty.
2) T contains a disjoint node R called root of T and the remaining nodes of forms ordered pair of
disjoint binary trees T1 and T2.
If T contains root R then two trees T1 and T2 are called left and right sub trees. If T1 is not empty
then its root is called left successor of R. Similarly if T2 is not empty then its root is called right successor
of R.

 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”.

 COMPLETE BINARY TREE :

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.

 REPRESENTATION OF BINARY TREE USING LINK LIST :

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.

 REPRESENTATION OF BINARY TREE USING ARRAY :

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.

 TRAVERSING A BINARY TREE :

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

 SEARCHING AND INSERTING IN A BINARY TREE :

A ] Following steps are used to search given ITEM in B.S.T.

1) Compare ITEM with root


a) If ITEM < root, proceed to the left sub tree
b) If ITEM > root, proceed to the right sub tree
2) Repeat following steps, until on of the following conditions occurs
a) we meet a node N such that
ITEM = N
Searching is successful
b) we get NULL value in a pointer
ITEM = NULL
Searching is unsuccessful

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

2) [Is ITEM root ? ]


If ITEM = INFO [ ROOT ]
LOC = ROOT & PAR = NULL

3) [ Initialize the pointer ]


If ITEM < INFO [ ROOT ] then
PTR = left [ ROOT ]
SAVE = ROOT
Else
PTR = right [ ROOT ]
SAVE = ROOT

4) Repeat steps (5) & (6)


Till PTR != NULL

5) [ Is ITEM found ? ]
If ITEM = INFO [ PTR ] then
LOC = PTR and
PAR = SAVE and Exit

6) [ Update PTR & SAVE ]


If ITEM < INFO [ PTR ] then
SAVE = PTR and PTR = left [ PTR]
Else
SAVE = PTR
PTR = right [ PTR ]

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

INSERT ( INFO, LEFT, RIGHT, PAR, LOC, ROOT, AVAIL )


1) Call FIND( )

2) If LOC != NULL
Write “Element is present” and Exit

3) [ Copy ITEM into new node ]


a) if AVAIL = NULL then
write “OVERFLOW” & Exit

b) NEW= AVAIL
AVAIL = left [ NEW ]
INFO [ NEW ] = ITEM
LEFT [ NEW] = NULL
RIGHT [ NEW ] =NULL

4) [ Add new node ]


If INFO [ PAR ] > ITEM
LEFT [ PAR ] = NEW
Else
RIGHT [ PAR ] = NEW

5) EXIT

 DELETING A NODE FROM BINARY SEARCH TREE :

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

Following algorithm deletes node N having no child or only one child

DELETE ( LEFT, RIGHT, INFO, PAR, LOC, ROOT)


This algorithm deletes node N. The pointer PAR gives location of parent node. If PAR = NULL, N
is a ROOT. The pointer variable “CHILD” gives the location of CHILD ( N ). If N is terminal
node CHILD = NULL

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

 THREADES BINARY TREE :

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

b) Two-Way Threaded Binary Tree :


In two-way threaded binary tree in which right part of node is used to store the
address of in-order successor and left part is used to stored address of in-order
predecessor.

Вам также может понравиться