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

查看:542
本文介绍了查找在最佳方式二叉搜索树第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 个最小的元素必须属于左子树。否则,如果 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. 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:

  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.

复杂性分析:

这需要 0(节点的深度)的时间,这是 O(log n)的在最坏的情况下,在一个平衡的BST,或 O(log 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操作采取 0(节点的深度)的时间,它需要 0(深度节点)额外的时间来保持数量的元素信息为插入,缺失或节点的旋转。因此,关于在左子树的元素数存储信息保持一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天全站免登陆