数据结构与算法(C语言版)__红黑树02
来源:互联网 发布:一致性hash算法原理 编辑:程序博客网 时间:2024/06/07 11:42
今天继续丰富红黑树,我们继续上一个博客
今天写红黑树的insert功能
在头文件中添加Except.h和RedBlackTree.h在源文件中添加TestRedBlackTree.cpp
//Except.h//返回异常#ifndef EXCEPT_H#define EXCEPT_H#include<string>using namespace std;class DSException{public: DSException(const string & msg = "") :message(msg){} virtual ~DSException(){} virtual string toString() const{ return "Exception " + string(":") + what(); } virtual string what() const{ return message; }private: string message;};class DuplicateItemException :public DSException{public: DuplicateItemException(const string & msg = "") :DSException(msg){}};#endif
//RedBlackTree.h//红黑树主要函数#ifndef RED_BLACK_TREE_H_#define RED_BLACK_TREE_H_#include"Except.h"template<class Comparable>class RedBlackTree;template<class Comparable>class RedBlackNode;template<class Comparable>class RedBlackTree{public: RedBlackTree(const Comparable & negInf);//红黑树的构造函数 ~RedBlackTree(); enum{RED,BLACK}; void insert(const Comparable & x); typedef RedBlackNode<Comparable> Node;//private:为了测试,临时变成公有的public: Node *header; Node *nullNode; Node *current;//当前节点 Node *parent;//父结点 Node *grand;//祖父节点 Node *great;//曾祖父节点};template<class Comparable>class RedBlackNode{//private:为了测试,临时变成publicpublic: Comparable element; RedBlackNode *left; RedBlackNode *right; int color; RedBlackNode(const Comparable & theElement = Comparable(), RedBlackNode *lt = NULL, RedBlackNode *rt = NULL, int c = RedBlackTree<Comparable>::BLACK) :element(theElement), left(lt), right(rt), color(c){} friend class RedBlackTree<Comparable>;};template<class Comparable>RedBlackTree<Comparable>::RedBlackTree(const Comparable & negInf){ nullNode = new Node(); nullNode->left = nullNode->right = nullNode; header = new Node(negInf); header->left = header->right = nullNode;}template<class Comparable>RedBlackTree<Comparable>::~RedBlackTree(){ delete nullNode; delete header;}template<class Comparable>void RedBlackTree<Comparable>::insert(const Comparable & x){ current = parent = grand = header; nullNode->element = x; while (current->element != x){ great = grand; grand = parent; parent = current; current = x < current->element ? current->left : current->right; } if (current != nullNode) throw DuplicateItemException(); current = new Node(x, nullNode, nullNode); if (x < parent->element) parent->left = current; else parent->right = current; //自动平衡 -> 红黑树}#endif
//TestRedBlackTree.cpp//测试红黑树#include<iostream>#include"RedBlackTree.h"using namespace std;int main(){ cout << "测试红黑树:" << endl; const int NEG_INF = -99999;//负的无穷大, RedBlackTree<int> t(NEG_INF);//创建一个空树, t.insert(30); t.insert(15); t.insert(70); cout << t.header->right->element << endl; cout << t.header->right->left->element << endl; cout << t.header->right->right->element << endl; system("pause"); return 0;}
总结:今天写了红黑树的insert功能,但是只是简单的insert,因为还不能改变颜色和旋转,现在只是二叉查找树。下一次博客继续写自动平衡功能。
0 0
- 数据结构与算法(C语言版)__红黑树02
- 数据结构与算法(C语言版)__红黑树01
- 数据结构与算法(C语言版)__红黑树03
- 数据结构与算法(C语言版)__红黑树04
- 数据结构与算法(C语言版)__红黑树05
- 数据结构与算法(C语言版)__红黑树06
- 数据结构与算法(C语言版)__交换
- 数据结构与算法(C语言版)__冒泡排序
- 数据结构与算法(C语言版)__选择排序
- 数据结构与算法(C语言版)__顺序查找
- 数据结构与算法(C语言版)__二分查找
- 数据结构与算法(C语言版)__递归
- 数据结构与算法(C语言版)__排列组合
- 数据结构与算法(C语言版)__插入排序
- 数据结构与算法(C语言版)__快速排序
- 数据结构与算法(C语言版)__归并排序
- 数据结构与算法(C语言版)__栈
- 数据结构与算法(C语言版)__队列
- c库函数之一 - atoi函数(补充)
- sitemesh框架入门
- 设备兼容性
- JS生成 UUID的四种方法
- Android 简单的3D动画效果
- 数据结构与算法(C语言版)__红黑树02
- SQL Server 2008还原数据时报错“因为数据库正在使用,所以无法获得对数据库的独占访问权”的解决方法
- ip 正则表达式
- 2.Java虚拟机内存模型及分区
- 浅谈PCB敷铜的“弊与利”
- Spring3.2中返回JSON去掉Bean中NULL值
- 支持通用链接(官方文档的翻译)
- iOS 开发须知
- 用户故事,史诗,主题