**Part which is working!! # First let us complete a minheap data structure. # Please complete missing parts below. class

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

**Part which is working!! # First let us complete a minheap data structure. # Please complete missing parts below. class

Post by answerhappygod »

Part Which Is Working First Let Us Complete A Minheap Data Structure Please Complete Missing Parts Below Class 1
Part Which Is Working First Let Us Complete A Minheap Data Structure Please Complete Missing Parts Below Class 1 (86.47 KiB) Viewed 52 times
Part Which Is Working First Let Us Complete A Minheap Data Structure Please Complete Missing Parts Below Class 2
Part Which Is Working First Let Us Complete A Minheap Data Structure Please Complete Missing Parts Below Class 2 (157.35 KiB) Viewed 52 times
Part Which Is Working First Let Us Complete A Minheap Data Structure Please Complete Missing Parts Below Class 3
Part Which Is Working First Let Us Complete A Minheap Data Structure Please Complete Missing Parts Below Class 3 (108.61 KiB) Viewed 52 times
Part Which Is Working First Let Us Complete A Minheap Data Structure Please Complete Missing Parts Below Class 4
Part Which Is Working First Let Us Complete A Minheap Data Structure Please Complete Missing Parts Below Class 4 (108.39 KiB) Viewed 52 times
Part Which Is Working First Let Us Complete A Minheap Data Structure Please Complete Missing Parts Below Class 5
Part Which Is Working First Let Us Complete A Minheap Data Structure Please Complete Missing Parts Below Class 5 (101.17 KiB) Viewed 52 times
Part Which Is Working First Let Us Complete A Minheap Data Structure Please Complete Missing Parts Below Class 6
Part Which Is Working First Let Us Complete A Minheap Data Structure Please Complete Missing Parts Below Class 6 (90.66 KiB) Viewed 52 times
Part Which Is Working First Let Us Complete A Minheap Data Structure Please Complete Missing Parts Below Class 7
Part Which Is Working First Let Us Complete A Minheap Data Structure Please Complete Missing Parts Below Class 7 (75.59 KiB) Viewed 52 times
Part Which Is Working First Let Us Complete A Minheap Data Structure Please Complete Missing Parts Below Class 8
Part Which Is Working First Let Us Complete A Minheap Data Structure Please Complete Missing Parts Below Class 8 (100.16 KiB) Viewed 52 times
Part Which Is Working First Let Us Complete A Minheap Data Structure Please Complete Missing Parts Below Class 9
Part Which Is Working First Let Us Complete A Minheap Data Structure Please Complete Missing Parts Below Class 9 (113.62 KiB) Viewed 52 times
Part Which Is Working First Let Us Complete A Minheap Data Structure Please Complete Missing Parts Below Class 10
Part Which Is Working First Let Us Complete A Minheap Data Structure Please Complete Missing Parts Below Class 10 (94.25 KiB) Viewed 52 times
Part Which Is Working First Let Us Complete A Minheap Data Structure Please Complete Missing Parts Below Class 11
Part Which Is Working First Let Us Complete A Minheap Data Structure Please Complete Missing Parts Below Class 11 (116.07 KiB) Viewed 52 times
Part Which Is Working First Let Us Complete A Minheap Data Structure Please Complete Missing Parts Below Class 12
Part Which Is Working First Let Us Complete A Minheap Data Structure Please Complete Missing Parts Below Class 12 (118.7 KiB) Viewed 52 times
Part Which Is Working First Let Us Complete A Minheap Data Structure Please Complete Missing Parts Below Class 13
Part Which Is Working First Let Us Complete A Minheap Data Structure Please Complete Missing Parts Below Class 13 (135.66 KiB) Viewed 52 times
**Part which is working!! # First let us complete a minheap data structure. # Please complete missing parts below. class MinHeap: def _init__(self): self.H= [None] def size (self): return len(self.H)-1 def repr_(self): return str(self.H[1:]) def satisfies_assertions (self): for i in range (2, len(self.H)): assert self.H = self.H[i//21, f'Min heap property fails at position {i//2}, parent elt: {self.H[i//2]}, child elt: {self.H}'| def min_element(self): return self.H[1] ## bubble_up function at index ## WARNING: this function has been cut and paste for the next problem as well def bubble_up(self, index): assert index >= 1 if index = 1: return parent_index = index // 2 if self.H[parent_index] < self.H[index]: else: return self.H[parent_index], self.H[index] = self.H[index], self.H[parent_index] self.bubble_up(parent_index) ## bubble down function at index ## WARNING: this function has been cut and paste for the next problem as well def bubble_down (self, index): assert index » 1 and index < len(self.H) lchild_index = 2 * index rchild_index = 2 * index + 1 # set up the value of left child to the element at that index if valid. or else make it +Infinity
rcnila_index = 2 * index + # set up the value of left child to the element at that index if valid, or else make it +Infinity lchild_value = self.H[lchild_index] if lchild_index < len(self.H) else float('inf') 2 # set up the value of right child to the element at that index if valid, or else make it +Infinity rchild_value = self.H[rchild_index] if rchild_index < len(self.H) else float('inf') # If the value at the index is lessthan or equal to the minimum of two children, then nothing else to do if self.H[index] < min(lchild_value, rchild_value): return # Otherwise, find the index and value of the smaller of the two children. # A useful python trick is to compare min_child_value, min_child_index = min ((lchild_value, lchild_index), (rchild_value, rchild_index)) # Swap the current index with the least of its two children self.H[index], self.H[min_child_index] = self.H[min_child_index], self. H[index] # Bubble down on the minimum child index self.bubble_down (min_child_index) # Function: heap_insert # Insert elt into heap # Use bubble_up/bubble_down function def insert (self, elt): # your code here self.H= self.H+ [elt] self.bubble_up(len(self.H) - 1) # Function: heap_delete_min # delete the smallest element in the heap. Use bubble_up/bubble_down def delete_min(self): # your code here self.H=[self.H for i in range (1, len(self.H))] for i in range(1, len(self.H)): self.bubble_up(i) h = MinHeap () 74 print('Inserting: 5, 2, 4, -1 and 7 in that order.') 75 h.insert (5) 76 print (f'\t Heap = {h}') assert (h.min_element() == 5) h.insert (2) 78 79 print (f'\t Heap = {h}') 80 assert (h.min_element() == 2) h.insert (4) 81 82 print (f'\t Heap = {h}')
81 h.insert (4) 82 print (f'\t Heap = {h}') 83 assert(h.min_element() == 2) 84 h.insert(-1) 85 print (f'\t Heap = {h}') 86 assert (h.min_element() == -1) 87 h.insert (7) 88 print (f'\t Heap = {h}') 89 assert (h.min_element() == -1) 90 h.satisfies_assertions() 23 92 print('Deleting minimum element') 93 h.delete_min() 20 94 print (f'\t Heap = {h}') asser assert(h.min_element() == 2) h.delete_min() 99 95 96 2 97 print (f'\t Heap = {h}') 20 98 006 delete mini h.delete_min() print/FITE HOS print (f'\t 99 100 100 100 princ 101 102 102 10 103 You 104 assert(h.min_element() == 7) 20 105 # Test delete_max on heap of size 1, should result in empty heap. 106 h.delete_min() 107 print (f'\t Heap = {h}') 108 print('All tests passed: 10 points!') 109 110 class TopkHeap: 111 A 112 113 20 114 115 20 116 110 117 118 119 120 121 433 mi assert(h.min_element() == 4) Heap = {h}') heal assert (h.min_element() == 5) adet h.delete_min() Hel print (f'\t Heap = {h}') may # The constructor of the class to initialize an empty data structure def __init__(self, k): self.k = k self. A = [] self.H = MinHeap () def size (self): return len(self.A) + (self.H.size()) def get_jth_element(self, j): E LA ----
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 def get_jth_element(self, j): assert 0 <=j< self.k-1 assert j < self.size() return self.A[j] def satisfies_assertions(self): # is self.A sorted for i in range(len(self.A) -1 ): assert self.A <= self.A[i+1], f'Array A fails to be sorted at position {i}, {self.A, self.A[i+1]}' # is self.H a heap (check min-heap property) self.H.satisfies_assertions() # is every element of self.A less than or equal to each element of self.H for i in range (len(self.A)): assert self.A = self.H.min_element(), f'Array element A[{i}] = {self.A} is larger than min heap element {self.H.min_element()}' # Function: insert_into_A # This is a helper function that inserts an element elt' into `self.A`. #whenever size is 1
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 def bubble_up(self, index): # your code here assert index >= 1 if index == 1: return parent_index = index // 2 if self.H[parent_index] > self.H[index]: else: return self.H[parent_index], self.H[index] = self.H[index], self.H[parent_index] self.bubble_up(parent_index) def bubble_down (self, index): # your code here left = index * 2 right = index largest index 2 + 1 if len(self.H)> left and self.H[largest] < self.H[left]: largest = left if len(self.H) > right and self.H[largest] < self.H[right]: largest = right if largest != index: self._swap(index, largest) self.bubble_down (largest) # Function: insert # Insert elt into minheap # Use bubble_up/bubble_down function def insert(self, elt): # your code here self.H= self.H+ [elt] self.bubble_up (len(self.H) - 1) # Function: delete_max # delete the largest element in the heap. Use bubble_up/bubble_down def delete_max(self): #your code here self.H=[self.H for i in range(1, len(self.H))] for i in range(1, len(self.H)); __12 L..LLY- ---FIN
317 318 for i in range(1, len(self.H)): self.bubble_up(i) 319 320 h = MaxHeap() 321 print('Inserting: 5, 2, 4, -1 and 7 in that order.') 322 h.insert (5) 222 323 print (f'\t Heap = {h}') 224 324 assert(h.max_element() = 5) 325 h.insert (2) 225 326 print (f'\t Heap = {h}') 327 assert(h.max_element() = 5) 328 h.insert (4) 329 print (f'\t Heap = {h}') 330 assert(h.max_element() = 5) h.insert(-1) 331 332 print (f'\t Heap = {h}') 333 assert(h.max_element() = 5) 334 h.insert (7) 335 print (f'\t Heap = {h}') 336 assert(h.max_element() = 7) h.satisfies_assertions() 337 338 229 339 print('Deleting maximum element') 340 h.delete_max() 341 print (f'\t Heap = {h}') 342 assert(h.max_element() = 5) 343 h.delete_max() 344 print (f'\t Heap = {h}') 345 assert(h.max_element() == 4) 346 h.delete_max() 347 print (f'\t Heap = {h}') 348 assert(h.max_element() = 2) 349 h.delete_max() 350 print (f'\t Heap = {h}') 351 assert(h.max_element() = -1) 352 352 # Test delete_max on heap of size 1, should result in empty heap. 353 h.delete_max() 354 print (f'\t Heap = {h}') 355 356 print('All tests passed: 5 points!') 357 **Part that needs attention!!** 358
336 357 **Part that needs attention!!** 358 359 class MedianMaintainingHeap: 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 definit__(self): self.hmin MinHeap() self.hmax = MaxHeap() def satisfies_assertions(self): if self.hmin.size() == 0: assert self.hmax.size() = 0 return if self.hmax.size() = 0: assert self.hmin.size() = 1 return # 1. min heap min element > max heap max element assert self.hmax.max_element() <= self.hmin.min_element(), f'Failed: Max element of max heap = {self.hmax.max_element()) > min element of min heap (self.hmin.min #2. size of max heap must be equal or one less than min heap. s_min= self.hmin.size() s_max = self.hmax.size() assert (s_min=s_max or s_max = s_min −1 ), f'Heap sizes are unbalanced. Min heap size = {s_min) and Maxheap size = {s_max}' -1 def _repr_(self): return Maxheap:' + str(self.hmax) + ' Minheap: '+str(self.hmin) def get_median (self): if self.hmin.size() == 0: assert self.hmax.size() = 0, 'Sizes are not balanced' assert False, 'Cannot ask for median from empty heaps' if self.hmax.size() == 0: assert self.hmin.size() = 1, 'Sizes are not balanced' return self.hmin.min_element() #your code here if self.hmax.size()==self.hmin.size(): else: print (median) return median median = (self.hmax.max_element()+self.hmin.min_element())/2 median =self.hmin.min_element() # function: balance_heap_sizes # ensure that the size of hmax = size of hmin or size of hmax +1 == size of hmin
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 # function: balance_heap_sizes # ensure that the size of hmax = size of hmin or size of hmax +1 = size of hmin # If the condition above does not hold, move the max element from max heap into the min heap or # vice versa as needed to balance the sizes. # This function could be called from insert/delete_median methods def balance_heap_sizes (self): #your code here if (self.hmin.size() >self.hmax.size()+1): self.hmax.insert (self.hmin.min_element()) self.hmin.delete_min() self.hmax.bubble_down(self.hmax.size()) return elif (self.hmin.size() self.hmin.min_element(): # Element needs to go into the min heap current_min= self.hmin.min_element() # done! self.hmin.delete_min() self.hmin.insert(elt) self.hmax.insert(current_min) else: # Element goes into the max heap just insert it there. self.hmax.insert(elt) return # Now assume both heaps are non-empty dan anda hana
435 436 437 450 438 439 439 440 440 441 404 442 442 443 449 444 445 449 446 4mg 447 448 4440 return # Now assume both heaps are non-empty # your code here if (elt>self.hmin.min_element()): self.hmin.insert (elt) else: self.hmax.insert(elt) self.balance_heap_sizes () return def delete_median (self): self.hmax.delete_max() self.balance_heap_sizes () 449 449 m = MedianMaintainingHeap() 450 print('Inserting 1, 5, 2, 4, 18, -4, 7, 9¹) 451 491 452 m.insert (1) 492 453 499 print (m) 454 print(m.get_median()) 455 m. satisfies_assertions() 456 assert m.get_median() == 1, f'expected median = 1, your code returned {m.get_median()}' 457 720 458 m.insert (5) 1200 459 print (m) 700 460 print(m.get_median()) 461 401 m. satisfies_assertions() 462 assert m.get_median() == 3, f'expected median = 3.0, your code returned {m.get_median()}" 463 464 m.insert (2) 465 405 print (m) 466 print(m.get_median()) 467 m.satisfies_assertions() 400 468 469 assert m.get_median() == 2, f'expected median = 2, your code returned {m.get_median ()}' 470 m.insert (4) 471 print (m) 472 print (m.get_median()) 473 m. satisfies_assertions () 474 assert m.get_median () == 3, f'expected median = 3, your code returned {m.get_median()}' 475 476 m.insert (18)
460 print(m.get_median()) 461 m.satisfies_assertions() 462 assert m.get_median() = 3, f'expected median = 3.0, your code returned {m.get_median()}' 463 YOU 464 m.insert(2) 465 print (m) 409 466 print(m.get_median()) 467 m.satisfies_assertions() 468 400 469 assert m.get_median() == 2, f'expected median = 2, your code returned {m.get_median ()}' 470 m.insert (4) 20 471 print (m) 472 print (m.get_median()) 473 m.satisfies_assertions() assert m.get_median () == 3, f'expected median = 3, your code returned {m.get_median()}' 474 424 475 475 476 477 477 m.insert (18) print (m) 478 print(m.get_median()) 479 m. satisfies_assertions() 480 assert m.get_median() == 4, f'expected median = 4, your code returned {m.get_median()}'| 481 481 482 m.insert(-4) 482 483 483 print (m) 484 print (m.get_median()) 485 m. satisfies_assertions() 486 assert m.get_median () == 3, f'expected median = 3, your code returned {m.get_median()}' 487 488 m.insert (7) print (m) 489 490 print(m.get_median()) 491 m. satisfies_assertions() 490 492 492 assert m.get_median() == 4, f'expected median = 4, your code returned {m.get_median()}' 493 495 494 m.insert (9) 495 print (m) 496 print (m.get_median()) 497 m.satisfies_assertions() 498 assert m.get_median()= 4.5, f'expected median = 4.5, your code returned {m.get_median()}' 499 500 print('All tests passed: 15 points') 501
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply