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

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

推荐答案

这里只是一个概念:

在BST中,节点 T 的左子树仅包含小于存储在 T中的值的元素。如果 k 小于左子树中元素的数量,则 k 最小元素必须属于左子树。否则,如果 k 更大,那么 k 最小的元素在正确的子树中。

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的左子树),那么显然我们可以忽略左子树,因为这些元素也将小于最小的 k 。所以,我们减少找到正确的子树的 k - num_elements(T的左子树)最小元素的问题。

  3. 如果 k < num_elements(T的左子树),则最小的 k 在左子树中的某处,所以我们减少了找到 k 左子树中的最小元素

  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(节点深度)时间,这是 O(log n)在最坏的情况下,平均BST,或平均为一个随机BST的 O(log n)

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(节点深度)时间,并且需要额外的时间来 O(节点深度)维护节点的插入,删除或旋转的元素数信息。因此,存储关于左子树中的元素数量的信息保持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天全站免登陆