コード例 #1
0
ファイル: minHeap.c プロジェクト: edenzik/min-heap
/*
    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 ;
    }
}
コード例 #2
0
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);
    }
}
コード例 #3
0
const void *TreeSetImpl::getMax() const {
  return getMaxNode()->key();
}