Professional Documents
Culture Documents
CS225 - Midterm 2 Run-Times
CS225 - Midterm 2 Run-Times
PLSFIXALLMYMISTAKESthanks.
WhatsthedifferencebetweenCPUoperationsanddiskseeks???
Adiskseek"takesup"oneCPUoperation(sortof)fornumberofCPUoperationstosearchthetree,you'dwantthe
numberofdiskseeksandthenumberofoperationsrequiredtosearchthenode.LisaSproatonPiazza
FA7MC5
MC5(2.5pts)SupposeanordermBtreecontainsnitems.Intheworstcase,howmanyCPUoperations
wouldberequiredtosearchthetreeforaspecifickey?
(a)O(log2n)
(b)O(logmn)
(c)O(mlog2n)
(d)O(mlog2m)
(e)O(mlogmn)
whatistheanswerforFA7MC5?
FA10MC8
WhichofthefollowingstatementsistrueforaBtreeofordermcontainingnitems?
(i)TheheightoftheBtreeisO(logmn)andthisboundsthetotalnumberofdiskseeks.
(ii)Anodecontainsamaximumofm1keys,andthisboundsthenumberofdiskseeksateachlevelofthe
tree.
(iii)Anorder2BtreeisalsoaBinarySearchTree.Makeoneofthefollowingchoices.
(a)Onlyitem(i)istrue.
(b)Onlyitem(ii)istrue.
(c)Onlyitem(iii)istrue.
(d)Twooftheabovechoicesaretrue.
(e)Allchoices(i),(ii),and(iii)aretrue.
SP14
(MC7)
1(AVL)
HowmanydatastructuresinthislistcanusedtoimplementaDictionarysothatallofits
functionshavestrictlybetterthanO(n)runningtime(worstcase)?:linkedlist,stack,queue,binarysearchtree,
AVLtreeIthinkitsonlyAVLbecauseworstcaseforBSTwouldbeO(n)<Iconcur
agree
(MC15)
O(1)
TheslowerofEnqueueorDequeueforaQueueimplementedwithanarray.
Constanttimetochangetheintegersthatrefertowheretheheadis,etc.
(MC16)
O(n)
FindthemaximumkeyinaBinaryTree(notnecessarilyBST).
Couldbeatreeinincreasingorderwhichwouldhavetotraverseentiretree
(MC17)
O(n)
FindtheInOrderPredecessorofagivenkeyinaBinaryTree(ifitexists).
Notbalancedsoheightcouldbeninworstcase,soO(n)
(MC18)
O(logn)
FindtheInOrderPredecessorofagivenkeyinanAVLTree(ifitexists).
Worstcaseistraversingtheheightofthetreewhichislogninthiscase
(MC19)
O(1)
PerformrightLeftRotatearoundagivennodeinanAVLTree.
RotationsareO(1),correctingtheheightimbalanceduetoremovalO(logn)butthisonlyasks
fordoingtherotationMYBADitwas2am.
(MC20)
O(n)
DetermineifagivenBinarySearchTreeisheightbalanced.
Traverserecursivelywhereheightofanodeismax(left,right)+1.
(MC21)
O(n^2)
Buildabinarysearchtree(notAVL)withkeysthatarethenumbersbetween0andn,inthat
order,byrepeatedinsertionsintothetree.
Considerinsertinginincreasingorderordecreasingorder
Cansomeoneclarify?
^
ithinkthisquestionmeans
while(inti=0i<=ni++)
tree.insert(i)
Eachtimeyouinsertyouhavetotraversedownthetreeagain.Sincedoingninserts,youare
traversingthetree(anO(n)process)ntimes.O(n^2)
(MC22)
O(n)
RemovetherightsubtreefromtherootofanAVLtree,andrestoretheheightbalanceofthe
structure.
O(n+logn)=O(n)toremoverightsubtree,logntorestoreheightbalance
ToremoveeachnodeintherightsubtreeyouhavetohaveseenitwhichisO(n/2),thenyouhave
areallyunbalancedtree,soyoucorrectthatwithO(logn)rotations.SinceO(n/2)=O(n)andO(n)+O(logn)=
O(n).itisO(n)
JustlikethetreedestructortraversesthetreewhichisO(n)
Sp12
(MC11)
O(1)
EnqueueforaQueueimplementedwithanarray.
O(1)timetochangewhatreferstothetailofthequeue,whichisusuallyanintegertotheindex
inthiscase
(MC12)
O(1)
PopforaStackimplementedwithanarray.
O(1)sameasabove
(MC13)
O(n)
FindakeyinaBinaryTree(notnecessarilyBST).
SearchingatreeinsertedinincreasingorderwouldbeO(n)time<worstcase
(MC14)
O(logn)
RemovetherootofabalancedBinarySearchTree.
HavetoreplacewithinorderpredecessorwhichisO(logn)tofindtheIOP.
(MC15)
O(n)
FindthelargestkeyinaBinarySearchTree.
O(n)foranonbalancedtreeasyoumayhavetotraverseeverynode
(MC16)
O(logn)
FindthelargestkeyinanAVLTree.
FindingthekeyisatmaxO(h)whereh=lognforabalancedtree.
(MC17)
O(n)
ForeachnodeinaBinarySearchTree,computethelengthofthelongestpathfromthenode
toaleaf.
Recursivecalltotraversetreeandcomputepathfromleafs
(MC18)
O(n)
MakeacopyofanAVLtree.
Havetotraverseeverynode
(MC19)
O(n)
DetermineiftwogivenBinarySearchTreesarecopiesofoneanother.
TraversebothtreessimultaneouslybothO(n).
(MC20)
O(n)
RemoveallthenodesintherightsubtreeofanonemptyAVLtree.
Deleteeverynodeinrightsubtree=O(n/2)=O(n)
FA10
(MC9)
O(1)
EnqueueforaQueueimplementedwithaSinglyLinkedListwithatailpointer,wherethefrontof
thequeueisattheend(tail)ofthelist.(Enqueueoccursatthehead.)
O(1)forenqueuer,asyoumakethenewnodepointtotheprevioushead,andassignheadto
newnode.
(MC10)
O(n)
DequeueforaQueueimplementedwithaSinglyLinkedListwithatailpointer,wherethefront
ofthequeueisattheend(tail)ofthelist.(Enqueueoccursatthehead.)
O(n)asyouneedthenodebeforethetail,whichyouhavetotraversethelistfor.
(MC11)
O(n)
WorstcaseforfindingagivenkeyinaBinarySearchTree.
O(n)asthetreecouldbeinincreasingorderinworstcase.
(MC12)
O(logn)
WorstcaseforinsertingasinglekeyintoanAVLTree.
O(logn)asheightoftreeismaxlogn
(MC13)
O(n)
Worstcaseforanalgorithmtofindthesmallestkeythatiswithinfixeddistancedfromagiven
keyinaBinarySearchTree(ifsuchakeyexists).
O(n)asitisatmaxnaway.Wedonotknowd
ThisshouldreallybeO(d),andsincedisafixedconstant,IbelievethisisO(1).
Westillneedtofindtheoriginalnodewiththegivenkey.Theworstcasetofindthenodeto
startoursearchisO(n).
(MC14)
O(logn)
Worstcaseforanalgorithmtofindthesmallestkeythatiswithinfixeddistancedfroma
givenkeyinanAVLTree(ifsuchakeyexists).
O(logn)asitisatmaxnaway.Sameasabove
(MC15)
O(n)
GivenaBinaryTree,checktoseeifitisaBinarySearchTree.
CheckbyinordertraversalwhichisO(n),inordertraversalshouldbeinincreasingorder.
(MC16)
O(n)
RemoveallthenodesfromanAVLTree(asisdonebythedestructor).
O(n)asyouhavetodeleteeachnodebytraversethroughtheentiretree(postorder)
(MC17)
O(n)
ComputetheheightofeverysubtreeinaBinarySearchTree.
O(n)asyoucomputerecursivelyfrombottomup.
(MC18)
O(n)
GivenaBinarySearchTreewhosesubtreeheightshavebeencomputeddetermineifthe
BinarySearchTreeisanAVLTree.
O(n)asyouhavetocheckbalanceoneverynode.
FA09
(MC9)
O(1)
EnqueueforaQueueimplementedwithanarray.
(MC10)
O(1)
DequeueforaQueueimplementedwithanarray.
(MC11)
O(n)
WorstcaseforinsertionintoaBinarySearchTree.
(MC12)
O(n)
WorstcaseforremovalfromaBinarySearchTree.
(MC13)
O(n)
Worstcaseforanalgorithmtoreturnallkeysthataregreaterthan20andthataremultiplesof
3inaBinarySearchTree.
(MC14)
O(logn)
WorstcaseforinsertionintoanAVLTree.
(MC15)
O(n)
Worstcaseforanalgorithmtoreturnallkeysthataregreaterthan20andthataremultiplesof
3inanAVLTree.
(MC16)
O(n)
LevelordertraversalofanAVLTree.
Hiteverynode
(MC17)
O(nlogn)
BuildanAVLtreewithkeysthatarethenumbersbetween0andn,inthatorder,by
repeatedinsertionintothetree.
O(nlogn),logntimeforeachinsertion,ninsertionsn*logn
(MC18)
O(n^2)
Buildabinarysearchtreewithkeysthatarethenumbersbetween0andn,inthatorder,by
repeatedinsertionintothetree.
O(n^2),O(n)timeforeachinsertion,ninsertions=n*n=n^2
Solidmusic
http://play.spotify.com/album/5AMOKSM1ftb3opIbGT2d4q
https://play.spotify.com/album/7bXOViTvx6EHXuYFuI2yfj
https://www.youtube.com/watch?v=1ifJkquRzlo
<ignoreruntimes,listentoFrightenedRabbit
https://www.youtube.com/watch?v=I9xOmeSQrQo
somestayingupmusic
https://www.youtube.com/watch?v=kffacxfA7G4
anotherstayingupmusic//awhhellyeah
https://www.youtube.com/watch?v=muAt4evb7Sk
latelatelatenightmusic
checkoutmysoundcloudbruh:
https://soundcloud.com/mickmarchan/lordof420macintoshplusdeathgrips
https://www.youtube.com/watch?v=gZHjRQjbHrE
uhhuh,thismyshit
https://www.youtube.com/watch?v=NWD7iqtOJSE
<ourTAsrightnow