红黑树结构完整实现与详解

红黑树结构以平衡、高效的随机访问著称。实际使用过程中,其效率超出想象(节点数量越多,效率越高),大部分情况下查找的节点数量小于节点总量的二分之一,最长的查询路径也才是总量的二分之一加一个节点的距离。

红黑树以黑色和红色用来标记根节点、枝节点与叶节点既有优势也有劣势(当然优势大于劣势):
优势:在多次查询节点时高度保持树型的平衡(旋转情况下,不超过三次旋转,插入数据不超过两次旋转);
劣势:树型旋转时造成多余的开支。

红黑树的插入情况:
这里写图片描述
从图片中可以看出,红黑树根节点(节点20),根节点左侧(节点10) 小于根节点,根节点右侧(节点30)大于根节点,右侧节点(父节点30) 小于右儿子节点(节点40),红黑树无论有多少个节点都是以这种形式进行平衡(根节点或枝节点大于他们的左节点,小于他们的右节点)。

红黑树以小左大右的方式进行遍历查询,查询到空节点停止,并且返回空节点的上一个节点作为父节点进行插入(如果小于父节点作为父节点的左节点插入,如果大于父节点作为父节点的右节点插入),参考下面代码:

//返回需要插入节点的位置
Test_Rb_tree_node_base* Test_Rb_tree::Test_M_lower_bound(Test_Rb_tree_Ptr x,Test_Rb_tree_Ptr y,unsigned int val)
{
	while (x != 0)
	{
		int nless = compare_less(x, val);
		if (-1 == nless)//当前节点小于查询到的节点
			y = x, x = Left(x);
		else if (1 == nless)//当前节点大于查询到的节点
			x = Right(x);
		else//如果相等,这条数据不再插入,直接返回
			return 0;
	}
		
	return y;
}

插入情况一、右节点插入:
这里写图片描述

这里写图片描述

这里写图片描述

这里写图片描述

这里写图片描述

这里写图片描述

插入情况二、左节点插入:

这里写图片描述

这里写图片描述

这里写图片描述

这里写图片描述

这里写图片描述

这里写图片描述

插入情况三、根节点左旋转:

这里写图片描述

这里写图片描述

这里写图片描述

这里写图片描述

从上面的示例可以看出:
1.红黑树非移动情况下父节点与节点之间不会出现相同的颜色(比如都是红色);
2.红黑树非移动情况下根节点为黑色;
3.红黑树旋转的过程中会出现节点断开的情况,旋转完成恢复为新的树结构;
4.红黑树结构适合随机访问,对于顺序访问采用链表结构效率会更高效。

下面贴上红黑树实现代码:
.h文件:

/*******************************************************************************
*红黑树完整结构
********************************************************************************/
enum Test_Rb_tree_color { _S_red = false, _S_black = true};
struct Test_Rb_tree_node_base
{
	typedef Test_Rb_tree_node_base* Test_Base_ptr;

	Test_Rb_tree_color	_M_color;//颜色
	Test_Base_ptr		_M_parent;//父节点
	Test_Base_ptr		_M_left;//左节点
	Test_Base_ptr		_M_right;//右节点
	//树值
	unsigned int _val;
};

