简体   繁体   English

ArrayDeque与LinkedList作为队列进行级别顺序遍历

[英]ArrayDeque vs LinkedList as Queue for level-order traversal

Is it true that ArrayDeque should be preferred over LinkedList in this scenario? 在这种情况下, ArrayDeque是否比LinkedList受青睐吗? Why is ArrayDeque better than LinkedList . 为什么ArrayDeque比LinkedList更好

In my opinion, I should be using LinkedList instead of ArrayDeque, as there are quite a lot of poll and offer operation going on in this algorithm, and there is no random access to elements. 在我看来,我应该使用LinkedList而不是ArrayDeque,因为此算法中正在进行很多polloffer操作,并且没有对元素的随机访问。

 public ArrayList<ArrayList<Integer>> levelOrder(TreeNode a) {
    Queue<TreeNode> q = new LinkedList<>();  // new ArrayDeque<>() ???
    q.offer(a);
    ArrayList<ArrayList<Integer>> ans = new ArrayList<ArrayList<Integer>>();
    while (q.peek() != null){ //returns null if empty
        ArrayList<Integer> list = new ArrayList<>();
        int n = q.size();
        for (int i = 0; i < n; i++){
            TreeNode node = q.poll();
            list.add(node.val);
            if (node.left != null) {
                q.offer(node.left);
            }
            if (node.right != null) {
                q.offer(node.right);
            }
        }
        ans.add(list);
    }
    return ans;
}

There are a number of tradeoffs to consider when deciding between ArrayDeque and LinkedList when treating the two as a doubly-ended queue. 在将ArrayDequeLinkedList视为双端队列时,需要在两者之间ArrayDeque权衡。

Generally speaking, the LinkedList type has the following advantages: 一般来说, LinkedList类型具有以下优点:

  • The cost of adding an element is worst-case O(1), so no individual insertion will ever take too much time. 添加元素的成本是最坏情况下的O(1),因此任何单个插入都不会花费太多时间。
  • The cost removing an element from the ends is worst-case O(1), so no individual deletion will ever take too much time. 从末端删除元素的成本是最坏的情况O(1),因此没有单个删除操作会花费太多时间。

Generally speaking, the LinkedList type has the following major disadvantage: 一般来说, LinkedList类型具有以下主要缺点:

  • Because elements in a LinkedList are not necessarily stored contiguously in memory, the LinkedList has poor locality of reference, and accesses may result in more cache misses, decreasing performance. 由于LinkedList中的元素不一定必须连续存储在内存中,因此LinkedList的引用位置很差,并且访问可能会导致更多的高速缓存未命中,从而降低性能。

Generally speaking, the ArrayDeque type has the following advantages: 一般来说, ArrayDeque类型具有以下优点:

  • Because elements are stored contiguously, the ArrayDeque has great locality of reference, so element accesses will typically create few cache misses and thus lead to excellent performance. 因为元素是连续存储的,所以ArrayDeque的引用位置很大,因此元素访问通常将创建很少的缓存未命中,从而导致出色的性能。

Generally speaking, the ArrayDeque has the following disadvantage: 一般来说, ArrayDeque具有以下缺点:

  • Although any sequence of n operations on an ArrayDeque will take time O(n), when the array capacity is exceeded on an insertion, the ArrayDeque may have to do O(n) work to copy elements. 尽管对ArrayDeque进行n个操作的任何序列ArrayDeque将花费时间O(n),但是当插入时超出数组容量时, ArrayDeque可能必须执行O(n)才能复制元素。 As a result, the worst-case performance of an ArrayDeque per operation is slower than a LinkedList . 结果,每个操作ArrayDeque的最坏情况性能要比LinkedList慢。
  • Similarly, if the array capacity drops too low on a deletion, it may take time O(n) to resize the array, though, as before, any series of n operations on an ArrayDeque will take time O(n). 同样,如果删除时阵列容量下降得太低,则调整阵列大小可能需要花费时间O(n),尽管像以前一样,对ArrayDeque任何n序列操作都会花费时间O(n)。

In your particular use case, since all you care about is the end-to-end efficiency of the level-order traversal, not the cost of any individual addition or removal of elements from the queue, it will probably be faster to use an ArrayDeque than a LinkedList . 在您的特定用例中,由于您关心的只是级别顺序遍历的端到端效率,而不是从队列中单独添加或删除元素的成本,因此使用ArrayDeque可能会更快比LinkedList In a sense, the LinkedList type is generally only better if you need to have good worst-case performance on each operation performed, and that isn't the case here. 从某种意义上说,通常只有在您需要在执行的每个操作上都具有良好的最坏情况下的性能时, LinkedList类型才更好,而在这里不是这种情况。 The ArrayDeque is generally more performant when all you care about is end-to-end runtime. 当您只关心端到端运行ArrayDeque通常会更ArrayDeque

Hope this helps! 希望这可以帮助!

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

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