Beruflich Dokumente
Kultur Dokumente
IntroductiontoBasicDataStructuresandAlgorithms
ClassificationofDataStructures
Arrays
LinkedList
Stack
Queue
Tree
Heap
Dictionary
HashTable
Graph
References
IntroductiontoBasicDataStructuresandAlgorithms
Beforeintroducingdatastructuresweshouldunderstandthatcomputersdostore,retrieve,
andprocessalargeamountofdata.Ifthedataisstoredinwellorganizedwayonstorage
mediaandincomputer'smemorythenitcanbeaccessedquicklyforprocessingthatfurther
reducesthelatencyandtheuserisprovidedfastresponse.
Datastructureintroductionreferstoaschemefororganizingdata,orinotherwordsadata
structureisanarrangementofdataincomputer'smemoryinsuchawaythatitcouldmake
thedataquicklyavailabletotheprocessorforrequiredcalculations.Adatastructureshould
beseenasalogicalconceptthatmustaddresstwofundamentalconcerns.First,howthedata
willbestored,andsecond,whatoperationswillbeperformedonit?Asdatastructureisa
schemefordataorganizationsothefunctionaldefinitionofadatastructureshouldbe
independentofitsimplementation.Thefunctionaldefinitionofadatastructureisknownas
ADT(AbstractDataType)whichisindependentofimplementation.Theimplementationpart
isleftondeveloperswhodecidewhichtechnologybettersuitstotheirprojectneeds.
Forexample,astackADTisastructurewhichsupportsoperationssuchaspushandpop.A
stackcanbeimplementedinanumberofways,forexampleusinganarrayorusingalinked
list.
Alongwithdatastructuresintroduction,inreallife,problemsolvingisdonewithhelpofdata
structuresandalgorithms.Analgorithmisastepbystepprocesstosolveaproblem.In
programming,algorithmsareimplementedinformofmethodsorfunctionsorroutines.To
getaproblemsolvedwenotonlywantalgorithmbutalsoanefficientalgorithm.Onecriteria
ofefficiencyistimetakenbythealgorithm,anothercouldbethememoryittakesatruntime.
Sometimes,wecanhavemorethanonealgorithmforthesameproblemtoprocessadata
structure,andwehavetochoosethebestoneamongavailablealgorithms.Thisisdoneby
algorithmanalysis.Thebestalgorithmistheonewhichhasafinebalancebetweentime
takenandmemoryconsumption.But,asweknowthebestexistsrarely,andwegenerallygive
moreprioritytothetimetakenbythealgorithmratherthanthememoryitconsumes.Also,
asmemoryisgettingcheaperandcomputershavemorememorytodaythanprevioustime,
therefore,runtimeanalysisbecomesmoresignificantthanmemory.Theanalysisof
algorithmsisanentirelyseparatetopicandwewilldiscussthatseparately.
ClassificationDataStructures
Datastructurescanbebroadlyclassifiedintwocategorieslinearstructuresand
hierarchicalstructures.Arrays,linkedlists,stacks,andqueuesarelinearstructures,while
trees,graphs,heapsetc.arehierarchicalstructures.
Everydatastructurehasitsownstrengths,andweaknesses.Also,everydatastructure
speciallysuitstospecificproblemtypesdependingupontheoperationsperformedandthe
dataorganization.Forexample,anarrayissuitableforreadoperations.Followingisaquick
introductiontoimportantdatastructures.
Arrays
ArraysarestaticallyimplementeddatastructuresbysomeprogramminglanguageslikeCand
C++hencethesizeofthisdatastructuremustbeknownatcompiletimeandcannotbe
alteredatruntime.Butmodernprogramminglanguages,forexample,Javaimplements
arraysasobjectsandgivetheprogrammerawaytoalterthesizeofthematruntime.Arrays
arethemostcommondatastructureusedtostoredata.
Arraysareunarguablyeasierdatastructurestouseandaccess.Butinsertinganitemtoan
arrayanddeletingitfromthearrayaresituationdependent.Ifyouwanttoinsertanitemata
particularpositionwhichisalreadyoccupiedbysomeelementthenyouhavetoshiftallitems
onepositionrightfromthepositionnewelementhastobeinsertedtheninsertthenewitem.
Thetimetakenbyinsertoperationisdependonhowbigthearrayis,andatwhichposition
theitemisbeinginserted.Thesameistrueaboutdeletionofanitem.
IfthearrayisunsortedthensearchoperationisalsoprovedcostlyandtakesO(N)timein
worstcase,whereNissizeofthearray.Butifthearrayissortedthensearchperformanceis
improvedmagicallyandtakesO(logN)timeinworstcase.
LinkedList
Linkedlistdatastructureprovidesbettermemorymanagementthanarrays.Becauselinked
listisallocatedmemoryatruntime,so,thereisnowasteofmemory.Performancewise
linkedlistisslowerthanarraybecausethereisnodirectaccesstolinkedlistelements.
Linkedlistisprovedtobeausefuldatastructurewhenthenumberofelementstobestored
isnotknownaheadoftime.
Therearemanyflavorsoflinkedlistyouwillsee:linear,circular,doubly,anddoublycircular.
Stack
Stackisalastinfirstoutstrategydatastructurethismeansthattheelementstoredinlast
willberemovedfirst.Stackhasspecificbutveryusefulapplicationssomeofthemareas
follows:
SolvingRecursionrecursivecallsareplacedontoastack,andremovedfromthereonce
theyareprocessed.
Evaluatingpostfixexpressions
SolvingTowersofHanoi
Backtracking
Depthfirstsearch
Convertingadecimalnumberintoabinarynumber
Queue
Queueisafirstinfirstoutdatastructure.Theelementthatisaddedtothequeuedata
structurefirst,willberemovedfromthequeuefirst.Dequeue,priorityqueue,andcircular
queuearethevariantsofqueuedatastructure.Queuehasthefollowingapplicationuses:
Accesstosharedresources(e.g.,printer)
Multiprogramming
Messagequeue
Trees
Treeisahierarchicaldatastructure.Theverytopelementofatreeiscalledtherootofthe
tree.Excepttherootelementeveryelementinatreehasaparentelement,andzeroormore
childrenelements.Allelementsintheleftsubtreecomebeforetherootinsortingorder,and
allthoseintherightsubtreecomeaftertheroot.
Treeisthemostusefuldatastructurewhenyouhavehierarchicalinformationtostore.For
example,directorystructureofafilesystemtherearemanyvariantsoftreeyouwillcome
across.SomeofthemareRedblacktree,threadedbinarytree,AVLtree,etc.
Heap
Heapisabinarytreethatstoresacollectionofkeysbysatisfyingheapproperty.Maxheap
andminheaparetwoflavorsofheapdatastructure.Theheappropertyformaxheapis:each
nodeshouldbegreaterthanorequaltoeachofitschildren.While,forminheapitis:each
nodeshouldbesmallerthanorequaltoeachofitschildren.Heapdatastructureisusually
usedtoimplementpriorityqueues.
Dictionary
Dictionaryisadatastructurethatmaintainsasetofitemsindexedonbasisofkeys.
Dictionarystoresdatainformofkeyelementpairs.
HashTable
HashTableisagainadatastructurethatstoresdatainformofkeyelementpairs.Akeyisa
nonnullvaluewhichismappedtoanelement.And,theelementisaccessedonthebasisof
thekeyassociatedwithit.Hashtableisausefuldatastructureforimplementingdictionary.
Graph
Graphisanetworkeddatastructurethatconnectsacollectionofnodescalledvertices,by
connections,callededges.Anedgecanbeseenasapathorcommunicationlinkbetweentwo
nodes.Theseedgescanbeeitherdirectedorundirected.Ifapathisdirectedthenyoucan
moveinonedirectiononly,whileinanundirectedpaththemovementispossibleinboth
directions.
LastWord
Inthistutorialwesawabriefintroductionofvariousimportantdatastructures.Wealso
talkedofwhatdatastructuresare,andwhydatastructuressomuchimportantfor
informationsystems.Datastructuresalongwithalgorithmsisacoresubjectofcomputer
science.Hopeyouhaveenjoyedreadingthistutorial.Pleasedowriteusifyouhaveany
suggestion/commentorcomeacrossanyerroronthispage.Thanksforreading!
References
1. AlgorithmDesign[Foundations,Analysis,andInternetExamples]
2. Algorithms,4thEdition
3. HandbookofDataStructuresandApplications
Social
SiteLinks
Like Us on Facebook
Home
Follow Us on Twitter
C Programming
Connect Us on Google+
Java Programming
Data Structures
Digg
Web Development
Delicious
Tech Interview
RSS Feed
About Us
Stumble Upon
Contact Us
Tumblr
Sitemap
copyright 2015 cs-Fundamentals.com [By Krishan Kumar]