以最优方式在二叉搜索树中找到第k个最小元素 [英] Find kth smallest element in a binary search tree in Optimum way

查看:25
本文介绍了以最优方式在二叉搜索树中找到第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 ?

推荐答案

这里只是一个想法的大纲:

Here's just an outline of the idea:

在BST中,节点T的左子树只包含小于T中存储的值的元素.如果 k 小于左子树中的元素数,则 k th 最小的元素必须属于左子树.否则,如果 k 较大,则 kth 最小的元素在右子树中.

In a BST, the left subtree of node T contains only elements smaller than the value stored in T. If k is smaller than the number of elements in the left subtree, the kth smallest element must belong to the left subtree. Otherwise, if k is larger, then the kth smallest element is in the right subtree.

我们可以扩充 BST 使其中的每个节点存储其左子树中的元素数量(假设给定节点的左子树包含该节点).有了这条信息,就可以通过反复询问左子树的元素个数来遍历树,决定是递归到左子树还是右子树.

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.

现在,假设我们在节点 T:

Now, suppose we are at node T:

  1. 如果k == num_elements(T的左子树),那么我们要寻找的答案是节点T中的值.
  2. 如果k > num_elements(T的左子树),那么显然我们可以忽略左子树,因为这些元素也将小于kth最小的元素.因此,我们将问题简化为找到右子树的 k - num_elements(T 的左子树) 最小元素.
  3. 如果 k ,那么 kth 最小的元素在左子树的某处,所以我们将问题简化为找到 kth 最小的元素在左子树中.
  1. If k == num_elements(left subtree of T), then the answer we're looking for is the value in node T.
  2. If k > num_elements(left subtree of T), then obviously we can ignore the left subtree, because those elements will also be smaller than the kth smallest. So, we reduce the problem to finding the k - num_elements(left subtree of T) smallest element of the right subtree.
  3. If k < num_elements(left subtree of T), then the kth smallest is somewhere in the left subtree, so we reduce the problem to finding the kth smallest element in the left subtree.

复杂度分析:

这需要O(depth of node)时间,在平衡BST的最坏情况下是O(log n),或者O(log n)n) 随机 BST 的平均值.

This takes O(depth of node) time, which is O(log n) in the worst case on a balanced BST, or O(log n) on average for a random BST.

一个 BST 需要 O(n) 存储,并且需要另一个 O(n) 来存储有关元素数量的信息.所有BST操作都需要O(depth of node)时间,并且需要O(depth of node)额外的时间来维护插入、删除的元素数"信息或节点的旋转.因此,存储左子树中元素数量的信息保持了BST的空间和时间复杂度.

A BST requires O(n) storage, and it takes another O(n) to store the information about the number of elements. All BST operations take O(depth of node) time, and it takes O(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个最小元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