设为首页 加入收藏

TOP

红黑树C++实现(四)
2015-11-10 13:45:49 来源: 作者: 【 】 浏览:10
Tags:实现
ptr->left;


? ? ? ? ? ? ? ? }


? ? ? ? ? ? ? ? return left_remember_ptr;


? ? ? ? ? ? }


? ? ? ? ? ? return right_child_ptr;


?


? ? ? ? }else{


? ? ? ? ? ? rb_ptr parent_ptr=current_ptr->parent;


? ? ? ? ? ? while(current_ptr ==parent_ptr->right){


? ? ? ? ? ? ? ? current_ptr=parent_ptr;


? ? ? ? ? ? ? ? parent_ptr=parent_ptr->parent;


? ? ? ? ? ? }


? ? ? ? ? ? return parent_ptr;


? ? ? ? }


? ? ? ? return nullptr;


? ? }
? ? template
? ? typename rb_tree::rb_ptr rb_tree::_get_node(const Value& e) const {
? ? ? ? rb_ptr insert_ptr = new tree_node();
? ? ? ? link_type(insert_ptr)->data=e;
? ? ? ? insert_ptr->parent=nullptr;
? ? ? ? insert_ptr->left=nullptr;
? ? ? ? insert_ptr->right=nullptr;
? ? ? ? insert_ptr->color=RED;
? ? ? ? return insert_ptr;
? ? }
? ? template
? ? bool rb_tree::_right_rotate(rb_ptr root){
? ? ? ? if(root->left!=nullptr){
? ? ? ? ? ? rb_ptr left_child_ptr=root->left;


? ? ? ? ? ? root->left=left_child_ptr->right;
? ? ? ? ? ? if(left_child_ptr->right !=nullptr)
? ? ? ? ? ? ? ? left_child_ptr->right->parent=root;


? ? ? ? ? ? left_child_ptr->right=root;
? ? ? ? ? ? left_child_ptr->parent=root->parent;
? ? ? ? ? ? if(root->parent->left ==root)
? ? ? ? ? ? ? ? root->parent->left=left_child_ptr;
? ? ? ? ? ? else if(root->parent->right==root)
? ? ? ? ? ? ? ? root->parent->right=left_child_ptr;


? ? ? ? ? ? if(root->parent==head){


? ? ? ? ? ? ? ? root->parent->parent=left_child_ptr;


? ? ? ? ? ? }


? ? ? ? ? ? root->parent=left_child_ptr;
? ? ? ? ? ? return true;
? ? ? ? }
? ? ? ? return false;
? ? }
? ? template
? ? bool? rb_tree< Value >::_left_rotate(rb_ptr root){
? ? ? ? if(root->right!=nullptr){
? ? ? ? ? ? rb_ptr right_child_ptr=root->right;


? ? ? ? ? ? root->right=right_child_ptr->left;
? ? ? ? ? ? if(right_child_ptr->left != nullptr)
? ? ? ? ? ? ? ? right_child_ptr->left->parent=root;


? ? ? ? ? ? right_child_ptr->left=root;
? ? ? ? ? ? right_child_ptr->parent=root->parent;
? ? ? ? ? ? if(root->parent->left ==root)
? ? ? ? ? ? ? ? root->parent->left=right_child_ptr;
? ? ? ? ? ? else if(root->parent->right ==root)
? ? ? ? ? ? ? ? root->parent->right=right_child_ptr;


? ? ? ? ? ? if(root->parent==head){


? ? ? ? ? ? ? ? root->parent->parent=right_child_ptr;


? ? ? ? ? ? }
? ? ? ? ? ? root->parent=right_child_ptr;


? ? ? ? ? ? return true;
? ? ? ? }
? ? ? ? return false;
? ? }
? ? template
? ? void? rb_tree::levelOrder() const {
? ? ? ? rb_ptr root =head->parent;
? ? ? ? queue q;
? ? ? ? if(head->parent !=head){
? ? ? ? ? ? q.push(root);
? ? ? ? ? ? while(!q.empty()){
? ? ? ? ? ? ? ? rb_ptr visit_ptr = q.front();
? ? ? ? ? ? ? ? cout<<"data: "<data<<"? color: "<<((visit_ptr->color==0)?"RED":"BLACK")<? ? ? ? ? ? ? ? if(visit_ptr->left)
? ? ? ? ? ? ? ? ? ? q.push(visit_ptr->left);
? ? ? ? ? ? ? ? if(visit_ptr->right)
? ? ? ? ? ? ? ? ? ? q.push(visit_ptr->right);
? ? ? ? ? ? ? ? q.pop();
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ? template
? ? void rb_tree:: _preOrder(rb_ptr root) const{
? ? ? ? if(root){
? ? ? ? ? cout<<"data: "<data<<"? color: "


? ? ? ? ? ? <<((root->color==0)?"RED":"BLACK")<? ? ? ? ? ? _preOrder(root->left);
? ? ? ? ? ? _preOrder(root->right);
? ? ? ? }
? ? }
? ? template
? ? void rb_tree:: preOrder() const{
? ? ? ? _preOrder(head->parent);
? ? }
}



//#include "rbtree.cpp"
#endif /* SRC_RBTREE_H_ */


首页 上一页 1 2 3 4 下一页 尾页 4/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Python正则表达式:如何使用正则.. 下一篇排序之归并排序

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: