algorithm - 如何判断堆的第k大元素是否大于x

标签 algorithm complexity-theory binary-heap

考虑一个包含 n 的二叉堆 数字(根存储最大的数字)。你被赋予了 正整数 k < n 和数字 x。你必须确定是否 堆的第 k 个最大元素是否大于 x。你的 算法必须花费 O(k) 时间。你可以使用 O(k) 额外的存储空间

最佳答案

简单的 dfs 就可以完成这项工作。我们有一个计数器设置为零。从根开始,在每次迭代中检查当前节点的值;如果它大于 x,则增加计数器并继续对其中一个子节点执行算法。如果 counter 大于等于 k ​​或没有节点要检查,则算法终止。运行时间为O(k),因为最多迭代k个节点,每次迭代时间为O(1)。

伪代码如下所示。

    void CheckNode(Node node,int k, int x, ref int counter)
    {
        if (node.value > x)
        {
            counter++;
            if (counter >= k)
                return;

            CheckNode(node.Left, k, x, ref counter);
            CheckNode(node.Right,k, x, ref counter);
        }
    }

使用它:

        counter = 0;
        CheckNode(root,index,val,counter );
        if (counter >= index)
            return true;
        return false;

如果 node.value < x 则所有子值都小于 x 并且不需要检查。

正如@Eric Mickelsen 在评论中提到的,最坏情况下的运行时间正好是 2k-1 (k>0),如下所示。

The number of nodes visited with values greater than x will be at most k. Each node visited with value less than x must be a child of a visited node with value greater than x. However, because every node visited except the root must have a parent with value greater than x, the number of nodes of value less than x visited must be at most ((k-1)*2)-(k-1) = k-1, since k-1 of the (k-1)*2 children have values greater than x. This means that we visit k nodes greater than x and k-1 less than x, which is 2k-1.

关于algorithm - 如何判断堆的第k大元素是否大于x,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4922648/

相关文章:

c++ - 改进 C++ 算法以查找半径为 r 的球体内的所有点

algorithm - 相交两个有序列表

sql - SQL中具有相同属性的单独表

java - 为什么二叉堆是树结构?

sorting - 如何创建弹出最小值而不是最大值的 BinaryHeap?

python - 如何在python中迭代动态 "depths"的字典?

java - Cracking the Code book 平方根算法中的整数范围

performance - 是否可以根据图灵空间复杂度估算所需的 RAM?

algorithm - 求这个修改区间图的色数问题是NP-Complete吗?

algorithm - 构建最小/最大二叉堆