+ -
当前位置:首页 → 问答吧 → stl中红黑树复制方法的好处

stl中红黑树复制方法的好处

时间:2011-12-13

来源:互联网

template <class K, class V, class KeyOfValue, class Compare, class Alloc>
typename rb_tree<K, V, KeyOfValue, Compare, Alloc>::link_type // __copy(x.root(), header); 广义的是当前节点和 父节点
rb_tree<K, V, KeyOfValue, Compare, Alloc>::__copy(link_type x, link_type p) { //x为root p为header
  // structural copy. x and p must be non-null.
  link_type top = clone_node(x);  
  top->parent = p;
 
  __STL_TRY {
  if (x->right)
  top->right = __copy(right(x), top);
  p = top;
  x = left(x);

  while (x != 0) {
  link_type y = clone_node(x);
  p->left = y; //目前为止 p的左右子树 和 父节点都已经复制完毕
  y->parent = p; //左子树的父节点赋值
  if (x->right) //左子树的右节点不为空
  y->right = __copy(right(x), y); //递归的为左子树的右节点赋值
  p = y; // 保持一直循环 x为当前节点 p为父节点
  x = left(x);
  }
  }
  __STL_UNWIND(__erase(top));

  return top;
}


一半用递归 ,用什么好处吗。

作者: yzhaozhe   发布时间: 2011-12-13

有一些算法使用递归比较好理解,这就是为什么很多教科书中的算法使用递归来展示。转换为高效的算法(非递归),不容易理解。所以,你看的这个可能不是最好的红黑树算法实现。

作者: lofeo   发布时间: 2011-12-13

?

operator=不就行了?什么意思?

作者: healer_kx   发布时间: 2011-12-13