Skip to main content

Exercises 30.10 Exercises

1.

Implement the largestChildIndex(int currentIndex) function for a MaxHeap. Given an index for a value in the heap, it should return the index of the child that contains the largest value. If there are no children, return -1.
Hint.
The index may have one or no valid children. You need to sanity check the indexes of the children to decide if they are valid.

2.

Implement the add(int value) function for a MaxHeap. This function should insert a new value into the heap while maintaining the max-heap property.
Hint.
The new value should be inserted at the end and then swap up until either it reaches index 0 or it is smaller than its parent.

3.

Implement the removeMax() function for a MaxHeap. This function should remove and return the maximum value from the heap while maintaining the max-heap property.
You will want to use the largestChildIndex function you implemented in a previous exercise. Copy and paste it into this problem.
You have attempted of activities on this page.