/* Function to get maximum node from a min heap The maximum node shall always be one of the leaf nodes. So we shall recursively move through both left and right child, until we find their maximum nodes, and compare which is larger. It shall be done recursively until we get the maximum node */ int getMaxNode(minHeap *hp, int i) { if(LCHILD(i) >= hp->size) { return hp->elem[i].data ; } int l = getMaxNode(hp, LCHILD(i)) ; int r = getMaxNode(hp, RCHILD(i)) ; if(l >= r) { return l ; } else { return r ; } }
void getMaxInK(int *arr, int n, int k) { struct AVLnode* root = NULL; int i = 0, j; while (i < k) { root = insert(root, arr[i]); i++; } struct AVLnode* temp = getMaxNode(root); printf("%d ", temp->data); for (i = k; i < n; ++i) { root = remove_node(root, arr[i-k]); root = insert(root, arr[i]); printf("%d ", getMaxNode(root)->data); } }
const void *TreeSetImpl::getMax() const { return getMaxNode()->key(); }