0% found this document useful (0 votes)
5 views77 pages

Datastructurescomplete

The document provides an introduction to data, entities, information, and data structures, emphasizing the importance of organizing data for program efficiency. It classifies data structures into primitive and non-primitive types, outlines operations that can be performed on them, and discusses abstract data types and algorithms. Additionally, it covers algorithm complexity, time-space trade-offs, and string processing in C programming.

Uploaded by

lathankp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views77 pages

Datastructurescomplete

The document provides an introduction to data, entities, information, and data structures, emphasizing the importance of organizing data for program efficiency. It classifies data structures into primitive and non-primitive types, outlines operations that can be performed on them, and discusses abstract data types and algorithms. Additionally, it covers algorithm complexity, time-space trade-offs, and string processing in C programming.

Uploaded by

lathankp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 77

Chapter-1

INTRODUCTION
Data is the basic entityor fact that is used incalculationor manipulationprocess. There are
two types of data such as numerical and alphanumerical data. Integer and floating-point
numbers are of numericaldatatype and strings are ofalphanumeric data type. Data may be
single or a set of values and it is to be organized in a particularfashion. This organizationor
structuring of data will have profound impact on the efficiency ofthe program.
DATA:
Dataisvalueorsetofvalueswhichdoesnotgive anymeaning.It isgenerallyaraw fact. For
example:
1.34 3.Chintan

2.13/05/2008 4.12,34,43,21

ENTITY
Anentityisathingorobjectintherealworldthatisdistinguishablefromallother objects.
 The entity has a set of properties or attributes and the values of some sets of these attributes
may uniquely identify an entity.
 Anentitysetisacollectionofentities.
Example:
Entity Student

Attribu Roll Name DOB % of marks


tes No. scored

Values 123 Ram 01/01/1 79%


980

Allstudentsofa particularclassconstituteanentity set.

INFORMATION
It canbe defined as meaningfuldataorprocessed data. Whenthe raw facts are processed, we get
a related piece of information as its output/result.

Example:
Data(01/01/1980)becomesinformationifentityRamisrelatedtoDateofbirthattribute
(01/01/1980) as follows:
DOBofstudentRamis 01/01/1980
DATATYPE
A data type is a term which refers to the kind of data that may appear in computation. Every
programming language has its own set of built-in data types.
Example:
Data Datatype

34 Numeric
Chintech String
21,43,56 Arrayofintegers
12/05/2008 Date

InC,thefollowingarethebasicdatatypesare:int, long, char, float,double, voidetc.

Definition:
Data Structure is a specialized format for storing data so that the data’s can be organized in
an efficient way.
Or

“Thedatastructureisthelogicalormathematicalmodelofparticularorganizationof data.”.
Or
A data structure is a mathematical or logical way of organizing data in the memory that
consider not only the items stored but also the relationship to each other and also it is
characterized by accessing functions.

DataStructure=Organizeddata+AllowedOperations.

OBJECTIVESOFDATASTRUCTURE

 To identify and create useful mathematical entities and operations to determine what classes
of problems can be solved by using these entities and operations.
 Todeterminetherepresentationoftheseabstractentitiesandtoimplementtheabstract operations on
these concrete representation.
NEEDOFADATASTRUCTURE

 To understand the relationship ofone data elements with the other and organize it within the
memory.
 Adatastructureshelpstoanalyzethedata,storeitandorganizeitinalogicalor mathematical manner.
 Datastructuresallowustoachieveanimportantgoal:componentreuse.
CLASSIFICATIONOFDATASTRUCTURE
Basedonhowthedataitemsareoperated,itwillclassifyintotwo broad categories.

 PrimitiveDataStructure
 Non-PrimitiveDataStructure

PrimitiveDataStructure: ThesearebasicDSandthedataitemsareoperatedclosesttothe machine


level instruction.
Example: integer,characters,strings,pointersanddouble.

Non-Primitive Data Structure: These are more sophisticated DS and are derived from primitive
DS. Here data items are not operated closest to machine level instruction. It
emphasizeonstructuringofagroupofhomogeneous(sametype) orheterogeneous(different type)
data items.

LinearDataStructure:Inwhichthedataitemsarestored insequenceorder.
Example:Arrays,LinkedLists,StacksandQueues

NonLinearDataStructure:InNon-Lineardatastructures,thedata itemsarenotin sequence.


Example:Trees,Graphs.

DATASTRUCTURE OPERATIONS:
Thevariousoperationsthat canbeperformedondifferent datastructuresaredescribed below:

1. Creating–Adatastructure createdfromdata.
2. Traversal–Processingeachelementinthelist
3. Search–Finding thelocationofgivenelement.
4. Insertion –Addinganewelementtothelist.
5. Deletion–Removinganelementfromthelist.
6. Sorting–Arrangingtherecordseitherinascendingordescendingorder.
7. Merging–Combiningtwolistsintoasinglelist.
8. Modifying–thevaluesofDScanbemodifiedby replacing oldvalueswithnewones.
9. Copying–recordsofonefilecanbe copied toanotherfile.
10. Concatenating –Recordsofafile are appendedat the endofanotherfile.
11. Splitting–Recordsofbigfilecanbesplittingintosmallerfiles.

ABSTRACTDATATYPES
Abstract data type (ADT) is a specification of a set of data and the set of operations that can be
performed on the data.
 Itisalogical descriptionofhowweviewthedataandtheoperationsthatareallowed without
knowing how they will be implemented.
 This meansthat weareconcernedonlywithwhat thedata isrepresentingand not withhow it will
eventually be constructed.

Figshowsapictureofwhatanabstractdatatypeisandhowit operates.
 Theuserinteractswiththeinterfacebyusingthe operations.
 Theuserisnotconcerned withthedetailsoftheimplementation.
 Theimplementationofanabstract datatype,oftenreferred toasadata structure
 An ADT is a data declaration packaged together with the operations that are meaningful
onthe data type.
1. DeclarationofData
2. DeclarationofOperations
Examples: Objects such as lists, sets and graphs with their operations can be called as ADT. For
the SET ADT, we might have operations such as union, intersection, and complementetc.

UsesofADT:
1. Ithelpstoefficientlydevelopwelldesignedprogram
2. Facilitatesthedecompositionofthecomplextaskofdevelopingasoftwaresystemintoa number of
simpler subtasks
3. Helpsto reducethenumberofthingstheprogrammershasto keep inmind atanytime.
4. Breaking down a complex task into a number of earlier subtasks also simplifies testing
anddebugging.
ALGORITHM:
Analgorithm isa methodofrepresentingthestepbysteplogicalprocedureforsolvinga problem. It is a
tool for finding the logic of a problem.
Inadditioneveryalgorithmmustsatisfythefollowing criteria:

 Input:Therearezeroormorequantitieswhichareexternallysupplied. i.e.eachalgorithm must


take zero, one or more quantities as input data.
 Output:Atleastonequantityisproduced.i.e.produceoneor moreoutputvalues.
 Definiteness:Each instructionorstepofanalgorithmmustbeclearand unambiguous.
 Finiteness:Analgorithmmustterminateinafinitenumber ofsteps.
 Effectiveness:Eachstepmust beeffective, inthesensethat it shouldbeprimitive(easily
convertible to program).

ALGORITHM COMPLEXITY:
 After designing an algorithm, we have to be checking its correctness. This is done by
analyzing the algorithm. The algorithm can be analyzed by tracing all step-by-step
instructions,readingthealgorithmfor logicalcorrectness,andtestingit onsomedatausing
mathematical techniques to prove it correct.
 There maybe morethanonealgorithmtosolveaproblem.Thechoiceof aparticular
algorithm depends on its complexity.
 Complexityreferstotherateat whichtherequiredstorageorconsumedtimegrowsasa
function of the problem size.
 Itistheperformanceevaluationoranalysis/ measurementofanalgorithmisobtained by
totaling the number of occurrences of each operation when running the algorithm.
 Twoimportant waysto characterizetheeffectivenessofanalgorithmare itsspace
complexity and time complexity.
 SpaceComplexity
 TimeComplexity

SPACECOMPLEXITY:
 Spacecomplexityofanalgorithmorprogramistheamountofmemoryitneedsto runto
completion.
 Wecanmeasurethespacebyfindingoutthathowmuchmemorywillbeconsumedbythe
instructions and by the variables used.
Example:
Supposewewantto addtwo integernumbers.Tosolvethisproblemwehave following
twoalgorithms:

Algorithm1: Algorithm2:
Step1-InputA. Step1-InputA.
Step2-InputB. Step2-InputB.
Step3-Set C:=A+ B. Step3-Write:‘Sumis‘,A+B.
Step4-Write:‘Sumis‘,C. Step

4- Exit.Step 5- Exit.

Both algorithms will produce the same result. But first takes 6 bytes and second takes 4
bytes(2bytesforeachinteger).Andfirsthasmoreinstructionsthanthesecondone.Sowe will
choose the second one as it takes less space than the first one. (Space Complexity).

Suppose 1 second is required to execute one instruction. So the first algorithm will take 4
secondsandthesecondalgorithm willtake3secondsforexecution.Sowewillchoosethe second
one as it will take less time. (Time Complexity).

TimeComplexity
➢ Thetimecomplexityofaprogram /algorithm istheamount ofcomputertimethatisneed to run
to completion.
➢ Butthecalculationofexactamountoftimerequiredbythealgorithm isverydifficult.Sowe can
estimate the time and to estimate the time we use some asymptotic notation, which are
described below. Let the no. of steps of an algorithm is measured by the function f(n).

1. Big‘O’notation

Thegivenfunctionf(n)can beexpressedbybig‘O’notationas follows.f(n)= O(g(n))ifand only if


