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

CS225 - Midterm 2 Run-Times

The document discusses runtimes of various operations on data structures like B-trees, binary search trees, and AVL trees. It includes questions about the worst-case runtime of searching a B-tree, properties of a B-tree, and the runtime of operations like insertion, deletion, searching, and traversal on different tree structures. Lisa Sproat clarifies that searching a B-tree involves both the number of disk seeks and CPU operations to search each node. The document seeks clarification on the answers to some multiple choice questions.

Uploaded by

Mihir Kumar
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)
131 views4 pages

CS225 - Midterm 2 Run-Times

The document discusses runtimes of various operations on data structures like B-trees, binary search trees, and AVL trees. It includes questions about the worst-case runtime of searching a B-tree, properties of a B-tree, and the runtime of operations like insertion, deletion, searching, and traversal on different tree structures. Lisa Sproat clarifies that searching a B-tree involves both the number of disk seeks and CPU operations to search each node. The document seeks clarification on the answers to some multiple choice questions.

Uploaded by

Mihir Kumar
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

tRuntimes

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

You might also like