class Test_Rb_tree : public Test_Rb_tree_node_base//红黑树的创建、增加以及删除节点
{
public:
	typedef Test_Rb_tree_node_base* Test_Rb_tree_Ptr;
	Test_Rb_tree();
	virtual ~Test_Rb_tree();

public:
	//添加树节点
	bool Test_insert(unsigned int val);
	//删除树节点
	bool Test_earse(unsigned int val);
	//删除所有节点
	void All_Delete_Rb_tree_node();
	//显示节点信息
	char *ShowNodeInfo();
private:
	void CreateHead(unsigned int val);
	//初始化
	bool Init_node(unsigned int val);
	//创建节点 
	Test_Rb_tree_node_base* Create_Rb_tree_node(unsigned int val);
	//删除节点
	void Delete_Rb_tree_node(Test_Rb_tree_Ptr & ptr);
	//删除所有节点,不平衡树结构
	void Test_M_erase(Test_Rb_tree_Ptr ptr);
	//树结构增加节点
	void Test_Rb_tree_insert();
	//增加或删除节点后平衡树结构
	void Test_Rb_tree_rebalance();
	//返回需要插入值的位置
	Test_Rb_tree_node_base* Test_M_lower_bound(Test_Rb_tree_Ptr x,Test_Rb_tree_Ptr y,unsigned int val);
	//返回查找到的节点位置,如果没有返回空
	Test_Rb_tree_node_base* Test_FindNode(unsigned int val);
	//
	Test_Rb_tree_node_base* Begin();
	//
	Test_Rb_tree_node_base* End();
	//
	Test_Rb_tree_node_base* Left();
	//
	Test_Rb_tree_node_base* Left(Test_Rb_tree_Ptr ptr);
	//
	Test_Rb_tree_node_base* Right();
	//
	Test_Rb_tree_node_base* Right(Test_Rb_tree_Ptr ptr);
	//
	Test_Rb_tree_node_base* Minimum(Test_Rb_tree_Ptr ptr);
	//
	Test_Rb_tree_node_base* Maximum(Test_Rb_tree_Ptr ptr);
	//比较结果小于val为1,如果相等返回0,如果大于val返回-1
	int compare_less(Test_Rb_tree_Ptr x,unsigned int val);
	//
	bool Test_M_insert_(Test_Rb_tree_Ptr x,Test_Rb_tree_Ptr y,unsigned int val);
	//
	bool Test_M_insert_unique_(Test_Rb_tree_Ptr position, const unsigned int & __v);
	//
	long long size();
	//
	bool Test_M_insert_unique(const unsigned int& __v);
	//
	inline void Test_Rb_tree_insert_and_rebalance(const bool __insert_left,
		Test_Rb_tree_node_base* __x,
		Test_Rb_tree_node_base* __p,
		Test_Rb_tree_node_base& __header);//红黑树结构内核实现
	inline Test_Rb_tree_node_base*
		Test_Rb_tree_rebalance_for_erase(Test_Rb_tree_node_base* __z,
		Test_Rb_tree_node_base& __root,
		Test_Rb_tree_node_base*& __leftmost,
		Test_Rb_tree_node_base*& __rightmost);

	inline void _Rb_tree_rotate_left(Test_Rb_tree_node_base* __x, Test_Rb_tree_node_base*& __root);//节点左旋转
	inline void _Rb_tree_rotate_right(Test_Rb_tree_node_base* __x, Test_Rb_tree_node_base*& __root);//节点右旋转
	//写入root信息
	char * WriteRootInfo(char *nodeInfo,Test_Rb_tree_node_base * node,int & noffset,int & nVal);
	//节点信息写入字符串
	char * GetNodeInfo(char *nodeInfo,Test_Rb_tree_node_base * node,int & noffset,int nVal,int left);
	//写入节点数据
	char * WriteNodeInfo(char *nodeInfo,Test_Rb_tree_node_base * node,int & noffset,int nVal,bool left);
private:
	Test_Rb_tree_node_base m_head;
	long long m_node_count;
	char *m_NodeInfo;
};

.cpp文件:

#include "stdafx.h"
#include "testRb_tree.h"

Test_Rb_tree::Test_Rb_tree():m_NodeInfo(0)
{
}

Test_Rb_tree::~Test_Rb_tree()
{
	if (m_NodeInfo)
		delete m_NodeInfo;
	All_Delete_Rb_tree_node();
}

void Test_Rb_tree::CreateHead(unsigned int val)
{
	Init_node(val);
}

//初始化
bool Test_Rb_tree::Init_node(unsigned int val)
{
	if (_S_red != m_head._M_color && _S_black != m_head._M_color)
	{
		m_head._M_left = &m_head;
		m_head._M_right = &m_head;
		m_head._M_parent = 0;
		m_head._M_color = _S_red;
		m_head._val = 0;
		m_node_count = 0;
		return true;
	}
	return false;
}