there exist the +ve const. ‘C’ and no such that f(n) ≤ C * g(n) for all n ≥ no.

2. Omega(Ω)notation
Thefunctionf(n)=Ω(g(n))ifandonlyifthereexistthe+veconstCand nosuch thatf(n)≥C
* g(n)foralln≥ no

3. Theta(θ)notation:
Thefunctionf(n)=θ(g(n))if andonlyifthereexist3+veconst‘C1’,‘C2’andnosuchthatC1
* g(n)≤f(n) ≤C2*g(n)

Big‘O’isthe upperboundΩisthe lowerboundθ istheavgboundwhichcanbeestimated in time


complexity.

SpaceComplexity
Thespacecomplexityistheprogramthattheamountofmemorythatis neededtorunto
completion.The spacecomplexityneedbya programhavetwodifferentComponents.

1. Fixedspacerequirement: -Thecomponentsrefertospacerequirementthatdonotdepend upon


the number and size of the programs input and output.
2. Variable space requirement: - This component consist of the space needed by structure
variablewhosesizedependsontheparticularinstructionI,oftheprogrambeginsolved.

TIME-SPACETRADE-OFF:

 There maybemorethanoneapproach(oralgorithm) tosolvea problem.


 Thebest algorithm(orprogram)to solveagivenproblemisonethat requireslessspace in
memory and takes less time to complete its execution.
 Butinpractice,itisnotalwayspossibletoachieve bothoftheseobjectives.
 Onealgorithmmayrequire morespacebut lesstimeto complete itsexecutionwhiletheother
algorithm requires less time space but takes more time to complete its execution.
 Thus,wemayhavetosacrificeoneatthecostofthe other.
 Ifthespace isourconstraint,thenwehaveto chooseaprogramthat requireslessspaceat the cost
of more execution time.
 Ontheotherhand, iftime isourconstraint suchas inrealtimesystem, we havetochoosea
program that takes less time to complete its execution at the cost of more space.
Chapter-2
STRINGPROCESSING
AstringinCisaarrayofcharacter.
 It isaonedimensionalarraytypeofchar.
 Everystringisterminatedbynullcharacter(‘\0’).
 Thepredefinedfunctions gets() andputs() inC languagetoreadanddisplaystring
respectively.

Declarationof strings:
Stringsare declared in Cin similarmanneras arrays. Only difference isthat,stringsare ofchar type.

Syntax:
charstr_name[str_size];
Example:
chars[5];

Initializationofstrings
charc[]="abcd";

Stringhandlingfunctions
 Manylibraryfunctionaredefinedunderheaderfile<string.h>toperformdifferenttasks.
 Diffrentuserdefinedfunctiosare:
 Strlen()
 Strcpy()
 Strcmp()
 Strcat()

Strlen():
 Thestrlen( )functionisusedtocalculatethelengthof thestring.
 Itmeansthatitcountsthetotalnumber ofcharacterspresentinthestringwhich
includes alphabets, numbers, and all special characters includingblank spaces.
Example:
charstr[]="LearnCOnline"; int
strLength;
strLength= strlen(str); //strLengthcontainsthelengthofthestringi.e.14
Strcpy()
• strcpyfunctioncopies a stringfroma sourcelocationtoadestinationlocationand
provides a null character to terminate the string.
Syntax:
strcpy(Destination_String,Source_String);
Example:
char*Destination_String;
char*Source_String="LearnCOnline";
strcpy(Destination_String,Source_String);
printf("%s", Destination_String);
Output:
LearnC Online

Strcmp()

• Strcmp()inC programminglanguageisusedtocomparetwostrings.
• Ifboththestrings areequalthenit gives theresult aszerobutifnot thenit givesthe
numeric difference between the first non matching characters in the strings .
Syntax:
intstrcmp(string1,string2);
Example:
char*string1="LearnCOnline"; char
*string2 = "Learn C Online";int ret;
ret=strcmp(string1, string2);
printf("%d",ret);
Output:
0
Strcat()

• Thestrcat()functionisusedfor stringconcatenationinCprogramminglanguage. Itmeans it


joins the two strings together
Syntax:
strncat(Destination_String,Source_String,no_of_characters);
Example:
char*Destination_String="Visit";
char*Source_String="LearnCOnlineisagreatsite"; strncat(Destination_String,
Source_String,14);
puts(Destination_String);
Output:
VisitLearnCOnline

Programto checkwhetherawordispalindrome or not


#include<stdio.h>
#include<string.h>voi
d main()
{
chars1[10],s2[10];
int x;
gets(s1);
strcpy(s2,s1);
strrev(s1);
x=strcmp(s1,s2);
if(x==0)
printf("pallindrome");
else
printf("not pallindrome");
getch();
}
Chapter-
3ARRAYS
Anarrayisa finite,orderedandcollectionofhomogeneousdataelements. finite – It

contain limited no. of element.

ordered–Alltheelementsarestoredonebyone in acontiguousmemorylocation.

homogeneous -All the elements of an array of same type.

Theelementsofanarrayareaccessedbymeansofanindexor subscript. That’s why

array is called subscripted variable.

LINEARARRAY
Ifone subscript is required to refer allthe elements in an arraythen this is called Linear arrayor
one-dimensional array.

Representation ofLinearArray inmemory

Letaisthenameofanintegerarray.
Itcontainsasequenceofmemorylocation.

Let b=addressofthe1stelement inthearrayi.e. baseaddressIfw=wordsize then

address of any element in array can be obtained as address of

(a[i]) = b + i x w i=indexno.addressof

a[3] = b + i x w

=b+3x2=b+6
OperationonArray
1. TRAVERSAL

Thisoperation isused to visitalltheelementsofthearray.

Voidtraverse(inta[],intn)
{
inti;
for(i=0;i<n;i++)
Printf(“%d”,a[i]);
}
2. INSERTION

Insertingthearrayattheendpositioncanbedone easily, buttoinsert atthemiddleofthe arraywe


have to move the element to create a vacant position to insert the new element. int insert
(int a[], int n, int pos, int ele)
{
inti;
for(i=n-1;i>=pos-1;i--)
{
a[i+1]=a[i];
}
a[pos-1]=ele; n++;
return(n);
}

3. DELETION

Deleting an element at the end of the array can be done easily by only decreasing the array size
by 1.

But deleting anelement at the middle ofthe arrayrequire that eachsubsequent element fromthe
position where to be deleted should be moved to fill up the array.

intdelete(int a[],int n,int pos)


{
for (i= pos-1;i<n-1;i++)
{
a[i]=a[i+1];

} n--;
return(0);
}

MemoryRepresentationof2DArray
- Thearrayhavingtwosubscriptiscalledas2Darray.
- In2Darraytheelementsarestoredincontiguousmemorylocationasinsingle dimensional
array.
- Thereare2waysofstoring anymatrix in memory.
1. Row-majororder
2. Column-majororder

Row-majorOrder
Inrow-majorordertherowelementsarefocused firstthat meanselementsofmatrixare stored on
a row-by-row basis.
LogicalRepresentationofarraya[3][3]

Rowmajor orderrepresentationofa[3][3]

StorageRepresentation inRow-majororder
Column-majorOrder
In column major order the column elements are focused first that means elements of the matrix
are stored in column-by-column basis.
Ex:Columnmajororderrepresentationofa[3][3]

StorageRepresentationofmatrixinColumn-majorOrder
AddressCalculationofMatrixinMemory

Row-majororder

Supposea[u1][u2]isa2Darrayu1=no.ofrow u2 = no.

of column

Addressofa[i][j]=b+(ixu2+ j)xw

Column-major order

Addressofa[i][j]= b+ (jxu1+i)xw

Pointers
ThepointerinClanguageisavariablewhichstorestheaddressofanothervariable.This variable can be
of type int, char, array, function, or any other pointer.

Syntax:datatype*var_name;

Example:

1. int*a;//pointerto int

2. char*c;//pointertochar

PointerExample

Anexampleofusingpointerstoprint theaddressand valueisgiven below.


Inthe above figure, pointer variable storesthe addressofnumber variable, i.e.,fff4. The value of
number variable is 50. But the address of pointer variable p is aaa3.

Bythehelpof*(indirectionoperator), wecanprint thevalueofpointervariablep. Let's see

the pointer example as explained for the above figure.


#include<stdio.h>
intmain(){
intnumber=50;
int*p;
p=&number;//storestheaddressofnumbervariable
printf("Address of p variable is %x \n",p);
printf("Value of p variable is %d \n",*p);
return0;
}
Output
Addressofnumbervariableisfff4
Address of p variable is fff4Value
of p variable is 50

Sparsematrices
Matrix with relatively a high proportion of zero entries are called sparse matrix. In other words,
thesparse matrixcanbedefinedasthe matrixthathasagreaternumber ofzero elementsthanthe non-
zero elements.
Benefitsofusing thesparsematrix
i) Storage -We know that a sparse matrix contains lesser non-zero elements than zero, so less
memory can be used to store elements. It evaluates only the non-zero elements.
ii) Computing time: In the case of searching in sparse matrix, we need to traverse only the non-
zero elements rather than traversing all the sparse matrix elements. It saves computing time by
logically designing a data structure traversing non-zero elements.
Representationofsparsematrix
Therearetwowaystorepresentthesparsematrixthatarelistedasfollows -
o Arrayrepresentation
o Linkedlist representation

Array representationofthesparsematrix
Representing a sparse matrix by a 2D array leads to the wastage of lots of memory. This is
becausezeroesinthematrixareofnouse,sostoringzeroeswithnon-zeroelementsiswastage
of memory. To avoid such wastage, we can store only non-zero elements. If we store only non-
zero elements, it reduces the traversal time and the storage space.
2Darrayisused torepresentasparsematrix inwhichtherearethreerowsnamed as

 Row: Indexofrow, wherenon-zeroelementislocated


 Column:Indexofcolumn, wherenon-zeroelementis located
 Value:Valueofthenonzeroelementlocatedatindex–(row,column)

