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;
}
一半用递归 ,用什么好处吗。
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=不就行了?什么意思?
operator=不就行了?什么意思?
作者: healer_kx 发布时间: 2011-12-13
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28