Sunteți pe pagina 1din 4

DSAMidsemLab-Test

9thMarch,2015

Question1,Morninglab

ThetaskistoimplementaBinarySearchTreeofstrings,supportedbyfewoperations.

Thebasicstructuretobeusedisthefollowing:

typedefstruct_BST{
charname[100]
struct_BST*left
struct_BST*right
struct_BST*parent//Thisisoptionaltoinclude.Uptoyou.
}BST

Thefunctionstobesupportedare:

InitTree(char*name):
Createsatree,andreturnsthepointertothetree.
Insert(char*name,BST**T):
InsertsanodeinthetreeT.Ifthetreeisempty(TisNULL,
thismighthappenonenoughdeletions),thenyourfunctionshouldinternallycallInitTree
(name).
Delete(char*name,BST**T):
Deletesthenodewiththegivennameifitispresent,or
printsNotFoundiftreeisemptyorifnodeisnotpresent.
Find(char*name,BST*T):
ReturnswhetheranodewiththegivennameispresentinTor
not.Printits
InOrderposition
ifpresent,orprintNotFoundotherwise.
Height(BST*T):
ComputestheheightofT
PrintTree(BST*T,intorder):
Printsthenodesinthetreeinin-ordermanneriforder=0,
pre-ordermanneriforder=-1,andpost-order(1)otherwise(Stringsofnodestobeprinted
space-separated)

Input/Output
ThefirstlineoftheinputcontainsT,thenumberoftreeoperationstobeperformed.
EachoftheFollowingTlineswillcontainoneofthefollowing:

->InitTree-
Followedbyastring(firststringtoinitiatethetree)onthesamelineseparatedbyaspace.
PrintNothing.InitTreewouldonlybegivenasinputasthefirsttestcase,andnotanytime
again.
->Insert-
Followedbyastringtobeaddedtoitsappropriateposition.
Printits
InOrderPosition
.
->Delete-
FollowedbyastringwhosecorrespondingnodeneedstobedeletedinaninOrderfashion.

DSAMidsemLab-Test
9thMarch,2015
PrintNotFoundifnosuchelementexistsorifthetreeisempty.Iffound,thenprintits
InOrderPosition
anddeleteit.
->Find-
FollowedbyastringwhosecorrespondingnodeistobesearchedinaninOrderFashion.
Printits
InOrderPosition
ifnodewithgivenstringexistsintree,orprintNotFound
otherwise.
->Height-
Printstheheight/depthofthetree.
(Notethatheightofanemptytreeis0,whilethatofatreecontainingjusttherootis1)
->PrintTree-
Followedbyanintegeri.
PrintsthetreeelementsinaPREORDERsingle-spaceseparatedfashionif
i=-1
PrintsthetreeelementsinanINORDERsingle-spaceseparatedfashion.if
i=0
PrintsthetreeelementsinaPOSTORDERsingle-spaceseparatedfashion.if
i=1
Ifthetreeisempty,blanklinewouldbeprinted.

Note
:InOrderPositionreferstotheindexofthenodewhentraversedintheinordertraversal
(indexstartsfrom1andnot0).

Constraints
1Length(String)100
1NumberofNodes10^4

SampleInput
13
InitTreef
Inserta
Insertb
Deleteg
Findm
Findb
Insertg
Insertm
Insertk
Height
PrintTree-1
PrintTree0
PrintTree1

SampleOutput:
1
2
NotFound

DSAMidsemLab-Test
9thMarch,2015
NotFound
2
4
5
5
4
fabgmk
abfgkm
bakmgf

Explanation:
#1
InitiatestheTreewithastringf
#2
InsertsanewStringa.Treelookslike:
f
/
a
#3
InsertsanewStringb.Treelookslike:
f
/
a
\
b
#4
Cannotfindg
#5
Cannotfindm
#6
InOrderofTreeLookslike:abf.Findsindexofb.
#7
InsertsanewStringg.Treelookslike
f
/\
ag
\
b
#8
InsertsanewStringm.Treelookslike
f
/\
ag
\\
bm
#9
InsertsanewStringk.Treelookslike
f
/\
ag
\\
bm

DSAMidsemLab-Test
9thMarch,2015
/
k
#10
Heightoftreeis:4
#11
PreOrder:fabgmk
#12
InOrder:abfgkm
#13
PostOrder:bakmfg

Assumptions:
FirstoperationwillalwaysbeinitTree.

S-ar putea să vă placă și