Question
The following are the functions for max heap using a Binary tree. def get_parent(i): if i % 2 == 1: # j = i//2 #odd
The following are the functions for max heap using a Binary tree.
def get_parent(i): if i % 2 == 1: # j = i//2 #odd equation else: j = i//2 - 1 return j
def get_children(i): left_child = i*2 + 1 right_child = i*2 + 2 return left_child, right_child
def insert_heap(heap, new_number ): heap.append(new_number) last_index = len(heap)-1 heap = heapify(heap, last_index) return heap
def heapify(heap, index): if index <= 0: return heap my_value = heap[index] parent_index = get_parent(index) parent_value = heap[parent_index] print(my_value, parent_value, index, parent_index) if my_value >= parent_value: heap[index], heap[parent_index] = parent_value, my_value else: #finish return heap heap = heapify(heap,parent_index ) return heap
Assuming we have the following list of numbers: >>> list_of_numbers = [48, 24, 37, 25, 38, 16, 50]
The heap would be created as follows: heap=[] for new_number in random_numbers: heap = insert_heap(heap, new_number)
The output would be : >>>heap [50, 38, 48, 24, 25, 16, 37]
Create a three-way heap (Modify the above functions) so that when creating the heap you would get the following: >>> heap [50, 48, 37, 25, 24, 16, 38]
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