Answered step by step
Verified Expert Solution
Question
1 Approved Answer
CSCA48 Week 10: Worksheet Question 1: The following code is the method that is used for removing data from a heap. According to the model
CSCA48 Week 10: Worksheet Question 1: The following code is the method that is used for removing data from a heap. According to the model that was discussed in the lecture, count the number of operations that is performed to remove a node in worst case scenario. def extract min(self): f (self.is empty)) raise Empty ("Heap is enpty") nin value self. heapl0 nodeself.heap.pop len(self. heap) -1) if (len (sel. heap)0) self-heap [O] 1-node self . downheap-bubbling () eturn ain def downheap bubbling (aelf): while (self.violates (cur)): child indexself.find index (cur self. heaplcur self. heap[child indexlf. heaplchild index] self. heaplcur cuchild index def violates (self, index) Left index 21 right index2+2 violates -True f (leftlen (self. heap)): elif (rightlen (self heap): else: violatesFalse violatesself heapindex] > self. heapleft) violates(self heap[index] > self. heapleEtl) or (self. heaplindex)> self. heap[right]) return violates def find index (self, index): Left index 21 right index2+2 returred index0 if (right len (self. heap)): eli (self. heapleftself. heaplright)): else: return returned index returned index-leit returned indexleft returned index-right Question 2: After you solved question 1, prove that the time cormplexity of the this algorithm is log 2 CSCA48 Week 10: Worksheet Question 1: The following code is the method that is used for removing data from a heap. According to the model that was discussed in the lecture, count the number of operations that is performed to remove a node in worst case scenario. def extract min(self): f (self.is empty)) raise Empty ("Heap is enpty") nin value self. heapl0 nodeself.heap.pop len(self. heap) -1) if (len (sel. heap)0) self-heap [O] 1-node self . downheap-bubbling () eturn ain def downheap bubbling (aelf): while (self.violates (cur)): child indexself.find index (cur self. heaplcur self. heap[child indexlf. heaplchild index] self. heaplcur cuchild index def violates (self, index) Left index 21 right index2+2 violates -True f (leftlen (self. heap)): elif (rightlen (self heap): else: violatesFalse violatesself heapindex] > self. heapleft) violates(self heap[index] > self. heapleEtl) or (self. heaplindex)> self. heap[right]) return violates def find index (self, index): Left index 21 right index2+2 returred index0 if (right len (self. heap)): eli (self. heapleftself. heaplright)): else: return returned index returned index-leit returned indexleft returned index-right Question 2: After you solved question 1, prove that the time cormplexity of the this algorithm is log 2
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started