LinkedListrepresentationofthesparse matrix

Ina linked list representation, the linked list data structure isused to represent the sparse matrix.
The advantage of using a linked list to represent the sparse matrix is that the complexity of
inserting or deleting a node in a linked list is lesser than the array.

Inlinked list,eachnodehasfourfields.These fourfieldsaredefinedas:


 Row:Indexofrow,wherenon-zeroelementislocated
 Column:Indexofcolumn, wherenon-zeroelementis located
 Value:Valueofthenonzeroelementlocatedatindex–(row,column)
 Nextnode: Addressofthenextnode
Chapter-4
STACKS&QUEUES
 Stack is a linear data structure inwhichanelement may be inserted or deleted at one end
called TOP of the stack.

 Thatmeans the elements are removedfrom a stack in the reverse order of that in which
they were inserted into the stack.
 StackiscalledLIFO(Last-in-first-Out)Str.i.e. theitemjustinsertedisdeletedfirst.
 Thereare2basic operationsassociatedwithstack
1. Push:-Thisoperationisusedtoinsertanelementintostack.
2. Pop:-Thisoperation isusedto deleteanelement fromstack

WorkingofStackDataStructure

Conditionalsoarise:
1. Overflow:-Whenastackisfullandweareattemptingapushoperation,overflow condition
arises.
2. Underflow:-Whenastackisempty,andweareattemptingapopoperationthen underflow
condition arises.
RepresentationofStackinmemory

Astackmayberepresentedinthememory intwoways:
1. Usingonedimensionalarrayi.e.ArrayrepresentationofStack.
2. Usingsinglelinkedlisti.e.Linkedlist representationofstack.
ArrayRepresentation ofStack :
Toimplement astackinmemory,weneedapointervariablecalledTOPthat holdthe indexof the top
element ofthe stack, a linear arrayto hold the elements ofthe stack and a variable MAXSTK
which contain the size of the stack.

MAXSTK=5

LinkedList Representation ofStack:

 ArrayrepresentationofStackisveryeasyandconvenient but it allowsonlytorepresent fixed


sized stack.

 But inseveralapplicationsizeofthestackmayveryduringprogramexecution, atthat cases


we represent a stack using linked list.

 SinglelinkedlistissufficienttorepresentanyStack.

 Herethe‘info’fieldfortheitemand‘next’
field is used to print the next item.
INSERTIONINSTACK(PUSHOPERATION)

Thisoperationisusedtoinsert anelement instackattheTOP ofthestack. Algorithm :-

PUSH(STACK,TOP,MAXSTK, ITEM)
Thisprocedurepushesanitemintostack.
1. IfTOP =0theprintUnderflowand Return.
2. SetITEM=STACK[TOP](AssignsTOPelementtoITEM)
3. SetTOP=TOP-1(DecreasesTOP by1)
4. Return

( WhereStack=Stackisalistoflinear structure
TOP=pointervariablewhichcontainnthe locationofTopelementofthestack MAXSTK=
variable which contain size of the stack

ITEM=Itemtobeinserted)

DELETIONINSTACK(POPOPERATION)

Thisoperationisusedtodeleteanelement fromstack. Algorithm :-

POP(STACK,TOP,ITEM)
ThisproceduredeletesthetopelementofSTACKandassignsittothevariable ITEM.
1. IfTOP =0theprintUnderflowand Return.
2. SetITEM=STACK[TOP](AssignsTOPelementtoITEM)
3. SetTOP=TOP-1(DecreasesTOP by1)
4. Return

DataStructure 20 SwagatikaDalai
StackPushandPopOperations

ARITHMETICEXPRESSION

Thereare3notationtorepresentanarithmetic expression.

1. Infix notation
2. Prefixnotation
3. Postfixnotation

1. INFIXNOTATION
• Theconventionalwayofwritinganexpressioniscalledasinfix.
Ex:A+B, C+D, E*F, G/M etc.
• Herethenotation is
<Operand><Operator><Operand>
• Thisiscalledinfixbecausetheoperatorscomeinbetweentheoperands.
2. PREFIXNOTATION
• Thisnotationisalsoknownas “POLISH NOTATION”
• Herethenotation is
<Operator><Operand><Operand>
• Ex:+AB,-CD,*EF, /GH

21
3. POSTFIXNOTATION
• Thisnotationiscalledaspostfixorsuffixnotationwhereoperatorissuffixed by
operand.
• Herethenotation is
<Operand><Operand><Operator>
• Ex:AB+, CD-,EF*,GH/
• Thisnotationisalsoknownas“REVERSEPOLISH NOTATION.”
CONVERSIONFROM INFIXTOPOSTFIXEXPRESSION

Algorithm:

POLISH(Q,P)

Q is an arithmetic expression written in infix notation. This algorithm finds the equivalent
postfix expression P.
1. Push“(” ontostackand add“)”tothe endofQ.
2. ScanQ fromleft toright andrepeat steps3to6foreachelementofQuntiltheSTACKis empty.

3. Ifanoperandisencountered,addit top.
4. Ifaleftparenthesis isencountered, pushitontostack.
5. IfanoperatorXisencountered,then:
a) Repeatedly POP from STACK and add to P each operator (on the top of the
STACK) which has the same precedence as or higher precedence than the
operator X .
b) Addthe operatorXtoSTACK.
6. Ifarightparenthesisisencounteredthen:
a) RepeatedlyPOPfromSTACKandaddtoPeachoperator(onthetopofthe STACK) until
a left parenthesis is encountered.
b) Removetheleftparenthesis.
[End of ifstr.]
[EndofStep-2Loop]

7. Exit

22
Ex:A+(B*C–(D/E^F)*G)*H)

SymbolScanned STACK EXPRESSION(POSTFIX)