//创建节点 
Test_Rb_tree_node_base* Test_Rb_tree::Create_Rb_tree_node(unsigned int val)
{
	Test_Rb_tree_node_base* ptr = new Test_Rb_tree_node_base;
	memset(ptr,0,sizeof(ptr));
	ptr->_M_color = _S_red;
	ptr->_val = val;
	return ptr;
}

//添加树数据
bool Test_Rb_tree::Test_insert(unsigned int val)
{
	//判断是否有头节点 如果没有创建头节点
	CreateHead(val);

	Test_Rb_tree_Ptr x = Begin();
	Test_Rb_tree_Ptr y = End();
	Test_Rb_tree_Ptr pbound;
	//首先检查val是否与之前插入过的数据有重复
	if (0 == (pbound = Test_M_lower_bound(x,y,val)))
		return false;

	//增加数节点以及插入数据
	return Test_M_insert_unique_(pbound,val);
}

//
bool Test_Rb_tree::Test_M_insert_unique_(Test_Rb_tree_Ptr position, const unsigned int & __v)
{

	// end()
	if (position == End())
	{
		if (size() > 0 && 1 == compare_less(Right(), __v))
			return Test_M_insert_(0, Right(), __v);
		else
			return Test_M_insert_unique(__v);
	}
	else if (-1 == compare_less(position, __v))
	{
		if (position == Left()) // begin()
			return Test_M_insert_(Left(), Left(), __v);
		else if (position->_M_parent)
		{
			if (position->_M_parent->_M_right == 0)
				return Test_M_insert_(0, position->_M_parent, __v);
			else
				return Test_M_insert_(position, position, __v);
		}
		else
			return Test_M_insert_unique(__v);
	}
	else if (1 == compare_less(position, __v))
	{
		// ... then try after.
		if (position == Right())
			return Test_M_insert_(0, Right(), __v);

		else if (1 == compare_less(position->_M_right, __v))
		{
			if (Right(position->_M_right) == 0)
				return Test_M_insert_(0, position->_M_right, __v);
			else
				return Test_M_insert_(position->_M_right, position->_M_right, __v);
		}
		else
			return Test_M_insert_unique(__v);
	}
		return false;
}

//
bool Test_Rb_tree::Test_M_insert_unique(const unsigned int& __v)
{
	Test_Rb_tree_Ptr __x = Begin();
	Test_Rb_tree_Ptr __y = End();
	bool __comp = true;
	while (__x != 0)
	{
		__y = __x;
		__comp = compare_less(__x,__v);
		__x = __comp ? Left(__x) : Right(__x);
	}
	Test_Rb_tree_Ptr __j = __y;
	if (__comp)
	{
		if (__y == Begin())
			return Test_M_insert_(__x, __y, __v);
		else
			__j = __j->_M_left;
	}
	if (1 == compare_less(__j,__v))
		return Test_M_insert_(__x, __y, __v);
	return false;
}

//
long long Test_Rb_tree::size()
{
	return m_node_count;
}

//
bool Test_Rb_tree::Test_M_insert_(Test_Rb_tree_Ptr x,Test_Rb_tree_Ptr y,unsigned int val)
{
	bool __insert_left = (x != 0 || y == End()
		
		|| -1 == compare_less(y,val));

	Test_Rb_tree_Ptr z = Create_Rb_tree_node(val);
	Test_Rb_tree_insert_and_rebalance(__insert_left, z,
		y,  this->m_head);
	++m_node_count;
	return true;
}

//返回需要插入节点的位置
Test_Rb_tree_node_base* Test_Rb_tree::Test_M_lower_bound(Test_Rb_tree_Ptr x,Test_Rb_tree_Ptr y,unsigned int val)
{
	while (x != 0)
	{
		int nless = compare_less(x, val);
		if (-1 == nless)
			y = x, x = Left(x);
		else if (1 == nless)
			x = Right(x);
		else//如果相等,这条数据不再插入,直接返回
			return 0;
	}
	
	return y;
}

