简体   繁体   English

一种在二进制最大堆中查找大于某个值X的所有节点的算法

[英]An algorithm to find all nodes greater than some value X in a binary max heap

Here is what I have so far. 这是我到目前为止所拥有的。

We can use recursive algorithm that starts at the root of the heap (since the root is the max number. We will then check to see if our X (what we're looking for) is greater than our root. If X is greater than the root we stop. If not, we print the root and then we check it's left child and right child. And so on... 我们可以使用从堆的根开始的递归算法(因为根是最大数。然后,我们将检查X是否大于我们的根。如果X大于根目录我们停止了,如果没有,我们打印根目录,然后检查它是左子节点还是右子节点,依此类推...

Is this a good algorithm? 这是一个好的算法吗? Also will the worse case time complexity of my algorithm be O(N) in which N is the # of nodes in the heap. 我算法的最坏情况下的时间复杂度也将是O(N),其中N是堆中节点的数量。

This algorithm is good. 这个算法很好。 In fact, its optimal in terms of time complexity as it visits at most 3 * k nodes, where k is the number of nodes that satisfy the given condition (it is the case because the node is visited only if it satisfies the condition or its parent does). 实际上,它在访问最多3 * k节点时在时间复杂度方面是最优的,其中k是满足给定条件的节点数(之所以如此,是因为只有满足条件或满足条件的节点才被访问父母)。

Yes, it's O(N) in the worst case. 是的,最坏的情况是O(N) But you cannot do better because you might need to print all the nodes in the heap. 但是您不能做得更好,因为您可能需要打印堆中的所有节点。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 合并两个二进制最大堆,其中一个堆中的所有元素都大于另一个堆中的所有元素? - Merging two binary max heaps in which all the elements in one heap is greater than all the elements in the other? 查找一组素数大于x的素数的算法 - algorithm to find products of a set of primes, in order, greater than x 查找(nxn)井字棋盘中所有单元格二进制值的算法? - Algorithm to find binary value of all cells in (n x n) Tic-tac-toe board? 如何找到小于或等于X的最大值和大于或等于X的最小值? - How do I find the largest value smaller than or equal to X and the smallest value greater than or equal to X? 查找加起来等于或大于 X 的数字集组合的算法 - Algorithm to find combinations of sets of numbers that add up to equal or greater than X 从二进制最大堆中删除根节点的算法 - Algorithm to delete root node from binary max heap 最小堆中大于或等于 x 的第 K 个最小元素 - Kth smallest element greater than or equal to x in a min heap 如何判断堆的第k大元素是否大于x - how to determine if the kth largest element of the heap is greater than x 查找最大距离不大于K的树的最大子集 - Find max subset of tree with max distance not greater than K 在二叉树中找到一组“k”个标记顶点的算法,该算法最小化所有节点到标记祖先的距离 - Algorithm to find a set of "k" marked vertices in a binary tree that minimizes distance to marked ancestors for all nodes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM