始终保持最佳元素的数据结构 [英] Data structure that always keeps n-best elements

查看:161
本文介绍了始终保持最佳元素的数据结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个数据结构,它始终保存到目前为止插入的最大项目(没有特定顺序)的 n

I need a data structure that always holds the n largest items inserted so far (in no particular order).

所以,如果 n 是3,我们可以在下面的会话中插入几个数字,内容的容器更改:

So, if n is 3, we could have the following session where I insert a few numbers and the content of the container changes:

[]  // now insert 1
[1] // now insert 0
[1,0] // now insert 4
[1,0,4] // now insert 3
[1,4,3] // now insert 0
[1,4,3] // now insert 3
[4,3,3]

你得到这个想法数据结构的名称是什么?实现这个的最好方法是什么?或者是在某些图书馆中?

You get the idea. What's the name of the data structure? What's the best way to implement this? Or is this in some library?

我正在考虑使用一个容器,它的元素具有 priority_queue 委托),它使用反向比较,所以 pop 将删除最小的元素。因此, insert 函数首先检查要插入的新元素是否大于最小值。如果是这样,那么我们把最小的值抛出来并推新元素。

I am thinking to use a container that has a priority_queue for its elements (delegation), which uses the reverse comparison, so pop will remove the smallest element. So the insert function first checks if the new element to be inserted is greater than the smallest. If so, we throw that smallest out and push the new element.

(我有一个 C ++ ,但问题是语言不可知论。)

(I have a C++ implementation in mind, but the question is language-agnostic nevertheless.)

推荐答案

您想要的具体数据结构可能是 / i>。原始数据结构只是一个数组;为了方便起见,说它是N = 2 ^ n个元素的大小,并且你想保持最大的N-1个元素。

The specific datastructure you want is probably the implicit heap. The raw datastructure is just an array; for convenience, say that it is N=2^n elements in size, and that you want to maintain the largest N-1 elements.

这个想法是对待数组(称为A)为深度为n的完整二叉树:

The idea is to treat the array (call it A) as a complete binary tree of depth n:


  • 忽略A [0];将A [1]作为每个节点A [k]的根节点
  • ,孩子是A [2 * k]和A [2 * k + 1]
  • 节点A [N / 2..N-1]是叶子
  • ignore A[0]; treat A[1] as the root node
  • for each node A[k], the children are A[2*k] and A[2*k+1]
  • nodes A[N/2..N-1] are the leaves

为了将树保持为堆,您需要确保每个节点小于(或等于)它的孩子。这被称为堆条件:

To maintain the tree as a "heap", you need to ensure that each node is smaller than (or equal to) its children. This is called the "heap condition":


  • A [k]< = A [2 * k]
  • A [k] <= A [2 * k + 1]
  • A[k] <= A[2*k]
  • A[k] <= A[2*k+1]

要使用堆来维护最大的N个元素:

To use the heap to maintain the largest N elements:

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