//返回查找到的节点位置,如果没有返回空
Test_Rb_tree_node_base* Test_Rb_tree::Test_FindNode(unsigned int val)
{
	//临时保存需要插入的节点位置
	Test_Rb_tree_Ptr x = Begin();
	while (x != 0)
	{
		int nless = compare_less(x, val);
		if (-1 == nless)
			x = Left(x);
		else if (1 == nless)
			x = Right(x);
		else//如果相等,返回这条数据
			return x;
	}
	return 0;
}

//
int Test_Rb_tree::compare_less(Test_Rb_tree_Ptr x,unsigned int val)
{
	if (x->_val < val)
	{
		return 1;
	}
	else if (x->_val > val)
	{
		return -1;
	}
	return 0;
}

//
Test_Rb_tree_node_base* Test_Rb_tree::Begin()
{
	return m_head._M_parent;
}

//
Test_Rb_tree_node_base* Test_Rb_tree::End()
{
	return &m_head;
}

//
Test_Rb_tree_node_base* Test_Rb_tree::Left()
{
	return m_head._M_left;
}

//
Test_Rb_tree_node_base* Test_Rb_tree::Left(Test_Rb_tree_Ptr ptr)
{
	return ptr->_M_left;
}

//
Test_Rb_tree_node_base* Test_Rb_tree::Right()
{
	return m_head._M_right;
}

//
Test_Rb_tree_node_base* Test_Rb_tree::Right(Test_Rb_tree_Ptr ptr)
{
	return ptr->_M_right;
}

//
Test_Rb_tree_node_base* Test_Rb_tree::Minimum(Test_Rb_tree_Ptr ptr)
{
	while (ptr->_M_left != 0) ptr = ptr->_M_left;
	return ptr;
}

//
Test_Rb_tree_node_base* Test_Rb_tree::Maximum(Test_Rb_tree_Ptr ptr)
{
	while (ptr->_M_right != 0) 
		ptr = ptr->_M_right;
	return ptr;
}

void Test_Rb_tree::Test_Rb_tree_insert_and_rebalance(const bool __insert_left,
	Test_Rb_tree_node_base* __x,
	Test_Rb_tree_node_base* __p,
	Test_Rb_tree_node_base& __header)//红黑树结构内核实现
{
	Test_Rb_tree_node_base **rootptrptr = &__header._M_parent;

	__x->_M_parent = __p;
	__x->_M_left = 0;
	__x->_M_right = 0;
	__x->_M_color = _S_red;

	if(__insert_left){
		__p->_M_left = __x;
		if(__p == &__header){
			__header._M_parent = __x;
			__header._M_right = __x;
		}else if( __p == __header._M_left )
			__header._M_left = __x;
	}else{
		__p->_M_right = __x;
		if(__p == __header._M_right)
			__header._M_right = __x;
	}

	while( __x != *rootptrptr && __x->_M_parent->_M_color==_S_red ){
		Test_Rb_tree_node_base* const xpp = __x->_M_parent->_M_parent;
		if(__x->_M_parent == xpp->_M_left){
			Test_Rb_tree_node_base* const y = xpp->_M_right;
			if(y && y->_M_color == _S_red){
				__x->_M_parent->_M_color = _S_black;
				y->_M_color = _S_black;
				xpp->_M_color = _S_red;
				__x = xpp;
			}else{
				if( __x==__x->_M_parent->_M_right){
					__x = __x->_M_parent;

					_Rb_tree_rotate_left(__x,*rootptrptr);
				}
				__x->_M_parent->_M_color = _S_black;
				xpp->_M_color = _S_red;
				_Rb_tree_rotate_right(xpp,*rootptrptr);
			}
		}else{
			Test_Rb_tree_node_base* const y = xpp->_M_left;
			if(y && y->_M_color == _S_red){
				__x->_M_parent->_M_color = _S_black;
				y->_M_color = _S_black;
				xpp->_M_color = _S_red;
				__x = xpp;
			}else{
				if(__x == __x->_M_parent->_M_left){
					__x = __x->_M_parent;
					_Rb_tree_rotate_right(__x,*rootptrptr);
				}
				__x->_M_parent->_M_color = _S_black;
				xpp->_M_color = _S_red;
				_Rb_tree_rotate_left(xpp,*rootptrptr);
			}
		}
	}
	(*rootptrptr)->_M_color = _S_black;
	return ;
}


