博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
红黑树
阅读量:6197 次
发布时间:2019-06-21

本文共 4447 字,大约阅读时间需要 14 分钟。

hot3.png

 

5个基本性质

1.每个节点要么是红 要么是黑2.根节点是黑3.每个叶子节点是黑的4.如果一个节点是红的,那么子节点都是黑的5.对于每个节点,该节点到跟的所有路径包含有相同的黑节点

 

插入

新节点默认是红,如果是黑肯定破坏了性质,红的话还要分情况讨论插入有3种情况,先按照平衡二叉树的插入,进行,依然保持查找特性,但是可能会破坏红黑树的性质因此要对树进行调整,以保持性质。

左右旋转

STL XTREE代码	void _Lrotate(_Nodeptr _Wherenode)		{	// promote right node to root of subtree		_Nodeptr _Pnode = this->_Right(_Wherenode);		this->_Right(_Wherenode) = this->_Left(_Pnode);		if (!this->_Isnil(this->_Left(_Pnode)))			this->_Parent(this->_Left(_Pnode)) = _Wherenode;		this->_Parent(_Pnode) = this->_Parent(_Wherenode);		if (_Wherenode == _Root())			_Root() = _Pnode;		else if (_Wherenode == this->_Left(this->_Parent(_Wherenode)))			this->_Left(this->_Parent(_Wherenode)) = _Pnode;		else			this->_Right(this->_Parent(_Wherenode)) = _Pnode;		this->_Left(_Pnode) = _Wherenode;		this->_Parent(_Wherenode) = _Pnode;		}	void _Rrotate(_Nodeptr _Wherenode)		{	// promote left node to root of subtree		_Nodeptr _Pnode = this->_Left(_Wherenode);		this->_Left(_Wherenode) = this->_Right(_Pnode);		if (!this->_Isnil(this->_Right(_Pnode)))			this->_Parent(this->_Right(_Pnode)) = _Wherenode;		this->_Parent(_Pnode) = this->_Parent(_Wherenode);		if (_Wherenode == _Root())			_Root() = _Pnode;		else if (_Wherenode == this->_Right(this->_Parent(_Wherenode)))			this->_Right(this->_Parent(_Wherenode)) = _Pnode;		else			this->_Left(this->_Parent(_Wherenode)) = _Pnode;		this->_Right(_Pnode) = _Wherenode;		this->_Parent(_Wherenode) = _Pnode;		}

插入,mark一下,插入只有四种情况,需要修复

xtree(1833)	template
iterator _Insert_at(bool _Addleft, _Nodeptr _Wherenode, _Valty&& _Val, _Nodety _Node) { // add node with value next to _Wherenode, to left if _Addleft if (max_size() - 1 <= this->_Mysize) { // tree would get too big, fail _Destroy_if_not_nil(_Node); _Xlength_error("map/set
too long"); } _Nodeptr _Newnode = _Buynode_if_nil(_Node, _STD forward<_Valty>(_Val)); ++this->_Mysize; _Newnode->_Parent = _Wherenode; if (_Wherenode == this->_Myhead) { // first node in tree, just set head values _Root() = _Newnode; _Lmost() = _Newnode; _Rmost() = _Newnode; } else if (_Addleft) { // add to left of _Wherenode this->_Left(_Wherenode) = _Newnode; if (_Wherenode == _Lmost()) _Lmost() = _Newnode; } else { // add to right of _Wherenode this->_Right(_Wherenode) = _Newnode; if (_Wherenode == _Rmost()) _Rmost() = _Newnode; } for (_Nodeptr _Pnode = _Newnode; this->_Color(this->_Parent(_Pnode)) == this->_Red;) if (this->_Parent(_Pnode) == this->_Left(this->_Parent(this->_Parent(_Pnode)))) { // fixup red-red in left subtree _Wherenode = this->_Right(this->_Parent(this->_Parent(_Pnode))); if (this->_Color(_Wherenode) == this->_Red) { // parent has two red children, blacken both this->_Color(this->_Parent(_Pnode)) = this->_Black; this->_Color(_Wherenode) = this->_Black; this->_Color(this->_Parent(this->_Parent(_Pnode))) = this->_Red; _Pnode = this->_Parent(this->_Parent(_Pnode)); } else { // parent has red and black children if (_Pnode == this->_Right(this->_Parent(_Pnode))) { // rotate right child to left _Pnode = this->_Parent(_Pnode); _Lrotate(_Pnode); } this->_Color(this->_Parent(_Pnode)) = this->_Black; // propagate red up this->_Color(this->_Parent(this->_Parent(_Pnode))) = this->_Red; _Rrotate(this->_Parent(this->_Parent(_Pnode))); } } else { // fixup red-red in right subtree _Wherenode = this->_Left(this->_Parent(this->_Parent(_Pnode))); if (this->_Color(_Wherenode) == this->_Red) { // parent has two red children, blacken both this->_Color(this->_Parent(_Pnode)) = this->_Black; this->_Color(_Wherenode) = this->_Black; this->_Color(this->_Parent(this->_Parent(_Pnode))) = this->_Red; _Pnode = this->_Parent(this->_Parent(_Pnode)); } else { // parent has red and black children if (_Pnode == this->_Left(this->_Parent(_Pnode))) { // rotate left child to right _Pnode = this->_Parent(_Pnode); _Rrotate(_Pnode); } this->_Color(this->_Parent(_Pnode)) = this->_Black; // propagate red up this->_Color(this->_Parent(this->_Parent(_Pnode))) = this->_Red; _Lrotate(this->_Parent(this->_Parent(_Pnode))); } } this->_Color(_Root()) = this->_Black; // root is always black return (iterator(_Newnode, this)); }

转载于:https://my.oschina.net/kkkkkkkkkkkkk/blog/749603

你可能感兴趣的文章
我的友情链接
查看>>
《第一行代码》2day~Activity
查看>>
11gR2 dataguard 备库文件损坏处理
查看>>
JVM 垃圾回收器与参数配置
查看>>
Juniper 基于路由的×××
查看>>
SSH登陆不上
查看>>
笨方法学习Python21-30
查看>>
Myeclipse常用快捷键
查看>>
centos7 BitNami一键安装Redmine
查看>>
SVN学习总结(2)——SVN冲突解决
查看>>
梦呓随笔
查看>>
linux下分配磁盘
查看>>
RabbitMQ学习总结(5)——发布和订阅实例详解
查看>>
tomcat 主要配置解析(1)
查看>>
互联网如何增强客户黏性?
查看>>
MyBatis学习总结(二)——使用MyBatis对表执行CRUD操作
查看>>
Log4j 1使用教程
查看>>
RHCA442学习笔记-Unit10内存地址及分配
查看>>
Java基础学习总结(10)——static关键字
查看>>
Linux 基础命令2
查看>>