(
A ( A
+ (+ A
( (+( A
B (+( AB
* (+(* AB
C (+(* ABC
- (+(- ABC*
C (+(-( ABC*
D (+(-( ABC*D
/ (+(-(/ ABC*D
E (+(-(/ ABC*DE
^ (+(-(/^ ABC*DE
F (+(-(/^ ABC*DEF
) (+(- ABC*DEF^/
* (+(-* ABC*DEF^/
G (+(-* ABC*DEF^/G
) (+ ABC*DEF^/G*-
* (+* ABC*DEF^/G*-
H (+* ABC*DEF^/G*-H
) ABC*DEF^/G*-H*+

EquivalentPostfixExpression:ABC*DEF^/G*-H*+

EVALUATIONOFPOSTFIXEXPRESSION

Algorithm:
Thisalgorithm findsthevalueofanarithmeticexpressionP writteninPostfix notation.
1. Addarightparenthesis“) ”attheendofP.
2. Scan P from left to rightand repeat steps 3 and 4 for each element of P until the “ ) ” is
encountered.
3. Ifanoperandisencountered,putitonSTACK.
4. IfanoperatorXisencounteredthen:
a) RemovethetwotopelementofSTACK,whereAistopelementandBisthe next-to-top
element.

23
b) Evaluate BXA.
c) Placetheresultof(b)onSTACK.
[End of if str.]
5. Set value equaltothe topelementonSTACK.
6. Exit

Ex: 5, 6,2,+, *, 12,4, /,-)

SymbolScanned STACK
5 5
6 5,6
2 5,6,2
+ 5,8
* 40
12 40,12
4 40,12,4
/ 40,3
- 37(Result)
)

APPLICATIONSOFSTACK:
 Recursionprocessusesstack for itsimplementation.
 Quicksortusesstackfor sortingthe elements.
 EvaluationofantitheticexpressioncanbesonebyusingSTACK.
 Conversionfrominfixtopostfixexpression
 Evaluationofpostfixexpression
 Conversionfrominfixtoprefixexpression
 Evaluationofprefixexpression.
 Backtracking
 Keeptrackofpost-visited(historyofaweb-browsing)
IMPLEMENTATIONOFRECURSION

Afunctionissaidto beRecursive functionifit callitselforit calla functionsuchthatthe function call


back the original function.
ThisconceptiscalledasRecursion.
TheRecursivefunctionhastwoproperties.

24
I. Thefunctionshouldhaveabase condition.
II. Thefunctionshouldcomeclosertowardsthebase condition.
Thefollowingisoneoftheexampleofrecursivefunctionwhichisdescribedbelow.
Factorialofano.usingRecursion

The factorialofano. ‘n’istheproductofpositiveintegers from1ton. n ! = 1

X 2 X 3 X.....................................X (n-) X n
Physicallyprovedn!= nX (n-1)!
Thefactorialfunctioncanbedefinedasfollows.

I. Ifn=0thenn!=1
II. Ifn>0thenn!=n.(n-1)!
Thefactorialalgorithmisgivenbelowfactorial(fact,n)

Thisprocedurecalculatesn!and returnsthevalueinthevariablefact.

1. Ifn=0thenfact=1and Return.
2. Callfactorial(fact,n-1)
3. Setfact= fact*n
4. Return

25
Ex:Calculatethefactorialof4. 4 ! = 4

x3!
3!=3x2!
2!=2x1!
1!=1x0!
0!=1
1!=1x1=1
2!=2x1=2
3!=3x2=6

4!=4x6=24

26
QUEUE
• Queue is a linear data structure or sequential data structure where insertion take place at
one end and deletion take place at the other end.
• TheinsertionendofQueueiscalledrearendanddeletionendiscalledfrontend.
• Queue is based on (FIFO) First in First Out Concept that means the node i.e. added firstis
processed first.
• HereEnqueueisInsertOperation.
• DequeueisnothingbutDeleteOperation.

Representation OfQueueInMemory

 TheQueue isrepresentedbyaLineararray“Q”andtwo pointervariableFRONTand


REAR.
 FRONTgivesthe locationofelementto bedeletedandREARgivesthelocationafter which
the element will be inserted.
 Thedeletionwillbedonebysetting
Front = Front + 1
 Theinsertionwillbedonebysetting
Rear = Rear + 1

INSERTIONINQUEUE(Enqueue)

Algorithm:

Insert(Q,ITEM,Front,Rear)
ThisprocedureinsertITEMinqueueQ.

1. IfFront=1andRear=Max
Print ‘Overflow’ and Exit.
2. Iffront=NULLthan
Front = 1
Rear=1
else
27
Rear =Rear+1
3. Q[Rear]= ITEM
4. Exit

DELETIONINQUEUE(Dequeue)

Algorithm:
Delete(Q,ITEM, FRONT, REAR)
Thisprocedureremoveelementfromqueue Q.
1. IfFront=Rear=NULL
Print ‘Underflow’ and Exit.
2. ITEM=Q(FRONT)
3. IfFront=Rear
Front = NULLRear
= NULL
else
Front =Front+1
4. Exit

28
EnqueueandDequeue Operations

29
CIRCULARQUEUE

 Letwehaveaqueuethatcontain‘n’elementsinwhichQ[1] comesafterQ[n].
 Whenthistechnicisusedtoconstructaqueuethenthequeueiscalled circular queue.
 InCircular queuewhenREARis‘n’andanyelementisinsertedthenREARwillsetas1.
 Similarlywhen the front is n and anyelement is deleted then front will
be 1.

CircularQueuerepresentation
INSERTIONALGORITHMOFCIRCULARQUEUE

Insert(Q,N,FRONT, REAR, ITEM)


ThisprocedureinsertsanelementITEMintothecircular queueQ.
1. IfFront=1andRear=N
then print ‘Overflow’ and Exit.
2. Iffront=NULL
thenFront=1andRear=1 else
if Rear = N then
setRear =1
else
setRear=Rear+1
(End of if Str.)
3. Set Q[Rear]=ITEM(Insertanew element)
4. Exit

30
DELETIONALGORITHMOFCIRCULARQUEUE

Delete(Q, N,ITEM,FRONT,REAR)
Thisproceduredeleteanelementfromacircular queue.
1. IfFront=NULLthenwriteUnderflowand Return.
2. SetITEM=Q[FRONT]
3. IfFront= Rear
ThenFront=NULLandRear=NULL Else if
Front = N then
SetFront=1
else
Front= Front+1
[Endofif str.]
4. Return
PRIORITYQUEUE
 Ifisatypeofqueue inwhicheachelement has beenassignedaprioritysuchthatthe order in
which the elements are processed according to the elements are processed according
to the following rule
I. Thiselement ofhighpriorityisprocessedfirst.
II. Theelement havingsamepriorityisprocessedaccordingtotheorderinwhich they
are inserted.
 Thelowerrankedno.enjoyhighpriority.

RepresentationofPriorityQueueinmemory
PriorityQueuecanberepresentedinmemoryin2 ways.

I. Onewaylistrepresentation

II. Arrayrepresentation

31
Onewaylistrepresentation

Eachnodeinthelistcontain3fields.i.e.INFO,PRIORITY, LINK

ArrayRepresentation
Herethe priorityqueueisrepresented bymeansof 2D array.
Whererowvaluerepresentstheprioritynumberandeachrowmaintainedasacircularqueue. Each row has
its FRONT and REAR value to represent the starting and ending of row.

FRONT REAR

2 4
1 2
3 1

TypesOfPriority Queue
1. AscendingPriorityQueue:-
Itisthecollectionofitems intowhichitemscanbe insertedarbitrarilyand fromwhichonlythe smallest item
can be removed.
2. DescendingPriorityQueue:-
ThisissimilartoascendingpriorityQueuebutitallowsdeletionofonlythelargestitem.

32
Chapter-5
LINKEDLIST
LinkedListisacollectionofdataelementscalledasnodes. The
node has 2 parts
 Infoisthedatapart
 Nexti.e. theaddresspartthatmeansitpointstothenext node.

Iflinkedlistadjacencybetweentheelementsaremaintainedbymeansoflinks or pointers. A link


or pointer actually the address of the next node.

Thelast nodeofthelistcontain‘\0’NULLwhichshows theendofthelist.


Thelinkedlist containanother pointer variable‘Start’whichcontaintheaddress thefirst node. Linked
List is of 4 types
 SingleLinkedList
 DoubleLinkedList
 CircularLinkedList
 HeaderLinkedList
RepresentationofLinkedListinMemory
 LinkedListcanberepresentedinmemorybymeans2lineararraysi.e.DataorinfoandLinkor address.

 They are designed such that info[K] contains the Kth element and Link[K] contains the next pointer
field i.e. the address of the Kth element in the list.

 TheendoftheListisindicatedbyNULLpointeri.e.thepointerfieldofthelastelementwillbe NULLorZero

.
Start=4
info[4]=N Link[4]=3
info[3]=T Link[3]=6
info[6]=E Link[6]=2
info [2] = F Link[2]=0i.e.thenullvalue So
the list has ended .

RepresentationofanodeinaLinkedList Struct
Link
{
intdata;
StructLink *add;
};

SINGLELINKEDLIST
ASingleLinkedListisalsocalledasone-waylist.Itisalinearcollectionofdataelementscallednodes, where the linear
order is given by means of pointers.
Eachnodeisdividedinto2 parts.
I. Information
II. Pointertonextnode.
OPERATIONONSINGLELINKEDLIST

1. Traversal
Algorithm:
Display(Start)This algorithmtraversethe list startingfromthe1 st nodetothe endof thelist. Step-
1 : “Start” holds the address of the first node.
Step-2 : Set Ptr = Start [Initializes pointer Ptr]
Step-3:RepeatStep4to5,WhilePtr ≠NULL
Step-4:Processinfo[Ptr][applyProcesstoinfo(Ptr)]
Step-5:Set Ptr=next[Ptr][movePrint tonext node]
[End of loop]
Step-6:Exit
2. Insertion

The insertion operation is used to add an element in an existing Linked List. Thereis various positions where
node can be inserted.

 Insertatthebeginning
 Insertatend
 Insertatspecificlocation.

InsertAtTheBeginning

Supposethenewmodewhoseinformationfieldcontains20isinsertedasthefirst node.

Algorithm:
This algorithm is used to insert a node at the beginning of the Linked List. Start holds the address of the first
node.
Step-1:Createanewnodenamedas P.

: Set info [P] = 𝑥 (copies a new data into a new node)


Step-2:IfP ==NULLthenprint“Outofmemoryspace”andexit. Step-3

Step-4:Setnext[P]=Start(newnodenowpointstooriginalfirstnode) Step-5 :
Set Start = P
Step-6:Exit

InsertAtTheEnd
Toinsert anodeat theendofthelist, weneedtotraversetheList andadvancethepointer untilthelast node is
reached.
Supposethenewnodewhoseinformationfieldcontains77isinsertedatthelast node.
Algorithm:
This algorithmis usedtoinsert anodeat theendofthelinked list. ‘Start’holds theaddress ofthefirst node. Step-
1 : Create a new node named as P.

: Set info [P] = 𝑥 (copies a new data into a new node)


Step-2:IfP =NULLthenprint“Outofmemoryspace”andExit. Step-3

Step-4:Setnext[P]=NULL
Step-5 : Set Ptr = Start
Step-6:RepeatStep-7whilePtr≠NULL
Step-7 : Set temp = Ptr
Ptr=next[Ptr](Endofstep-6loop)
Step-8 : Set next [temp] = P
Step-9:Exit

InsertAtAnySpecificLocation

To insert a new node at any specific location we scan the List fromthe beginning and move up to the desired
node where we want to insert a new node.
Inthebelowfig.Whoseinformationfieldcontain88 isinsertedat3 rdlocation.

Algorithm:
‘Start’holdstheaddress ofthe1stnode.
Step-1 : Set Ptr = Start
Step-2:Createanewnodenamedas P.

Step-4:Setinfo[P]= 𝑥 (copiesa newdata intoa newnode)


Step-3 : If P = NULL then write‘Out of memory’ andExit.

Step-5 : Set next [P] = NULL


Step-6:ReadLocStep-7:Seti= 1
Step-8:Repeatsteps9to11whilePtr ≠NULLandi<Loc Step-
9 : Set temp = Ptr.
Step-10:SetPtr=next [Ptr]
Step-11:Seti=i+1 [End
of step-7 loop]
Step-12:Setnext[temp]=P
Step-13 : Set next [P] = Ptr
Step-14 : Exit

3. Deletion
Thedeletionoperationsusedto deleteanelement fromasingle linked list.Therearevarious position
where node can be deleted.

Deletethe1stNode

Algorithm:
Startholdstheaddressofthe1stnode.
Step-1 : Set temp = Start
Step-2:IfStart=NULLthenwrite‘UNDERFLOW’&Exit. Step-
3 : Set Start = next[Start]
Step-4:Freethespaceassociatedwithtemp.
Step-5 : Exit

Deletethelastnode
Algorithm:
Startholdstheaddressofthe1st node.
Step-1:SetPtr=Start Step-2: Settemp=Start
Step-3: IfPtr=NULLthenwrite‘UNDERFLOW’&Exit.Step-4:RepeatStep-5 and 6
While next[Ptr] ≠ NULL Step-5 : Set temp = Ptr
Step-6:SetPtr=next[Ptr](Endofstep4loop) Step-
7 : Set next[temp] = NULL
Step-8:FreethespaceassociatedwithPtr.
Step-9 : Exit

Deletethenodeatanyspecificlocation

Algorithm:
Startholdstheaddressofthe1stnode.
Step-1 : Set Ptr = Start
Step-2:Settemp=Start
Step-3:IfPtr=NULLthenwrite‘UNDERFLOW’andExit. Step-
4 : Set i = 1
Step-5:ReadLoc
Step-6:RepeatStep-7to9whilePtr ≠NULLandi<Loc Step-7
: Set temp = Ptr
Step-8:SetPtr= next[Ptr]
Step-9 : Set i = i+1
(EndofStep6loop)
Step-10:Setnext[temp]= next[Ptr]
Step-11:FreethespaceassociatedwithPtr.
Step-12 : Exit

4. SEARCHING

Searchingmeansfindinganelementfromagivenlist.
Algorithm:
Startholdstheaddressofthe1stnode.
Step-1 : Set Ptr = Start
Step-2 : Set Loc = 1
Step-3:Readelement
Step-4:Repeat Step-5and7WhilePtr≠NULL
Step-5:If element=info[Ptr]thenWrite‘Elementfound atposition’, LocandExit. Step-
6 : Set Loc = Loc+1
Step-7:SetPtr=next[Ptr] (End
of step 4 loop)
Step-8:Write‘Elementnotfound’
Step-9 : Exit

Headerlinkedlist
HeaderLinkedListisa modified versionofSinglyLinkedList.
In Header linked list, we have a special node, the Header Nodepresent at the beginning of the
linked list.
The Header Node is an extra node at the front of the list storing meaningful information aboutthe
list.
It does not represent any items of the list like other nodes rather than the information present in
the Header node is global for all nodes such as Count of Nodes in a List,Maximumamong all
Items, Minimum value among all Items etc.
ThisgivesusefulinformationabouttheLinkedlist.

Thisdatapartofthisheader nodeisgenerallyusedto holdanyglobalinformationaboutthe entire linked


list. The next part ofthe header node points to the first node in the list.

Aheaderlinkedlistcanbedividedintotwotypes:

i) Groundedheaderlinked listthatstoresNULLinthelastnode’snextfield.

ii) Circularheader linked list thatstorestheaddressoftheheadernode inthenext partofthelast node


of the list.
GarbageCollection
Theoperatingsystemofacomputer mayperiodicallycollect allthedeleted spaceontothefree storage list.
Any technique which does these collections is called garbage collection.

When we delete a particular note froman existing linked list or delete the linked list the space
occupied byit must begivenbacktothefreepool.Sothatthe memorycanbetheusedbysome other
program that needs memory space.

Tothefreepoolisdone.

Theoperatingsystemwillperformthisoperationwhenever it findstheCPU idleorwheneverthe programs


are falling short of memoryspace. The OS scans throughthe entire memorycell & marks those
cells. That are being by some programthen it collects the entire cellwhich are not being used
&add tothe free pool. Sothat this cells canbe used byother programs. This process is called
garbage collection. The garbage collection is invisible to the programmer.
Chapter-6
TREE
Atree is non-linear and a hierarchical data structure consisting ofa collection of nodes such that
each node of the tree stores a value.

Application:
The followingaretheapplicationsoftrees:
 Storingnaturallyhierarchicaldata: Treesareusedtostorethedatainthehierarchical
structure. For example, the file system.
 Organizedata: Itisusedtoorganizedataforefficientinsertion,deletionand searching.
 Trie:It isaspecialkindoftreethatisusedtostorethedictionary.It isa fast andefficient way for
dynamic spell checking.
 Heap:Itisalsoatreedatastructureimplementedusingarrays.It isusedtoimplement priority
queues.
 B-TreeandB+Tree: B-TreeandB+Treearethetreedatastructuresusedto implement
indexing in databases.
 Routingtable: Thetreedatastructureisalso usedtostorethedatainroutingtables in the
routers.
BasicTreeTerminologies
1. Node
Anodeisanentitythatcontainsakeyorvalue .
The last nodesofeachpatharecalled leafnodesorexternalnodesthat donotcontaina link/pointer to
child nodes.
Thenodehavingatleastachildnodeiscalledaninternalnode.
2. Root Node: The topmost node of a tree or the node which does not have anyparent node is
calledtherootnode. {1} istherootnodeofthetree. Anon-emptytreemust containexactlyone root
node .
3. ParentNode: Thenodewhichisapredecessorofanodeiscalledtheparent nodeofthat node.
{2} is the parent node of {6, 7}.
4. ChildNode:Thenodewhichisthe immediatesuccessorofanodeiscalledthechild nodeof that
node. Examples: {6, 7} are the child nodes of {2}.
5. Degreeof a Node: The totalcount of subtrees attached to that node is called the degree of the
node. The degree ofa leaf node must be 0. The degree ofa tree is the degree of its root. The
degreeofthenode{3} is3. LeafNodeorExternalNode:Thenodeswhichdo nothaveanychild nodes
are called leaf nodes. {6, 14, 8, 9, 15, 16, 4,11, 12, 17, 18, 19} are the leaf nodes ofthe tree
6. Sibling:Childrenofthesameparentnodearecalledsiblings.{8,9,10}arecalled siblings.
7. Depthofanode:Thecountofedgesfromtheroottothe node.Depthofnode{14}is 3.

8. Heightofanode:Thenumber ofedgesonthe longest pathfromthat nodeto a leaf. Heightof node


{3} is 2.
9. Heightofatree: Theheightofatreeistheheightoftherootnodei.ethecountofedges from the root to
the deepest node. The height of the above tree is 3.
10. Levelofanode: Thecountofedgesonthepathfromthe root nodetothatnode.Theroot node has
level 0.
11. Internalnode: AnodewithatleastonechildiscalledInternalNode.
12. AncestorofaNode: Anypredecessor nodesonthepathoftheroottothatnodearecalled
Ancestors of that node. The node 1 is called ancestor of node7 as there is successive parent
present from node 7 to node 1. {1, 2} arethe parent nodes of the node {7}
13. Descendant: Anysuccessor nodeonthepathfromthe leafnodetothat node.{7,14}arethe
descendants of the node. {2}.
14. Path:Asequenceofedgesiscalled aspath.
BinaryTree
Eachnodeofabinarytreeconsistsofthreeitems:
 dataitem
 addressofleftchild
 addressofrightchild
A binarytree is a tree-type non-linear data structure with a maximumof two children for each
parent. Here,binaryname itselfsuggeststhat 'two';therefore, eachnodecanhaveeither 0, 1or2
children.
TypesofBinary Tree
1. Full/proper/strictBinarytree
The fullbinarytree isalso knownasa strict binarytree.The tree canonlybe considered asthe
fullbinarytreeifeachnodemust containeither0or2children. The fullbinarytreecanalso be defined
as the tree in which each node must contain 2 children except the leaf nodes.

2. CompleteBinarytree

Thecompletebinarytreeisatreeinwhichallthe nodesarecompletelyfilled except the last


level.Inthe last level,allthe nodesmust beasleft aspossible.Inacompletebinarytree,the nodes
should be added from the left.

Ifwe number thenodesofthecompletebinarytreefromleft toright levelbylevelthenthe node k has


its left child at 2k position and right child at 2k+1 position.
BinaryTreeRepresentationinmemory
A tree must represent a hierarchical relationship between parent node and child node.
Let TbeaBinaryTree. Therearetwo waysofrepresenting T inthe memoryas follow
1. SequentialorLinearRepresentationofBinaryTree.
2. Link RepresentationofBinaryTree.

1. SequentialorLinearRepresentationofBinaryTree.
Let us consider that we have a tree T. let our treeT is a binary tree that us complete binary tree.
Then there is an efficient way of representing T in the memory called the sequential
representation or array representation ofT. This representation uses only a linear array TREE as
follows:
1. TherootNofTisstored inTREE[1].
2. If anode occupiesTREE[k]thenitsleftchildis storedinTREE[2*k] andits right child is
stored into TREE [2 * k + 1].

Itssequentialrepresentation isasfollow:

2. LinkedRepresentationofBinaryTree
ConsideraBinaryTree T.Twillbe maintained inmemorybymeansofa linked list representation
which uses three parallel arrays; INFO, LEFT, and RIGHT pointer
variableROOT as follows. InBinaryTreeeachnode NofT willcorrespondto alocationksuch that
1. LEFT[k]containsthelocationoftheleftchildofnodeN.
2. INFO[k]containsthedataat thenodeN.
3. RIGHT[k]containsthelocationofright childofnode N.
Representation of a node:

Inthis

representationofbinarytreerootwillcontainthelocationoftherootRofT.Ifanyoneof the
subtree isempty, thenthecorrespondingpointerwillcontainthenullvalue ifthetree Titselfis empty,
the ROOT will contain the null value.
Example
Consider thebinarytreeTinthe figure. Aschematicdiagramofthelinked list representationof T
appears in the following figure. Observe that each node is pictured with its three fields, and that
the empty subtree is pictured by using x for null entries.

BinaryTree

Linked Representationofthe BinaryTree:

BinaryTree Traversal

Traversalisaprocessto visit allthenodesofatreeand mayprint theirvaluestoo.Because,all nodes are


connected via edges (links) we always start fromthe root (head) node. That is, we cannot
randomly access a node in a tree.
There arethree wayswhichweuse totraverse atree−
 In-order Traversal
 Pre-orderTraversal
 Post-orderTraversal
1. In-orderTraversal
Thisisalsoknownassymmetricorder.
This traversal is referred as LNR.
Algorithm-

1. First,visitallthenodesintheleftsubtree
2. Thentherootnode
3. Visitallthenodesintherightsubtree
Left→ Root→Right

Application-
Inordertraversalisusedtoget infixexpressionofanexpressiontree.
2. PreorderTraversal-
Thisisalsoknownasdepthfistorder. This
traversal is referred as NLR.
Algorithm-
1. Visittheroot
2. Traversetheleftsubtreei.e.callPreorder(leftsubtree)
3. Traversetheright subtreei.e.callPreorder(right subtree)

Root → Left → Right

Applications-
Preordertraversalisusedto getprefixexpressionofanexpressiontree.Preordertraversalisused to create a
copy of the tree.

3. PostorderTraversal-
ThistraversalisreferredasLRN.
Algorithm-
1. Traversetheleftsubtreei.e. callPostorder(leftsubtree)
2. Traversetherightsubtreei.e.callPostorder(rightsubtree)
3. Visit the root

Left→Right→Root

Applications-
 Postordertraversalisusedto getpostfixexpressionofanexpressiontree.
 Postordertraversalisused todelete thetree.
 Thisisbecauseitdeletesthechildrenfirstandthenitdeletestheparent.
BinarySearchtree
AbinarytreeTistermedasBinarySearchTreeifeachnodenofTsatisfiesthefollowing property.
 Thevalueofn isgreaterthanthevalueofallnodesin itsleftsubtree.
 Thevalueofn islessthanthevalueofall nodesin itsrightsubtree.

AdvantagesofBinarysearchtree
o Searchinganelement intheBinarysearchtreeiseasyaswealways haveahint that which
subtree has the desired element.
o Ascomparedtoarrayand linked lists, insertionanddeletionoperationsare fasterinBST.
Example of creating a binary search tree
Supposethe dataelements are-45,15,79,90,10,55,12,20,50
o First,wehavetoinsert45intothetreeastheroot ofthetree.
o Then,readthenext element;ifit issmallerthantheroot node,insert it as therootofthe left
subtree, and move to the next element.
o Otherwise, iftheelement islargerthantheroot node,theninsert it astherootoftheright
subtree.
Now,let'sseetheprocessofcreatingtheBinarysearchtreeusingthegivendataelement. The process of
creating the BST is shown below -
Step1-Insert 45.

Step 2-Insert15.
As15issmallerthan45,soinsertitastherootnodeoftheleftsubtree

Step3-Insert 79.
As79 isgreaterthan45,so insertitastherootnodeoftherightsubtree.

Step4-Insert 90.
90isgreaterthan45and79,soitwillbeinsertedastherightsubtreeof79.

Step5-Insert 10.
10issmallerthan45and15,soitwillbeinsertedasaleftsubtreeof15.
Step6-Insert 55.

55is largerthan45andsmallerthan79, soitwillbeinsertedastheleftsubtreeof79.

Step7-Insert 12.

12issmaller than45and15butgreaterthan10,soitwillbeinsertedastherightsubtreeof10.

Step8-Insert 20.

20issmaller than45butgreaterthan15,so itwillbeinsertedastherightsubtreeof15.


Step9-Insert 50.

50isgreaterthan45butsmallerthan79and55. So,itwill beinsertedasaleftsubtreeof55.

Now,thecreationofbinarysearchtreeiscompleted.

SearchinginBinarysearchtree
Searching means to find or locate a specific element or nodeina data structure. InBinarysearch
tree, searchinga nodeiseasybecauseelements inBST arestoredinaspecificorder.Thestepsof
searching a node in Binary Search tree are listed as follows –
1. First, comparetheelementtobesearchedwiththerootelementofthetree.
2. Ifrootismatchedwiththetargetelement,thenreturnthenode'slocation.
3. Ifit isnotmatched,thencheckwhethertheitemislessthantherootelement, ifit is smaller
than the root element, then move to the left subtree.
4. Ifitis largerthantherootelement,thenmovetotherightsubtree.
5. Repeattheaboveprocedurerecursivelyuntilthematchisfound.
6. Iftheelementisnot foundor not presentinthetree,thenreturnNULL.
Now,let'sunderstandthesearching inbinarytreeusinganexample.Wearetakingthebinary search tree
formed above. Suppose we have to find node 20 from the below tree.
Step1:

Step2:

Step3:
Now,let'sseethealgorithmtosearchanelement intheBinarysearchtree.
Algorithm to search an element in Binary search tree
1. Search(root,item)
2. Step1-if(item=root→data)or(root=NULL)
3. returnroot
4. elseif(item<root→ data)
5. returnSearch(root→left,item)
6. else
7. returnSearch(root→right,item)
8. ENDif
9. Step2-END

InsertioninBinarySearchtree
AnewkeyinBSTisalwaysinsertedattheleaf.
To insert an element in BST, we have to start searching from the root node; if the node to be
inserted is less than the root node, then search for an empty location in the left subtree. Else,
search for the empty location in the right subtree and insert the data.
Now,let'sseetheprocessofinserting anodeinto BSTusing anexample.
DeletioninBinarySearchtree
In a binary search tree, we must delete a node from the tree bykeeping in mind that the property
of BST is not violated. To delete a node fromBST, there are three possible situations occur -
o Thenodetobedeletedistheleafnode,or,
o Thenodeto bedeleted hasonlyonechild,and,
o Thenodeto bedeleted hastwochildren
1. Whenthenodetobedeleted istheleafnode
o ItisthesimplestcasetodeleteanodeinBST.Here,wehavetoreplacetheleafnode with NULL
and simply free the allocated space.
o Inbelowimage,supposewehavetodeletenode 90,as thenode tobedeletedisaleaf node, so it
will be replaced with NULL, and the allocated space will free.

2. Whenthe nodetobe deleted hasonlyonechild


In this case, we have to replace the target node with its child, and then delete the child node. It
meansthat afterreplacing thetarget nodewith itschild node,the child node willnow containthe
value to be deleted. So, we simply have to replace the child node with NULL and free up the
allocated space.
In the below image, suppose we have to delete the node 79, as the node to be deleted has only
one child, so it will be replaced with its child 55.
So,thereplaced node79 willnowbealeafnode thatcanbeeasilydeleted.

3. Whenthe nodetobe deletedhastwochildren


If the node has 2 children ,the strategy is to place the data of this nodewith the smallest data of
the right subtree and recursively delete that node.
In the below image, suppose we have to delete node 45 that is the root node, as the node to be
deleted has two children, so it will be replaced with its inorder successor. Now, node 45 will beat
the leaf of the tree so that it can be deleted easily.
Chapter-7
GRAPH
Agraphisa non-linearkind ofdatastructure madeupofnodesorverticesandedges.

Formally, a graph is a pair ofsets (V, E), where Vis the set ofvertices and E is the set ofedges,
connecting the pairs of vertices.

In the above Graph, the set of vertices V = {0,1,2,3,4} and the set of edges E = {01, 12, 23, 34,
04, 14, 13}.

Applications

Graphs are used to solve many real-life problems. Graphs are used to represent networks. The
networks may include paths in a city or telephone network or circuit network. Graphs are also
used in social networks like linkedIn, Facebook. For example, in Facebook, each person is
represented witha vertex(ornode). Eachnode is a structure and contains information like person
id, name, gender etc.

GraphTerminologies
1. Undirected: Agraphinwhichalltheedgesarebi-directional.Theedgesdonotpointina specific
direction.
2. Directed Graph orDigraph: AgraphG=(V, E) witha mapping fsuchthat everyedge maps
onto some ordered pair of vertices (Vi, Vj) is called Digraph. It is also called Directed Graph.
Ordered pair (Vi, Vj) means an edge between Vi and Vj with an arrow directed from Vi to Vj.

3. Weighted Graph:A graph that has a value associated with every edge. The values
corresponding to the edges are called weights. A value in a weighted graph can represent
quantities such as cost, distance, and time, depending on the graph. Weighted graphs aretypically
used in modeling computer networks.

4. Unweighted Graph:A graph in which there is no value or weight associated with the
edge. All the graphs are unweighted by default unless there is a value associated.
5. Complete Graph:A simple graph with n vertices is called a complete graph if the degree
of each vertex is n-1, that is, one vertex is attach with n-1 edges. A complete graph is also called
Full Graph.

6. CyclicGraph:Foragraphtobecalledacyclicgraph,itshouldconsistofatleastonecycle.Ifa graph
hasaminimumofonecycle present, it is called acyclic graph.

7. Acyclic Graph:A graph is called an acyclic graph if zero cycles are present, and an acyclic
graph is the complete opposite of a cyclic graph.

8 .Vertex:Every individualdataelementiscalled avertexoranode.


9 .EdgeItisaconnectinglinkbetweentwonodesorvertices.
10. Degree: The total number ofedges connected to a vertex
in a graph. Indegree: The total number of incoming edges connected to a
vertex. Outdegree
Thetotalnumberofoutgoingedgesconnectedtoavertex.
11. Self-loop: Ifthere is an edge whose stat and end vertices are same i.e.(vi,vi)is an edge ,then
it is called self loop.
12. Adjacent Nodes:Twonodesarecalled adjacentiftheyareconnectedthroughanedge.
13. Path:sequenceofverticesinwhicheachpairofsuccessiveverticesisconnected byan edge
14. IsolatedVertex:Avertexwithdegreezeroiscalledanisolatedvertex.
Example:

Here,thevertex'a'andvertex'b'hasano connectivitybetweeneachother andalsoto anyother vertices.


So the degree ofboththe vertices 'a' and 'b' are zero. These are also called as isolated vertices.

RepresentationofGraph
Thegraphcanberepresentedasmatrices.
Adjacency Matrix
Adjacency Matrix is used to represent a graph. We can represent directed as well as undirected
graphs using adjacency matrices..
If agraph hasn numberofvertices,then theadjacency matrix of thatgraphisnx n,andeach entry of
the matrix represents the number of edges from one vertex to another.
AdjacencyMatrixRepresentationforundirectedgraph
IfanUndirectedGraphGconsistsofnverticesthentheadjacencymatrixofagraphisnxn matrix A = [aij]
and defined by -
aij=1{ifthere isapathexists fromVitoVj} aij = 0
{Otherwise}
Inanundirectedgraph,ifthereisanedgeexistsbetweenVertexAandVertexB,thenthe vertices can be
transferred from A to B as well as B to A.
Theadjacencymatrixoftheabovegraphwillbe-

Adjacencymatrixrepresentationforadirectedgraph

Inadirectedgraph, edgesformanorderedpair. Edgesrepresent aspecificpathfromsomevertex


Atoanother vertexB. NodeAiscalledtheinitialnode,whilenodeBiscalledtheterminalnode.

Theadjacency matrixoftheabovegraphwillbe-
Propertiesoftheadjacencymatrix

Someofthepropertiesoftheadjacencymatrixarelistedasfollows:

o An adjacency matrix is a matrix that contains rows and columns used to represent a
simple labeledgraphwiththenumbers0and1 inthepositionof(VI, Vj),accordingtothe
condition of whether or not the two Vi and Vjare adjacent.

o For adirected graph, ifthere isanedgeexistsbetween vertex ior Vito Vertexjor Vj, then
the value of A[Vi][Vj] = 1, otherwise the value will be 0.

o For anundirectedgraph, ifthere isanedgethat existsbetweenvertexiorVito Vertexj or Vj,


then the value of A[Vi][Vj] = 1 and A[Vj][Vi] = 1, otherwise the value will be 0.
Chapter-8
SORTINGSEARCHING&MERGING
Sorting refers to the operation or technique of arranging and rearranging sets of data in some
specific order. Sorting can be done in ascending and descending order such as such as
increasing or decreasing with numerical data or alphabetically withcharacter data.
BUBBLE SORT
Bubble sort is a simple sorting algorithm. Bubble sort works on the repeatedly swapping of
adjacent elements until they are not in the intended order. This algorithm is not suitable for
large data sets as its average and worst case complexity are of Ο(n2) where n is the number of
items.
Algorithm:
a isthe list ofelementsstoredinmemoryto besorted. m is the
size of the list.
bubbleSort(a[],m)
1. for(pass=0;pass<m-1;pass++)
2. for(i=0;i<m-pass-1;i++)
3. if(a[i]>a[i+1])
4. temp=a[i];
5. a[i] =a[i+1];
6. a[i+1]=temp;
End

WorkingofBubblesortAlgorithm
Tounderstandtheworkingofbubblesort algorithm, let'stakeanunsortedarray. Let the
elements of array are -

FirstPass
Sortingwillstartfromtheinitialtwoelements.Letcomparethemtocheckwhichis greater.

Here, 32isgreaterthan13(32>13),soitisalreadysorted.Now,compare32with 26.

Here, 26issmallerthan36. So,swapping isrequired. Afterswappingnewarraywilllook like - Now,

compare 32 and 35.

Here, 35isgreaterthan32. So,thereisno swappingrequiredastheyarealreadysorted. Now, the


comparison will be in between 35 and 10.
Here, 10 is smaller than 35 that are not sorted. So, swapping is required. Now, we reach at the
end of the array. After first pass, the array will be -

Now,movetotheseconditeration.
Second Pass
Thesameprocesswillbefollowedforsecond iteration.

Here, 10issmallerthan32. So,swappingisrequired. Afterswapping, thearraywillbe-

Now,movetothethirditeration. Third
Pass
Thesameprocesswillbefollowedforthirditeration.

Here,10issmallerthan26. So,swappingisrequired. Afterswapping,thearraywill be-

Now,movetothefourthiteration.
Fourth pass
Similarly,afterthefourthiteration,thearraywill be-

Hence,thereis noswappingrequired, sothearrayiscompletelysorted.


TimeComplexity

Case TimeComplexity

BestCase O(n)

AverageCase O(n2)

WorstCase O(n2)

SpaceComplexity
SpaceComplexity O(1)

Quicksort
Quick sort is a faster and highly efficient sorting algorithm which follows the divide and
conquers approach.
Divide and conquer is a technique of breaking down the algorithms into sub problems, then
solving the sub problems, and combining the results back together to solve the originalproblem.

Divide:InDivide, first pick apivot element. Afterthat, partitionorrearrangethearrayinto two sub-


arrays suchthat eachelement inthe left sub-arrayis less thanor equalto the pivot element and
each element in the right sub-array is larger than the pivot element.
Conquer:Recursively,sorttwosubarrayswithQuicksort. Combine:
Combine the already sorted array.
Choosingthepivot
Pivotcanberandom,i.e.selecttherandompivotfromthegivenarray.
Pivotcaneitherbetherightmost elementofthe leftmost elementofthegivenarray. Select
median as the pivot element.
Algorithm:
a isthe list ofelementsstoredinmemoryto besorted. beg
is the initial index of the list.
endisthelastindexofthelist.
Pivotisthepositioninthelistwherepartitiontakesplace.

quickSort(a[],beg,end)
1. if(beg<end)
2. pivot=partition(a,beg,end)
3. quickSort(a,beg,pivot-1)
4. quickSort(a,pivot+1,end)
End
partition(a,beg, end)
1.pivotItem=a[beg]
2. i=beg
3. for(j=beg+1;j<=end;j++)
4. if(a[j]<=pivotItem)
5. i=i+1
6. if(i!=j)
7. swap(a[i],a[j])
8. swap(a[beg],a[i])
9. returni
End

WorkingofQuickSortAlgorithm
To understand the working of quick sort, let's take an unsorted array. It will make the concept more
clear and understandable.
Lettheelementsofarrayare-

Inthegivenarray,weconsidertheleftmostelementaspivot.So,inthiscase,a[left]=24, a[right] = 27 and


a[pivot] = 24.
Since,pivotisatleft, soalgorithmstartsfromrightandmovetowardsleft.

Now,a[pivot]<a[right], soalgorithmmovesforwardonepositiontowardsleft,i.e.–

Now,a[left] =24,a[right] =19,anda[pivot]=24.


Because, a[pivot] > a[right], so, algorithmwill swap a[pivot] with a[right], and pivot moves to right,
as -

Now, a[left] = 19, a[right] = 24, and a[pivot] = 24. Since, pivot is at right, so algorithm startsfrom
left and moves to right.
Asa[pivot] >a[left], soalgorithmmovesonepositiontorightas -
Now, a[left] = 9, a[right] = 24, and a[pivot]= 24. As a[pivot] >a[left], so algorithmmoves one
position to right as -

Now, a[left] = 29, a[right] = 24, and a[pivot] = 24. As a[pivot] < a[left], so, swap a[pivot] and
a[left], now pivot is at left, i.e. –

Since, pivot is at left, so algorithm starts from right, and move to left. Now, a[left] = 24,a[right]
= 29, and a[pivot]= 24. As a[pivot]< a[right], so algorithm moves one positionto left, as –

Now, a[pivot] =24, a[left] = 24, and a[right]= 14. As a[pivot] >a[right], so, swap a[pivot]and
a[right], now pivot is at right, i.e. –

Now, a[pivot]= 24, a[left] = 14, and a[right] =24. Pivot is at right, sothe algorithmstarts from
left and move to right.
Now,a[pivot]=24,a[left]=24,anda[right]=24.So,pivot,leftandrightarepointingthe same element. It
represents the termination of procedure.
Element 24,whichisthepivotelementisplacedatitsexactposition.
Elements that are right side of element 24 are greater than it, and the elements that are left side
of element 24 are smaller than it.

Now, in a similarmanner, quick sort algorithm isseparately applied to the leftand rightsub-
arrays. After sorting gets done, the array will be –

1. TimeComplexity

Case TimeComplexity

BestCase O(n*logn)

AverageCase O(n*logn)

WorstCase O(n2)

2. SpaceComplexity
SpaceComplexity O(n*logn)

MERGING:
Theoperationofsorting iscloselyrelatedto theprocessofmerging. The merging oftwoorder table
which can be combined to produce a single sorted table.
Thisprocesscanbeaccomplisheseasilybysuccessivelyselectingtherecord withthesmallest key
occurring by either of the table and placing this record in a new table.
SIMPLEMERGE
SIMPLE MERGE[FIRST,SECOND,THIRD,K]
Giventwoordersintable sortedina vectorKwithFIRST,SECOND,THIRD
ThevariableI&JdenotesthecurserassociatedwiththeFIRST&SECONDtable respectively. L is the
index variable associated with the vector TEMP.
Algorithm

Step1:[Initialize]
Set I = FIRST
SetJ =SECOND
SetL=0
Step2:[Comparecorrespondingelementsandoutputthesmallest]
Repeat while I < SECOND & J < THIRD
IfK[I]<=K[J],thenL=L+1
TEMP [L]=K[I]
I=I+1
Else L=L+1
TEMP[L]=K[J]
J=J+1
Step3:[Copyremainingunprocessedelement inoutputarea] If
I>=SECOND
ThenrepeatwhileJ<=THIRD L=L+1
TEMP[L]=K[J]
J=J+1
Else
RepeatwhileI<SECOND
L=L+1
TEMP[L]=K[I]
I=I+1
Step4:[Copytheelement intotemporaryvectorintooriginalarea]
Repeat for I=1,2,…..L
K[FIRST-I+1]=TEMP[I]
Step5:Exit

SEARCHING
Findingthe locationofagiveniteminaparticulararrayiscalledassearching. These are 2
types Searching
1. LinearSearch
2. BinarySearch

LinearSearch
 Suppose‘a’isanarraywithnelement.
 Tofindanitemwehavetosearchthearray‘a’ fromthefirstelementsequentially.
 Thissequentialsearch isknownasLinearSearch.
Algorithm:
voidlinearsearch(int a[],int n,intno)
{
intloc= -1,i;for (i=0;i<n;i++)
{
if(a[i]==no)loc=i;
}
if(loc==-1)
Printf(“Datanotfound”); else
Printf(“Datafoundat%dposition/location”,loc);
}
Wherea[]= nameofthe array
n=no.ofelementpresentinarray‘a’
no =numbertobesearchedloc=locationtobesearched

BinarySearch
- SequentialSearchisasimplemethodforsearchinganelementinarray.
- Butthisisnotefficientforlargelistbecausewewillhavetomakencomparisonto search for the
last element in the list.
- Thebinarysearchtechnique isverymuchefficient andappliedonlyinsortedarray. Ex:
Searching a name in telephone directory or searching a word in dictionary.
- In this method we make a comparison between the key and the middle elementof
thearray.
- Since the array is sorted this comparison results either in a match between key and the
middle element of the arrayor identifying the left half or result half of the array.
- Whenthe current element is not equalto the middle element ofthe array, the procedure is
repeated on the half in which the desired element is likely to be present.
- Proceedingin thisway,eithertheelementisdetected orfinal divisionleadstoahalf consisting
of no element.

Algorithm:
intBinarySearch(intkey,inta[],int n)
{
intlow,hi,mid; low = 0
;
hi= n-1;
while(low<=hi)
{
mid = (low+hi)/2;
if(key==a[mid])
return(mid);
if(key<a[mid])
hi = mid-1;else
low=mid+1;
}
return(-1);
}
Example:
Supposethearrayelementsare 11,
22, 33, 44, 55, 66, 77

Wewishtosearchfor33So key=33 low =


0
hi=6
n=7
Islow<=hi Yes
mid = (low+hi)/2=(0+6)/2=3

Is33== a[3]No
Is33<a[3] yes
thestepswillberepeatedforlowerhalf hi =
mid-1=3-1=2
low=0
Is low <= hi
Mid=(0+2)/2=1

If key (33) == a[1] No


If 33 < a[1] No
low=1+1 =2
hi = 2
mid=(2+2)/2=2

Is33==a[2]
Yesthesearchissuccessfulatindex 2.
Chapter-9
FILE ORGANIZATION
File:
 Afileisanexternalcollectionofrelateddatatreatedas a unit.
 Filesare storedinauxiliary/secondarystoragedevices.
– Disk
– Tapes
 Afileisacollectionofdatarecordswitheachrecord consistingofoneormorefields.
 A file is a collection of data records grouped together for purpose of access control
andmodification.
 It is the primaryresource inwhichwe canstore information and canretrievethe
information when it is required.
 Theabsolutefile nameconsistsof:
• drivename
• directoryname(s)
• filename
• Extension
Forexample:d:/network/LAN.doc
TermsUsedwithFiles
 Field: This is the basic element of data which contains a single value
andcharacterizedby itslength and data type.
 Record: This is the collection of related fields that can be treated as a unit bysome
applicationprogram.

Extension TypeofDocument
TypeofDocument Application

Word- MicrosoftWord2003
MicrosoftWord2003
.doc
processingdocument

Word- MicrosoftWord2007
MicrosoftWord2007
.docx
processingdocument

Word- MicrosoftWorkswordprocessing
MicrosoftWorkswordprocessing
.wks
processingdocument

Word- CorelWordPerfect
CorelWordPerfect
.wpd
processingdocument

.xls Spreadsheet MicrosoftExcel

.slr Spreadsheet MicrosoftWorksspreadsheet


MicrosoftWorksspreadsheet

.mdb Database MicrosoftAccess


PowerPoint MicrosoftPowerPoint
.ppt
Presentation

PortableDocumentFor
PortableDocumentFor AdobeAcrobatorAdobeReader
AdobeAcrobatorAdobeReader
.pdf
mat

File Operations:
Differenttypesofoperationscanbeperformedonthefile.
• Create
• Delete
• Open
• Close
• Read
• Write

FILEACCESSINGMETHODS
Information is stored in files and files reside on secondary storage. When this information is to
beused, it hasto beaccessedand brought intoprimarymain memory.Informationin filescould be
accessed in many ways. There are 3 different file access methods.

 Sequentialaccessmethod
 Directaccessmethod
 Indexedsequentialaccessmethod
Sequential Access Method:
 This method is simplest among all methods. Information in the file is processed in order,
one record after the other.
 Magnetictapesaresupportingthistypeoffile accessing.
 Ex:Afileconsisting of100 records,thecurrentposition
 of read/write head is 45th record, suppose we want to read the 75th record, then it
accesssequentially from 45,46,. 74,75.
 So,theread/writehead traversealltherecordsbetween45 to75.
 Sequentialfilesaretypicallyusedin batchapplicationand payrollapplication.
Directaccess/Randomaccessmethod:
 Direct accessisalso calledasrelativeaccess.
 Inthismethod recordscanread/write randomlywithoutanyorder.
 The directaccessmethodis basedon diskmodel of a file,because disks allow random access
to any file block.
 Adirectaccessfileallowsarbitraryblocksto bereadorwritten.
 Forex.Adiskconsistingof256blocks, thecurrentpositionof
 read/write head isat 95th block. The blockto be read or write is250th block. Thenwe can
access the 250th block directly without any direction.
 Best example for direct access is a CD consisting of10 songs, at present we are listening
the song no.3, suppose we want listening the song no.9, thenwe canshift fromsong no.3 to
9 without any restriction.

Indexedsequentialaccessmethod:
 The main disadvantage in the sequential file is, it takes more time to access a record, to
overcome this problem, we are using this method.
 In this method, the records are stored sequentially for efficient processing. But they
canbe accessed directlyusing indexorkeyfield. That’s whythis method is said to bethe
indexed sequential file.
 Keysarepointerwhichcontainsaddressofvariousblocks.
 Recordsareorganizedinsequencebased onakeyfield.
 Generallyindexedfilesareusedinairlinereservationsystemandpayrollpackages
ADDRESSCALCULATIONORHASHING

Hashingisthefunctionto generate aphysicaladdressfromtherecordkeyvalue.

KeyValue Hashing Uniquephysicaladdress

Sohashingisaprocesstogeneratethephysicaladdressfromkeyvalueindirectaccessfile.

H(k)=L
WhereH->function
K->key
L->address
Characteristicsofhashing function

- Itshouldbeveryeasyandquick tocompute.

- Thehashfunctionshouldbeasfaraspossibleuniformlydistributethehashaddress throughout
the set L, so that there will be minimum no. of collision.

Typesofhash function
1) DivisionRemaindermethod
2) Midsquaremethod
3) Folding method

Division- RemainderMethod
chooseanumbermlargerthanthenumbernofkeysinK.(misusuallyeitheraprime numberor a number
without small divisor) the hash function H is defined by
H(k)=k(modm)orH(k)=k(modm)+1.
here k (mod m) denotes the reminder when kis dividedby m. the secondformulais used whenwe
want a hash address to range from 1 to m rather than 0 to m-1.
Theremainder iscalculatedphysicaladdress forthatrecord. Suppose
key value of the record is 2345.
i.e. k=2345andm= 97thenH(k)=2345 %97=17
Sothat particularrecordwillbesortedin17 locationofmemory.
Generally m should be a prime number.
MidSquaremethod
Thekeyvalueissquared.Thenthemiddigitsaretheaddressforthatkey.
Supposekeyvalue=2345 k2
= 5499025
Ifthememoryisoftwobitthen mid will be99.

FoldingMethod
Infolding methodthekeyvalue is folded into no.ofparts,whereeachpartcontainssame no.of digits
except possibly the last.
H(k) = k1+
k2+k3…..knSupposekeyvalue=23
56

k=2356 k1=23,k2=56
23+56=79
Sotheaddressforthatrecordis79.

Collision
Supposewehavecalculatedanaddress Lforagivenkeyvaluek.Butifthat locationLis previously
occupied by some other record then that situation is known as collision.
Soifthe hashfunctionwillgeneratesameaddressfortwodifferent keyvalues.Thenit is said tobe
collision.
Consider the division remainder
method.Letk1=150,k2=184, m
=17
H (k1)=150mod17=14
H (k2)=184mod17=14
Sofor keyvalues150and184thecalculatedaddress is14.Thisiscollision.

Therearevariouscollisionresolutiontechnique.
- Linearprobing( alsocalledopenaddressing orarraymethod)
- QuadraticProbing
- Doublehashing
- SeparateChaining(alsocalledclosedhashingorlinked listmethod)
Advantages
- Accessingarecordis faster.
- Noneedtoarrangetherecordonasortedorder.
- Ifrequired,recordscanbeprocessed sequentially.
- Insertion,deletioniseasier

Disadvantage
- ExpensiveI/Odeviceascomparedtosequentialfile structure.
- Addressgenerationoverheadduetohashingfunction.
- Updationofallrecordisinefficientascomparedtosequential file.
Application
Interactiveonlineapplicationsuchasairlineandrailwayreservationsystem.
References

1. BookReference:DataStructurebyS. LipschutzSchaumSeries
2. BookReference:DataStructuresUsingC
byAmiyaKumarRath(Author),AlokKumarJagadev(Author)
3. BookReference:DataStructuresUsingCbyUditAgrawal
4. https://nptel.ac.in
5. https://www.javatpoint.com
6. https://www.geeksforgeeks.org

You might also like