//删除树节点
bool Test_Rb_tree::Test_earse(unsigned int val)
{
	if (0 >= size())
		return false;

	Test_Rb_tree_Ptr ptr = Test_FindNode(val);
	if (0 == ptr)
		return false;
	Test_Rb_tree_rebalance_for_erase(ptr,*Begin(),End()->_M_left,End()->_M_right);
	Delete_Rb_tree_node(ptr);
	--m_node_count;
	return true;
}

//删除节点
void Test_Rb_tree::Delete_Rb_tree_node(Test_Rb_tree_Ptr & ptr)
{
	if (ptr)
		delete ptr,ptr = 0;
}

//删除所有节点,不平衡树结构
void Test_Rb_tree::Test_M_erase(Test_Rb_tree_Ptr ptr)
{
	while (ptr != 0)
	{
		Test_M_erase(Right(ptr));
		Test_Rb_tree_Ptr left = Left(ptr);
		Delete_Rb_tree_node(ptr);
		ptr = left;
	}
}

//删除所有节点
void Test_Rb_tree::All_Delete_Rb_tree_node()
{
	Test_M_erase(Begin());
	m_node_count = 0;
	m_head._M_color = Test_Rb_tree_color(-1);
	m_head._M_parent = 0;
	m_head._M_left = &m_head;
	m_head._M_right = &m_head;
}

