简体   繁体   English

二进制搜索树与数组的有序元素

[英]Binary Search Tree vs Array for ordered elements

Consider the scenario where data to be inserted in an array is always in order, ie (1, 5, 12, 20, ...)/A[i] >= A[i-1] or (1000, 900, 20, 1, -2, ...)/A[i] <= A[i-1] . 考虑以下情况:要插入数组中的数据始终按顺序排列,即(1, 5, 12, 20, ...)/A[i] >= A[i-1](1000, 900, 20, 1, -2, ...)/A[i] <= A[i-1]

To support such a dataset, is it more efficient to have a binary search tree or an array. 为了支持这样的数据集,拥有二叉搜索树或数组是否更有效。

(Side note: I am just trying to run some naive analysis for a timed hash map of type (K, T, V) and the time is always in order. I am debating using Map<K, BST<T,V>> vs Map<K, Array<T,V>> .) (旁注:我只是想对类型为(K, T, V)的定时哈希图运行一些幼稚的分析,并且时间总是井井有条。我正在使用Map<K, BST<T,V>>进行辩论vs Map<K, Array<T,V>> 。)

As I understand, the following costs (worst case) apply— 据我了解,以下费用(最坏的情况)适用:

          Array            BST

Space     O(n)             O(n)
Search    O(log n)         O(n)
Max/Min   O(1)             O(1) *
Insert    O(1) **          O(n)
Delete    O(n)             O(n)

* : Max/Min pointers * :最大/最小指针
** : Amortized time complexity ** :摊销时间复杂度

Q: I want to be more clear about the question. 问:我想对这个问题更清楚。 What kind of data structure should I be using for such a scenario between these two? 在这两者之间的这种情况下,我应该使用哪种数据结构? Please feel free to discuss other data structures like self balancing BSTs, etc. 请随时讨论其他数据结构,例如自平衡BST等。

EDIT: 编辑:

  1. Please note I didn't consider the complexity for a balanced binary search tree (RBTree, etc). 请注意,我没有考虑平衡二进制搜索树(RBTree等)的复杂性。 As mentioned, a naive analysis using a binary search tree. 如前所述,使用二进制搜索树进行的幼稚分析。

  2. Deletion has been updated to O(n) (didn't consider time to search the node). 删除已更新为O(n)(未考虑搜索节点的时间)。

  3. Max/Min for skewed BST will cost O(n) . 倾斜的BST的最大值/最小值将花费O(n) But it's also possible to store pointers for Max & Min so overall time complexity will be O(1) . 但是也可以存储MaxMin指针,因此总体时间复杂度为O(1)

See below the table which will help you choose. 请参阅下表,这将帮助您选择。 Note that I am assuming 2 things: 请注意,我假设有两件事:

1) data will always come in sorted order - you mentioned this ie if 1000 is the last data inserted, new data will always be more than 1000 - if data does not come in sorted order, insertion can take O(log n), but deletion will not change 1)数据将始终按排序顺序排列-您提到了这一点,即,如果最后插入的数据为1000,则新数据将始终大于1000-如果数据未按排序顺序排列,则插入可以采用O(log n),但是删除不会改变

2) your "array" is actually similar to java.util.ArrayList. 2)您的“数组”实际上类似于java.util.ArrayList。 In short, its length is mutable. 简而言之,它的长度是可变的。 (it is actually unfair compare a mutable and an immutable data structure) However, if it is a normal array, your deletion will take amortized O(log n) {O(log n) to search and O(1) to delete, amortized if you need to create new array} and insertion will take amortized O(1) {you need to create new array} (比较可变和不可变的数据结构实际上是不公平的。)但是,如果它是普通数组,则删除将使用摊销的O(log n){O(log n)进行搜索,并使用O(1)进行删除,摊销如果需要创建新数组},则插入将分摊O(1){您需要创建新数组}

  ArrayList BST Space O(n) O(n) Search O(log n) O(log n) {optimized from O(n)} Max/Min O(1) O(log n) {instead of O(1) - you need to traverse till the leaf} Insert O(1) O(log n) {optimized from O(n)} Delete O(log n) O(log n) 

So, based on this, ArrayList seems better 因此,基于此,ArrayList似乎更好

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM