以最佳方式查找二叉搜索树中的第 k 个最小元素
我需要在二叉搜索树中找到第 k 个最小元素,而不使用任何静态/全局变量。如何高效实现? 我想到的解决方案是在 O(n) 中进行操作,这是最坏的情况,因为我计划对整个树进行中序遍历。但内心深处我觉得我在这里并没有使用 BST 属性。我的假设解决方案是否正确或者是否有更好的解决方案?
I need to find the kth smallest element in the binary search tree without using any static/global variable. How to achieve it efficiently?
The solution that I have in my mind is doing the operation in O(n), the worst case since I am planning to do an inorder traversal of the entire tree. But deep down I feel that I am not using the BST property here. Is my assumptive solution correct or is there a better one available ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(30)
我找不到更好的算法..所以决定写一个:)
如果这是错误的,请纠正我。
}
I couldn't find a better algorithm..so decided to write one :)
Correct me if this is wrong.
}
这是java代码,
max(Node root, int k) - 找到第k个最大的
min(Node root, int k) - 找到第k个最小的
Here is the java code,
max(Node root, int k) - to find kth largest
min(Node root, int k) - to find kth Smallest
这也行。只需在树中调用带有 maxNode 的函数
def k_largest(self, node , k):
如果 k < 0:
返回无
如果 k == 0:
返回节点
别的:
k-=1
返回 self.k_largest(self.predecessor(node), k)
this would work too. just call the function with maxNode in the tree
def k_largest(self, node , k):
if k < 0 :
return None
if k == 0:
return node
else:
k -=1
return self.k_largest(self.predecessor(node), k)
我认为这比接受的答案更好,因为它不需要修改原始树节点来存储其子节点的数量。
我们只需要使用中序遍历从左到右统计最小的节点,当计数等于K时就停止搜索。
I think this is better than the accepted answer because it doesn't need to modify the original tree node to store the number of it's children nodes.
We just need to use the in-order traversal to count the smallest node from the left to right, stop searching once the count equals to K.
最好的方法已经存在。但我想为此添加一个简单的代码
}
Best approach is already there.But I'd like to add a simple Code for that
}
使用辅助 Result 类来跟踪是否找到节点以及当前的 k。
Using auxiliary Result class to track if node is found and current k.
Python解决方案
时间复杂度:O(n)
空间复杂度:O(1)
想法是使用 Morris 中序遍历
Python Solution
Time Complexity : O(n)
Space Complexity : O(1)
Idea is to use Morris Inorder Traversal
步骤如下:
1.向每个节点添加一个字段,指示其根树的大小。这支持O(logN)平均时间的操作。
2.为了节省空间,一个字段指示其根节点的大小就足够了。我们不需要同时保存左子树和右子树的大小。
3.进行中序遍历,直到LeftTree == K, LeftTree = Size(T->Left) + 1。
4.示例代码如下:
5.同样,我们也可以得到KthLargest函数。
Here are the steps:
1.Add a field to each node indicating the size of the tree it roots. This supports operation in O(logN) average time.
2.To save space, one field indicating the size of a node it roots is enough. We don't need to save both the left subtree and right subtree size.
3.Do an inorder traversal until LeftTree == K, LeftTree = Size(T->Left) + 1.
4.Here is the sample code:
5.Similarly, we can also get the KthLargest function.
这个问题是LeetCode 230:BST中的第K个最小元素 。
根据BST性质,第k个最小的节点就是中序遍历时访问到的第k个节点。因此,我们进行递归中序遍历,并记录迄今为止访问过的节点的数量。
时间复杂度:最坏的情况,我们最终可能会访问所有节点,O(n)。但如果找到第 k 个节点,我们就会提前退出。
空间复杂度:O(1)(不考虑递归栈)。
Python 实现。
This question is LeetCode 230: Kth Smallest Element in a BST.
By BST property, the kth smallest node is the kth node visited during the inorder traversal. Thus, we do a recursive inorder traversal, and keep count of the nodes visited thus far.
Time Complexity: Worst case, we may end up visiting all nodes, O(n). But we exit early if the k-th node is found.
Space Complexity: O(1) (not considering recursion stack).
Python implementation.
我写了一个简洁的函数来计算第 k 个最小元素。我使用中序遍历,并在到达第 k 个最小元素时停止。
i wrote a neat function to calculate the kth smallest element. I uses in-order traversal and stops when the it reaches the kth smallest element.
好吧,这是我的 2 美分......
Well here is my 2 cents...
这就是我的想法并且有效。它将运行在 o(log n )
This is what I though and it works. It will run in o(log n )
那么我们可以简单地使用中序遍历并将访问过的元素压入堆栈。
pop k 次,得到答案。
我们也可以在 k 个元素之后停止
Well we can simply use the in order traversal and push the visited element onto a stack.
pop k number of times, to get the answer.
we can also stop after k elements
完整 BST 案例的解决方案:-
Solution for complete BST case :-
Linux 内核具有出色的增强红黑树数据结构,支持 linux/lib/rbtree.c 中 O(log n) 的基于排序的操作。
还可以在 http://code.google.com/p/refolding/source/browse/trunk/core/src/main/java/it/unibo/refolding/alg/RbTree.java ,以及 RbRoot.java 和 RbNode.java。第 n 个元素可以通过调用 RbNode.nth(RbNode node, int n) 来获取,传入树的根。
The Linux Kernel has an excellent augmented red-black tree data structure that supports rank-based operations in O(log n) in linux/lib/rbtree.c.
A very crude Java port can also be found at http://code.google.com/p/refolding/source/browse/trunk/core/src/main/java/it/unibo/refolding/alg/RbTree.java, together with RbRoot.java and RbNode.java. The n'th element can be obtained by calling RbNode.nth(RbNode node, int n), passing in the root of the tree.
C# 的简洁版本,它返回第 k 个最小元素,但需要将 k 作为 ref 参数传递(与 @prasadvk 的方法相同):
这是 (log n) 找到最小的节点,然后 O(k) 遍历到第 k 个节点,所以它是 O(k + log n)。
Here's a concise version in C# that returns the k-th smallest element, but requires passing k in as a ref argument (it's the same approach as @prasadvk):
It's O(log n) to find the smallest node, and then O(k) to traverse to k-th node, so it's O(k + log n).
http://www.geeksforgeeks.org/archives/10379
这是这个问题的确切答案:-
1.在 O(n) 时间内使用中序遍历
2.在 k+log n 时间内使用增广树
http://www.geeksforgeeks.org/archives/10379
this is the exact answer to this question:-
1.using inorder traversal on O(n) time
2.using Augmented tree in k+log n time
这是我基于上面的算法在 C# 中的实现,我只是想发布它,以便人们可以更好地理解它对我有用,
谢谢 IVlad
this is my implementation in C# based on the algorithm above just thought I'd post it so people can understand better it works for me
thank you IVlad
//添加一个不带递归的java版本
//add a java version without recursion
一个更简单的解决方案是进行中序遍历并使用计数器 k 跟踪当前要打印的元素。当我们到达 k 时,打印该元素。运行时间为 O(n)。请记住,函数返回类型不能为 void,它必须在每次递归调用后返回更新后的 k 值。一个更好的解决方案是使用增强的 BST,在每个节点处都有排序的位置值。
A simpler solution would be to do an inorder traversal and keep track of the element currently to be printed with a counter k. When we reach k, print the element. The runtime is O(n). Remember the function return type can not be void, it has to return its updated value of k after each recursive call. A better solution to this would be an augmented BST with a sorted position value at each node.
您可以使用迭代中序遍历:
http://en.wikipedia.org/wiki/Tree_traversal#Iterative_Traversal
从堆栈中弹出节点后,简单检查第 k 个元素。
You can use iterative inorder traversal:
http://en.wikipedia.org/wiki/Tree_traversal#Iterative_Traversal
with a simple check for kth element after poping a node out of the stack.
给定一个普通的二叉搜索树,您所能做的就是从最小的节点开始,向上遍历以找到正确的节点。
如果您经常这样做,您可以向每个节点添加一个属性,表示其左子树中有多少个节点。使用它,您可以将树直接下降到正确的节点。
Given just a plain binary search tree, about all you can do is start from the smallest, and traverse upward to find the right node.
If you're going to do this very often, you can add an attribute to each node signifying how many nodes are in its left sub-tree. Using that, you can descend the tree directly to the correct node.
带计数器的递归有序遍历
这个想法类似于@prasadvk解决方案,但它有一些缺点(见下面的注释),所以我将其作为单独的答案发布。
注意(以及与 @prasadvk 解决方案的差异):
if( counter == k )
在三个位置需要测试:(a) 在左子树之后,( b) 在根之后,以及 (c) 在右子树之后。这是为了确保在所有位置检测到第 k 个元素,即无论它位于哪个子树。if( result == -1 )
需要进行测试以确保仅打印结果元素,否则将从第 k 个最小的元素开始一直到根的所有元素打印。Recursive In-order Walk with a counter
The idea is similar to @prasadvk solution, but it has some shortcomings (see notes below), so I am posting this as a separate answer.
Notes (and differences from @prasadvk's solution):
if( counter == k )
test is required at three places: (a) after left-subtree, (b) after root, and (c) after right subtree. This is to ensure that kth element is detected for all locations, i.e. irrespective of the subtree it is located.if( result == -1 )
test required to ensure only the result element is printed, otherwise all the elements starting from the kth smallest up to the root are printed.对于非平衡搜索树,需要O(n)。
对于平衡搜索树,在最坏情况下需要O(k + log n),但在摊销情况下只需O(k) 感觉。
拥有并管理每个节点的额外整数:子树的大小给出了O(log n)时间复杂度。
这种平衡搜索树通常称为RankTree。
一般来说,有解决方案(不是基于树)。
问候。
For not balanced searching tree, it takes O(n).
For balanced searching tree, it takes O(k + log n) in the worst case but just O(k) in Amortized sense.
Having and managing the extra integer for every node: the size of the sub-tree gives O(log n) time complexity.
Such balanced searching tree is usually called RankTree.
In general, there are solutions (based not on tree).
Regards.
这很有效: status :是保存是否找到元素的数组。 k :是要找到的第 k 个元素。 count :跟踪树遍历期间遍历的节点数。
This works well: status : is the array which holds whether element is found. k : is kth element to be found. count : keeps track of number of nodes traversed during the tree traversal.
虽然这绝对不是问题的最佳解决方案,但它是另一个潜在的解决方案,我认为有些人可能会觉得有趣:
While this is definitely not the optimal solution to the problem, it is another potential solution which I thought some people might find interesting:
签名:
调用为:
定义:
signature:
call as:
definition:
下面只是这个想法的概述:
在 BST 中,节点
T
的左子树仅包含小于T
中存储的值的元素。如果 k 小于左子树中的元素数量,则第 k 个最小元素必定属于左子树。否则,如果 k 较大,则第 k 个最小元素位于右子树中。我们可以扩充 BST,让其中的每个节点存储其左子树中的元素数量(假设给定节点的左子树包括该节点)。有了这条信息,就可以很简单地遍历这棵树,通过反复询问左子树的元素数量,来决定是递归到左子树还是右子树。
现在,假设我们位于节点 T:
T
中的值。复杂度分析:
这需要
O(节点深度)
时间,在平衡BST的最坏情况下为O(log n)
,或者O(log n)
随机 BST 的平均值。BST 需要
O(n)
存储,并且需要另一个O(n)
来存储有关元素数量的信息。所有BST操作都需要O(节点深度)
时间,并且需要O(节点深度)
额外时间来维护插入、删除的“元素数量”信息或节点的旋转。因此,存储有关左子树中元素数量的信息可以保持 BST 的空间和时间复杂度。Here's just an outline of the idea:
In a BST, the left subtree of node
T
contains only elements smaller than the value stored inT
. Ifk
is smaller than the number of elements in the left subtree, thek
th smallest element must belong to the left subtree. Otherwise, ifk
is larger, then thek
th smallest element is in the right subtree.We can augment the BST to have each node in it store the number of elements in its left subtree (assume that the left subtree of a given node includes that node). With this piece of information, it is simple to traverse the tree by repeatedly asking for the number of elements in the left subtree, to decide whether to do recurse into the left or right subtree.
Now, suppose we are at node T:
T
.k
th smallest. So, we reduce the problem to finding thek - num_elements(left subtree of T)
smallest element of the right subtree.k
th smallest is somewhere in the left subtree, so we reduce the problem to finding thek
th smallest element in the left subtree.Complexity analysis:
This takes
O(depth of node)
time, which isO(log n)
in the worst case on a balanced BST, orO(log n)
on average for a random BST.A BST requires
O(n)
storage, and it takes anotherO(n)
to store the information about the number of elements. All BST operations takeO(depth of node)
time, and it takesO(depth of node)
extra time to maintain the "number of elements" information for insertion, deletion or rotation of nodes. Therefore, storing information about the number of elements in the left subtree keeps the space and time complexity of a BST.一个更简单的解决方案是进行中序遍历并跟踪当前要打印的元素(不打印它)。当我们到达 k 时,打印该元素并跳过树遍历的其余部分。
A simpler solution would be to do an inorder traversal and keep track of the element currently to be printed (without printing it). When we reach k, print the element and skip rest of tree traversal.