首页 > 代码库 > 算法导论 红黑树 实现(一)
算法导论 红黑树 实现(一)
首先实现插入 左旋转 右旋转 并测试
暂时未考虑颜色转换
// rbTreeTest.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include <memory> #include <iostream> using namespace std; enum Color { red = 1, black }; struct node { Color color_; std::shared_ptr<node> left_; std::shared_ptr<node> right_; std::shared_ptr<node> parent_; int value_; node() { left_ = right_ = parent_ = nullptr; value_ = -1; color_ = black; } }; std::shared_ptr<node> nil(new node); std::shared_ptr<node> CreateNode(Color color, int i) { std::shared_ptr<node> p(new node); p->color_ = color; p->left_ = nil; p->right_ = nil; p->parent_ = nil; p->value_ = i; return p; } void RightRotate(std::shared_ptr<node>& root, std::shared_ptr<node>& x) { std::shared_ptr<node> y = x->left_; x->left_ = y->right_; if (y->right_ != nil) y->right_->parent_ = x; y->parent_ = x->parent_; if (x->parent_ == nil){ root = y; } else if (x->parent_->left_ == x) { x->parent_->left_ = y; } else { x->parent_->right_ = y; } y->right_ = x; x->parent_ = y; } void LeftRotate(std::shared_ptr<node>& root, std::shared_ptr<node>& x) { std::shared_ptr<node> y = x->right_; x->right_ = y->left_; if (y->left_ != nil) y->left_->parent_ = x; y->parent_ = x->parent_; if (x->parent_ == nil) { root = y; } else if (x->parent_->left_ == x) { x->parent_->left_ = y; } else { x->parent_->right_ = y; } y->left_ = x; x->parent_ = y; } void PrinTree(std::shared_ptr<node> root) { if (root == nil) { return; } std::cout << root->value_ << " "; if(root->left_!=nil) PrinTree(root->left_); if(root->right_ != nil) PrinTree(root->right_); } void test11() { std::shared_ptr<node> root = CreateNode(black, 2); root->parent_ = nil; std::shared_ptr<node> x = root; std::shared_ptr<node> y = CreateNode(red, 1); root->left_ = y; y->parent_ = x; PrinTree(root); std::cout << std::endl; RightRotate(root, x); PrinTree(root); std::cout << std::endl; std::cout << std::endl; } void test1() { //构建测试1,2 std::shared_ptr<node> root = CreateNode(black, 1); root->parent_ = nil; std::shared_ptr<node> x = root; root->right_ = CreateNode(red, 2); root->right_->parent_ = root; PrinTree(root); std::cout << std::endl; LeftRotate(root, x); PrinTree(root); std::cout << std::endl; std::cout << std::endl; } void test22() { std::shared_ptr<node> root = CreateNode(black, 4); root->parent_ = nil; std::shared_ptr<node> x = root; std::shared_ptr<node> y = CreateNode(black, 3); x->left_ = y; y->parent_ = x; std::shared_ptr<node> z = CreateNode(black, 2); y->left_ = z; z->parent_ = y; PrinTree(root); std::cout << std::endl; RightRotate(root, x); PrinTree(root); std::cout << std::endl; std::cout << std::endl; } void test2() { std::shared_ptr<node> root = CreateNode(black, 1); root->parent_ = nil; root->right_ = CreateNode(red, 2); root->right_->parent_ = root; std::shared_ptr<node> x = root->right_; std::shared_ptr<node> y = CreateNode(red, 3); x->right_ = y; y->parent_ = x; PrinTree(root); std::cout << std::endl; LeftRotate(root, x); PrinTree(root); std::cout << std::endl; std::cout << std::endl; } void test33() { std::shared_ptr<node> root = CreateNode(black, 9); root->parent_ = nil; std::shared_ptr<node> a = root; std::shared_ptr<node> b = CreateNode(black, 10); a->right_ = b; b->parent_ = a; std::shared_ptr<node> c = CreateNode(black, 6); a->left_ = c; c->parent_ = a; std::shared_ptr<node> d = CreateNode(black, 8); c->right_ = d; d->parent_ = c; std::shared_ptr<node> e = CreateNode(black, 4); c->left_ = e; e->parent_ = c; std::shared_ptr<node> f = CreateNode(black, 2); e->left_ = f; f->parent_ = e; std::shared_ptr<node> g = CreateNode(black, 5); e->right_ = g; g->parent_ = e; PrinTree(root); std::cout << std::endl; RightRotate(root, c); PrinTree(root); std::cout << std::endl; std::cout << std::endl; } void test3() { std::shared_ptr<node> root = CreateNode(black, 1); root->parent_ = nil; root->right_ = CreateNode(red, 3); root->right_->parent_ = root; std::shared_ptr<node> x = root->right_; std::shared_ptr<node> y = CreateNode(red, 2); x->left_ = y; y->parent_ = x; std::shared_ptr<node> z = CreateNode(red, 4); x->right_ = z; z->parent_ = x; PrinTree(root); std::cout << std::endl; LeftRotate(root, x); PrinTree(root); std::cout << std::endl; std::cout << std::endl; } void test4() { std::shared_ptr<node> root = CreateNode(black, 5); root->parent_ = nil; std::shared_ptr<node> a = CreateNode(red, 2); root->left_ = a; a->parent_ = root; std::shared_ptr<node> b = CreateNode(red, 7); root->right_ = b; b->parent_ = root; std::shared_ptr<node> c = CreateNode(red, 6); b->left_ = c; c->parent_ = b; std::shared_ptr<node> d = CreateNode(red, 9); b->right_ = d; d->parent_ = b; std::shared_ptr<node> e = CreateNode(red, 8); d->left_ = e; e->parent_ = d; std::shared_ptr<node> f = CreateNode(red, 10); d->right_ = f; f->parent_ = d; PrinTree(root); std::cout << std::endl; LeftRotate(root, b); PrinTree(root); std::cout << std::endl; std::cout << std::endl; } void RBInsert(std::shared_ptr<node>& root, std::shared_ptr<node>& ins) { std::shared_ptr<node> y = nil; std::shared_ptr<node> x = root; while (x != nil) { y = x; if (ins->value_ < x->value_) { x = x->left_; } else { x = x->right_; } } ins->parent_ = y; if (y == nil) { root = ins; } else if (ins->value_ < y->value_) { y->left_ = ins; } else { y->right_ = ins; } ins->left_ =ins->right_ = nil; ins->color_ = red; // todo fixup } void TestInsert() { std::shared_ptr<node> root = nil; std::shared_ptr<node> x = CreateNode(red, 7); RBInsert(root, x); PrinTree(root); std::cout << std::endl; x = CreateNode(red,4); RBInsert(root, x); PrinTree(root); std::cout << std::endl; x = CreateNode(red, 11); RBInsert(root, x); PrinTree(root); std::cout << std::endl; x = CreateNode(red, 3); RBInsert(root, x); PrinTree(root); std::cout << std::endl; x = CreateNode(red, 6); RBInsert(root, x); PrinTree(root); std::cout << std::endl; x = CreateNode(red, 9); RBInsert(root, x); PrinTree(root); std::cout << std::endl; x = CreateNode(red, 18); RBInsert(root, x); PrinTree(root); std::cout << std::endl; x = CreateNode(red, 2); RBInsert(root, x); PrinTree(root); std::cout << std::endl; x = CreateNode(red, 14); RBInsert(root, x); PrinTree(root); std::cout << std::endl; x = CreateNode(red, 19); RBInsert(root, x); PrinTree(root); std::cout << std::endl; x = CreateNode(red, 12); RBInsert(root, x); PrinTree(root); std::cout << std::endl; x = CreateNode(red, 17); RBInsert(root, x); PrinTree(root); std::cout << std::endl; x = CreateNode(red, 22); RBInsert(root, x); PrinTree(root); std::cout << std::endl; x = CreateNode(red, 20); RBInsert(root, x); PrinTree(root); std::cout << std::endl; std::cout << std::endl; } int main() { TestInsert(); test1(); test2(); test3(); test4(); test11(); test22(); test33(); return 0; }
算法导论 红黑树 实现(一)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。