简体   繁体   English

时间和空间复杂度在二叉树的每个级别上创建元素矢量(NON-BST)

[英]time and space complexity creating vector of elements at each levels of a binary tree(NON-BST)

Could you please help me understand calculating the time and space complexity of the below function. 您能否帮助我理解计算以下函数的时间和空间复杂度。

function(): Role: create vector of nodes at each levels function():作用:在每个级别创建节点向量

  1. create a queue. 创建一个队列。
  2. add the root 添加根
  3. copy the elements in the queue to the vector. 将队列中的元素复制到向量。
  4. traverse the vector and append the child(left & right) to the queue. 遍历向量并将子级(左右)附加到队列中。
  5. repeat steps 3 & 4 until the queue is empty. 重复步骤3和4,直到队列为空。

below is the code. 下面是代码。

struct node {
     int value;
     struct node* p_left;
     struct node* p_right;
};

void levels_list(struct node* p_btree) {
     if(!p_btree) {
          return ;
     }


     std::queue<struct node*> q;
     std::vector<std::vector<struct node*> > v1;

     q.push(p_btree);
     bool choice = false;

     while(!q.empty()) {
          std::vector<struct node*> v;

          while(!q.empty()) {
               v.push_back(q.front());
               q.pop();
          }

          for(int i = 0; i < v.size(); i++) {
               if(v[i]->p_left) {
                    q.push(v[i]->p_left);
               }
               if(v[i]->p_right) {
                    q.push(v[i]->p_right);
               }
          }

          v1.push_back(v);
          v.clear();
     }
}

I see that it is O(n^2), am I correct? 我看到它是O(n ^ 2),对吗? There are two loops the first one is outer and the second one is the inner which pushes the elements into the vector from the queue. 有两个循环,第一个循环在外部,第二个循环在内部,将元素从队列推入向量。

Thanks 谢谢

At the beginning of each iteration, q has a set of nodes. 在每次迭代的开始, q有一组节点。 At the end of each iteration, q has a set of its children. 在每次迭代结束时, q都有一组子项。 So each iteration of the loop is a level. 因此,循环的每个迭代都是一个级别。

Each level only processes the nodes at that level. 每个级别仅处理该级别的节点。 Push back to a vector is O(1) , push and pop from a queue is O(1) (amortized). 推回向量是O(1) ,从队列推入和弹出是O(1) (摊销)。

We don't do anything fancier than that. 我们没有比这更出色的事情了。 So this is O(n) computation time. 因此,这是O(n)计算时间。 Since the size of v and q will never be greater than n (If there is a fully complete row in the bt, it'll be ciel(n/2) ), and v1 only contains one of each node, this is also O(n) in space. 由于v和q的大小永远不会大于n(如果bt中有一个完全完整的行,则为ciel(n/2) ),并且v1仅包含每个节点之一,这也是O(n)在太空中。

That being said, the above code is an excellent example of how you can write a complexity fine algorithm poorly. 话虽如此,上面的代码是一个很好的示例,说明了如何无法很好地编写复杂度高的算法。 You end up doing a lot of unnecessary copies and allocations...There is no reason to use a queue like this, or even any intermediate data structure...you know the output vector and can keep track of the limits of where each level is in the output vector, so you can just iterate over it until you reach a level whose size is 0. If you know the number of elements of the BST you can preallocate this and it'll be nice and cache friendly. 您最终会进行很多不必要的复制和分配...没有理由使用这样的队列,甚至没有任何中间数据结构...您都知道输出向量,并且可以跟踪每个级别的限制在输出向量中,因此您可以对其进行迭代,直到达到大小为0的级别为止。如果您知道BST的元素数量,则可以对其进行预分配,它将很好并且对缓存友好。

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

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