inline Test_Rb_tree_node_base* Test_Rb_tree::Test_Rb_tree_rebalance_for_erase(Test_Rb_tree_node_base* __z,
	Test_Rb_tree_node_base& __root,
	Test_Rb_tree_node_base*& __leftmost,
	Test_Rb_tree_node_base*& __rightmost)
{
	Test_Rb_tree_node_base* __y = __z;
	Test_Rb_tree_node_base* __x = 0;
	Test_Rb_tree_node_base* __x_parent = 0;
	Test_Rb_tree_node_base* __root_ = &__root;
	if (__y->_M_left == 0)     // __z has at most one non-null child. y == z.
		__x = __y->_M_right;     // __x might be null.
	else
		if (__y->_M_right == 0)  // __z has exactly one non-null child. y == z.
			__x = __y->_M_left;    // __x is not null.
		else {                   // __z has two non-null children.  Set __y to
			__y = __y->_M_right;   //   __z's successor.  __x might be null.
			while (__y->_M_left != 0)
				__y = __y->_M_left;
			__x = __y->_M_right;
		}
		if (__y != __z) {          // relink y in place of z.  y is z's successor
			__z->_M_left->_M_parent = __y; 
			__y->_M_left = __z->_M_left;
			if (__y != __z->_M_right) {
				__x_parent = __y->_M_parent;
				if (__x) __x->_M_parent = __y->_M_parent;
				__y->_M_parent->_M_left = __x;      // __y must be a child of _M_left
				__y->_M_right = __z->_M_right;
				__z->_M_right->_M_parent = __y;
			}
			else
				__x_parent = __y;  
			if (__root_ == __z)
				__root_ = __y;
			else if (__z->_M_parent->_M_left == __z)
				__z->_M_parent->_M_left = __y;
			else 
				__z->_M_parent->_M_right = __y;
			__y->_M_parent = __z->_M_parent;
			//swap(__y->_M_color, __z->_M_color);
			__y = __z;
			// __y now points to node to be actually deleted
		}
		else {                        // __y == __z
			__x_parent = __y->_M_parent;
			if (__x) __x->_M_parent = __y->_M_parent;   
			if (__root_ == __z)
				__root_ = __x;
			else 
				if (__z->_M_parent->_M_left == __z)
					__z->_M_parent->_M_left = __x;
				else
					__z->_M_parent->_M_right = __x;
			if (__leftmost == __z) 
				if (__z->_M_right == 0)        // __z->_M_left must be null also
					__leftmost = __z->_M_parent;
			// makes __leftmost == _M_header if __z == __root
				else
					__leftmost = Minimum(__x);
			if (__rightmost == __z)  
				if (__z->_M_left == 0)         // __z->_M_right must be null also
					__rightmost = __z->_M_parent;  
			// makes __rightmost == _M_header if __z == __root
				else                      // __x == __z->_M_left
					__rightmost = Maximum(__x);
		}
		if (__y->_M_color != _S_red) { 
			while (__x != __root_ && (__x == 0 || __x->_M_color == _S_black))
				if (__x == __x_parent->_M_left) {
					Test_Rb_tree_node_base* __w = __x_parent->_M_right;
					if (__w->_M_color == _S_red) {
						__w->_M_color = _S_black;
						__x_parent->_M_color = _S_red;
						_Rb_tree_rotate_left(__x_parent, __root_);
						__w = __x_parent->_M_right;
					}
					if ((__w->_M_left == 0 || 
						__w->_M_left->_M_color == _S_black) &&
						(__w->_M_right == 0 || 
						__w->_M_right->_M_color == _S_black)) {
							__w->_M_color = _S_red;
							__x = __x_parent;
							__x_parent = __x_parent->_M_parent;
					} else {
						if (__w->_M_right == 0 || 
							__w->_M_right->_M_color == _S_black) {
								if (__w->_M_left) __w->_M_left->_M_color = _S_black;
								__w->_M_color = _S_red;
								_Rb_tree_rotate_right(__w, __root_);
								__w = __x_parent->_M_right;
						}
						__w->_M_color = __x_parent->_M_color;
						__x_parent->_M_color = _S_black;
						if (__w->_M_right) __w->_M_right->_M_color = _S_black;
						_Rb_tree_rotate_left(__x_parent, __root_);
						break;
					}
				} else {                  // same as above, with _M_right <-> _M_left.
					Test_Rb_tree_node_base* __w = __x_parent->_M_left;
					if (__w->_M_color == _S_red) {
						__w->_M_color = _S_black;
						__x_parent->_M_color = _S_red;
						_Rb_tree_rotate_right(__x_parent, __root_);
						__w = __x_parent->_M_left;
					}
					if (__w && (__w->_M_right == 0 || 
						__w->_M_right->_M_color == _S_black) &&
						(__w->_M_left == 0 || 
						__w->_M_left->_M_color == _S_black)) {
							__w->_M_color = _S_red;
							__x = __x_parent;
							__x_parent = __x_parent->_M_parent;
					} else {
						if (__w && (__w->_M_left == 0 || 
							__w->_M_left->_M_color == _S_black)) {
								if (__w->_M_right) __w->_M_right->_M_color = _S_black;
								__w->_M_color = _S_red;
								_Rb_tree_rotate_left(__w, __root_);
								__w = __x_parent->_M_left;
						}
						__w->_M_color = __x_parent->_M_color;
						__x_parent->_M_color = _S_black;
						if (__w->_M_left) __w->_M_left->_M_color = _S_black;
						_Rb_tree_rotate_right(__x_parent, __root_);
						break;
					}
				}
				if (__x) __x->_M_color = _S_black;
		}
		return __y;
}

inline void Test_Rb_tree::_Rb_tree_rotate_left(Test_Rb_tree_node_base* __x, Test_Rb_tree_node_base*& __root)//节点左旋转
{
	Test_Rb_tree_node_base* __y = __x->_M_right;
	__x->_M_right = __y->_M_left;
	if (__y->_M_left !=0)
		__y->_M_left->_M_parent = __x;
	__y->_M_parent = __x->_M_parent;

	if (__x == __root)
		__root = __y;
	else if (__x == __x->_M_parent->_M_left)
		__x->_M_parent->_M_left = __y;
	else
		__x->_M_parent->_M_right = __y;
	__y->_M_left = __x;
	__x->_M_parent = __y;
}

inline void Test_Rb_tree::_Rb_tree_rotate_right(Test_Rb_tree_node_base* __x, Test_Rb_tree_node_base*& __root)//节点右旋转
{
	Test_Rb_tree_node_base* __y = __x->_M_left;
	__x->_M_left = __y->_M_right;
	if (__y->_M_right != 0)
		__y->_M_right->_M_parent = __x;
	__y->_M_parent = __x->_M_parent;

	if (__x == __root)
		__root = __y;
	else if (__x == __x->_M_parent->_M_right)
		__x->_M_parent->_M_right = __y;
	else
		__x->_M_parent->_M_left = __y;
	__y->_M_right = __x;
	__x->_M_parent = __y;
}

