Javascript:我需要一个良好的数据结构来保持排序列表 [英] Javascript: I need a good data structure to keep a sorted list

查看:134
本文介绍了Javascript:我需要一个良好的数据结构来保持排序列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能会被实现为一棵树或什么东西?我的观点是需要有效。



虽然我不知道在哪里可以找到Javascript的数据结构的好的实现。



帮助赞赏。

解决方案

取决于你为什么需要它例如,如果您只需要顶级元素,则二进制堆可能对您而言是正确的。否则,对数组实现binarySearch和insertSorted函数,不应超过十五行。除非你计划拥有成千上万的元素;那么只需要批量插入,然后使用内置函数排序就更有意义了。


This would probably be implemented as a tree or something? My point is it needs to be efficient.

I don't know where to find good implementations of data structures for Javascript for something like this, though. I don't want to have to roll my own if I can avoid it.

Help appreciated.

解决方案

Depends why you need it. For instance, if you only need the top element, this binary heap might be okay for you. Otherwise, implement a binarySearch and insertSorted functions for arrays, should not be more than ten-fifteen lines. Unless you plan on having thousands upon thousands of elements; then it makes more sense to just insert in bulk and then sort using the builtin.

这篇关于Javascript:我需要一个良好的数据结构来保持排序列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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