0% found this document useful (0 votes)
62 views4 pages

Question 1, Morning Lab

The document describes a midterm lab test involving implementing functions for a binary search tree of strings. The functions include initializing the tree, inserting nodes, deleting nodes, finding nodes, calculating height, and printing the tree in different orders. Sample input and output is provided showing a series of operations on a binary search tree and the expected outputs.

Uploaded by

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

Question 1, Morning Lab

The document describes a midterm lab test involving implementing functions for a binary search tree of strings. The functions include initializing the tree, inserting nodes, deleting nodes, finding nodes, calculating height, and printing the tree in different orders. Sample input and output is provided showing a series of operations on a binary search tree and the expected outputs.

Uploaded by

Gaurav Jain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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.

You might also like