6-2 Analysis of dd-ary heaps

A dd-ary heap is like a binary heap, but (with one possible exception) non-leaf nodes have dd children instead of 22 children.

a. How would you represent a dd-ary heap in an array?

b. What is the height of a dd-ary heap of nn elements in terms of nn and dd?

c. Give an efficient implementation of EXTRACT-MAX\text{EXTRACT-MAX} in a dd-ary max-heap. Analyze its running time in terms of dd and nn.

d. Give an efficient implementation of INSERT\text{INSERT} in a dd-ary max-heap. Analyze its running time in terms of dd and nn.

e. Give an efficient implementation of INCREASE-KEY(A,i,k)\text{INCREASE-KEY}(A, i, k), which flags an error if k<A[i]k < A[i], but otherwise sets A[i]=kA[i] = k and then updates the dd-ary max-heap structure appropriately. Analyze its running time in terms of dd and nn.

a. We can use those two following functions to retrieve parent of ii-th element and jj-th child of ii-th element.

d-ARY-PARENT(i)
    return floor((i - 2) / d + 1)
d-ARY-CHILD(i, j)
    return d(i − 1) + j + 1

Obviously 1jd1 \le j \le d. You can verify those functions checking that

d-ARY-PARENT(d-ARY-CHILD(i,j))=i.d\text{-ARY-PARENT}(d\text{-ARY-CHILD}(i, j)) = i.

Also easy to see is that binary heap is special type of dd-ary heap where d=2d = 2, if you substitute dd with 22, then you will see that they match functions PARENT\text{PARENT}, LEFT\text{LEFT} and RIGHT\text{RIGHT} mentioned in book.

b. Since each node has dd children, the height of a dd-ary heap with nn nodes is Θ(logdn)\Theta(\log_d n).

c. d-ARY-HEAP-EXTRACT-MAX(A)d\text{-ARY-HEAP-EXTRACT-MAX}(A) consists of constant time operations, followed by a call to d-ARY-MAX-HEAPIFY(A,i)d\text{-ARY-MAX-HEAPIFY}(A, i).

The number of times this recursively calls itself is bounded by the height of the dd-ary heap, so the running time is O(dlogdn)O(d\log_d n).

d-ARY-HEAP-EXTRACT-MAX(A)
    if A.heap-size < 1
        error "heap under flow"
    max = A[1]
    A[1] = A[A.heap-size]
    A.heap-size = A.heap-size - 1
    d-ARY-MAX-HEAPIFY(A, 1)
    return max
d-ARY-MAX-HEAPIFY(A, i)
    largest = i
    for k = 1 to d
        if d-ARY-CHILD(k, i) ≤ A.heap-size and A[d-ARY-CHILD(k, i)] > A[i]
            if A[d-ARY-CHILD(k, i)] > largest
                largest = A[d-ARY-CHILD(k, i)]
    if largest != i
        exchange A[i] with A[largest]
        d-ARY-MAX-HEAPIFY(A, largest)

d. The runtime is O(logdn)O(\log_d n) since the while loop runs at most as many times as the height of the dd-ary array.

d-ARY-MAX-HEAP-INSERT(A, key)
    A.heap-size = A.heap-size + 1
    A[A.heap-size] = key
    i = A.heap-size
    while i > 1 and A[d-ARY-PARENT(i) < A[i]]
        exchange A[i] with A[d-ARY-PARENT(i)]
        i = d-ARY-PARENT(i)

e. The runtime is O(logdn)O(\log_d n) since the while loop runs at most as many times as the height of the dd-ary array.

d-ARY-INCREASE-KEY(A, i, key)
    if key < A[i]
        error "new key is smaller than current key"
    A[i] = key
    while i > 1 and A[d-ARY-PARENT(i) < A[i]]
        exchange A[i] with A[d-ARY-PARENT(i)]
        i = d-ARY-PARENT(i)