Saturday, July 30, 2016

if array is in heap property

bool isminheap(a):
    if a == null:
        throw
    lasti = a.length() -1
    lastparent = (lasti-1) /2
    for i = 0, i <= lastparent, i++:
        left  = i*2 +1
        right = left +1
        // left is never out of range
        if left < a.length()  && a[i] > a[left]:
            return false
        // right may be out of range
        if right < a.length() && a[i] > a[right]:
            return false
    return true

No comments:

Post a Comment