//显示节点信息
char * Test_Rb_tree::ShowNodeInfo()
{
	if (0 == m_node_count)
		return 0;
	if (0 != m_NodeInfo)
	{
		delete m_NodeInfo;
		m_NodeInfo = new char[m_node_count * 50];
		memset(m_NodeInfo,0,sizeof(m_NodeInfo));
	}
	else
	{
		m_NodeInfo = new char[m_node_count * 50];
		memset(m_NodeInfo,0,sizeof(m_NodeInfo));
	}
	int noffset = 0;
	int nVal = 0;
	return GetNodeInfo(WriteRootInfo(m_NodeInfo,m_head._M_parent,noffset,nVal),m_head._M_parent,noffset,nVal,-1);
}

//写入root信息
char * Test_Rb_tree::WriteRootInfo(char *nodeInfo,Test_Rb_tree_node_base * node,int & noffset,int & nVal)
{
	if (0 == nodeInfo)
		return 0;
	if (node)
	{
		sprintf(nodeInfo,"The root node is %d\r\n",node->_val);
		noffset = strlen(nodeInfo);
		nVal = node->_val;
		*(nodeInfo + nVal) = 0;
	}
	return nodeInfo;
}

//节点信息写入字符串
char * Test_Rb_tree::GetNodeInfo(char *nodeInfo,Test_Rb_tree_node_base * node,int & noffset,int nVal,int left)
{
	if (0 == nodeInfo)
		return 0;
	if (node)
	{
		if (-1 != left)
			WriteNodeInfo(nodeInfo,node,noffset,nVal,left);

		if (node->_M_left)
			GetNodeInfo(nodeInfo,node->_M_left,noffset,node->_val,1);
		if (node->_M_right)
			GetNodeInfo(nodeInfo,node->_M_right,noffset,node->_val,0);
	}
	return nodeInfo;
}

//写入节点数据
char * Test_Rb_tree::WriteNodeInfo(char *nodeInfo,Test_Rb_tree_node_base * node,int & noffset,int nVal,bool left)
{
	char TempData[100] = {0};
	if (_S_red == node->_M_color)	
		sprintf(TempData,"%d is the red %s node of %d\r\n",node->_val,(left ? "left" : "right"),nVal);
	else if (_S_black == node->_M_color)
		sprintf(TempData,"%d is the black %s node of %d\r\n",node->_val,(left ? "left" : "right"),nVal);
		int nLen = strlen(TempData);
		memcpy(nodeInfo + noffset,TempData,nLen);
		noffset += nLen;
		*(nodeInfo + noffset) = 0;
		return nodeInfo;
}

调用函数说明:

//添加树节点函数
//参数一 节点号(比如10、20等)
bool Test_insert(unsigned int val);

//删除树节点函数
bool Test_earse(unsigned int val);

//删除所有节点 类对象释放时自动调用
void All_Delete_Rb_tree_node();

//显示节点信息
//在字符串中显示当前所有的节点以及节点相应的关系
char *ShowNodeInfo();

下面贴上测试示例:

Test_Rb_tree tree;
tree.Test_insert(10);
tree.Test_insert(20);
tree.Test_insert(30);
tree.Test_insert(50);
tree.Test_insert(40);
tree.Test_insert(100);
tree.Test_insert(60);
tree.Test_insert(70);
tree.Test_insert(80);
tree.Test_insert(90);
tree.Test_insert(120);
tree.Test_insert(110);
char * Test = tree.ShowNodeInfo();

tree.Test_earse(100);
tree.Test_earse(50);
tree.Test_earse(30);
tree.Test_earse(80);
Test = tree.ShowNodeInfo();
tree.All_Delete_Rb_tree_node();

源码下载地址:红黑树文件下载!

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 博客之星2020 设计师:CY__ 返回首页
实付 19.90元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值