Professional Documents
Culture Documents
9thMarch,2015
Question1(AfternoonLab)
ImplementaBinarymin-Heapusinganarray.Eachelementofthearrayshouldbeapointertothe
followingstructure.
typedefstruct_Element{
charfirst_name[100]
charlast_name[100]
intheap_index
}Element
wherefirst_nameandlast_namearetwostringsandtheheap_indexholdsthecurrentpositionof
anelementinthearray.
Anodewillbeminimumifitsfirst_nameislexicographicallysmallerthanthatofallothernodes.In
caseoffirst_namebeingequal,thencomparisonshouldbedoneonthebasisoflast_name
(lexicographicallysmallerlast_namewouldbemin)
Note:Itisguaranteedthatintheinputboththefirst_nameandlast_namewontbesameforany2
nodes.
NoteII:
Indexingisalwaysfrom
1
.
Implementthefollowingfunctions:
InitHeap(char*first_name,char*last_name):
Createsaheapwithasingleelement
Insert(char*first_name,char*last_name):
Insertsanelementintotheheap
FindMin():
Returnsthetopelementoftheheap
DeleteMin():
Deletesthetopelementoftheheap
Delete(intindex):
Deletetheelementinindexedposition,ifitisthere
Input/Output
ThefirstlineoftheinputcontainsT,thenumberofheapoperationstobeperformed.
->InitHeap-
Followedbyapairofstrings(Spaceseparated,Firstonetoinitiatetheheap).
Nooutputrequired.
FirstoperationwillalwaysbeInitHeap,anditdneverrepeatininputfile.
->Insert-
Followedbythepairofstrings(spaceseparated)tobeaddedtoitsappropriateposition.
PrintthepositionwheretheelementwasinsertedintheminHeap.
->FindMin-
Printsthetopminimumpairofstring(first_namelast_name).
DSAMidsemLab-Test
9thMarch,2015
Iftheheapisempty,output-1.
->DeleteMin-
Deletesthetoppairofstring
Print-1iftheheapisempty.
Otherwise,printthepairofstringsatthetopnode,anddeleteit.
->Delete
Followedbyanintegerspecifyingtheindexofthenodetobedeleted.
Print-1ifnonodeexistsatthespecifiedindex.
Otherwiseprintthepairofstrings(space-separated)atthegivennode,anddeleteit.
Constraints
1Length(Strings)100
1NumberofNodes10^6
SampleInput
8
InitHeapabcdef
Insertaaafeg
Insertbcdbhg
DeleteMin
FindMin
FindMin
DeleteMin
Delete2
SampleOutput
1
3
aaafeg
abcdef
abcdef
abcdef
-1
Explanation
#1
Heap:{1:(abc,def)}
#2
Heap:{1:(aaa,feg),2:(abc,def)}
#3
Heap:{1:(aaa,feg),2:(abc,def),3:(bcd,bhg)}
#4
Heap:{1:(abc,def),2:(bcd,bhg)}
#5
Heap:{1:(abc,def),2:(bcd,bhg)}
#6
Heap:{1:(abc,def),2:(bcd,bhg)}
#7
Heap:{1:(bcd,bhg)}
#8
Heap:{1:(bcd,bhg)}