Given the following data 19 23 45 18 26 10 50 48 build a ma

Given the following data:
19 , 23, 45, 18, 26 ,10, 50 ,48
build a maxheap showing all the steps. Count number of comparisons in each step to get the final runtime. Use array notation and represent your data using a table. Finally, draw the tree to validate if it is a maxheap.

I\' m having trouble with this one, can anyone help?

Solution

def max_heapify(A, i):
    m = 0
    left = 2*i + 1
    m += 1
    right = 2*i + 2
    m += 1
    largest = i
    m += 1

    m += 1
    if left<len(A) and A[left]>A[largest]:
        largest = left
        m += 1

    m += 1
    if right < len(A) and A[right] > A[largest]:
        largest = right
        m += 1
    m += 1
    if largest != i:
        A[i], A[largest] = A[largest], A[i]
        m += 1
        max_heapify(A, largest)
    return m
def build_max_heap(A):
    n = 0
    for i in range(len(A)):
        m = max_heapify(A, i)
        n += m
    return A, n

A = [19, 23, 45, 18, 26, 10, 50, 48]
A, n = build_max_heap(A)
print \"Running Time:\", n

Given the following data: 19 , 23, 45, 18, 26 ,10, 50 ,48 build a maxheap showing all the steps. Count number of comparisons in each step to get the final runti

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site