用c++实现红黑树的插入、删除、遍历操作

完整详细版红黑树-c++完整实现:https://www.cnblogs.com/skywang12345/p/3624291.html
红黑树是一棵二叉搜索树,它在每个节点上增加了一个存储位来表示节点的颜色,可以是Red或Black。通过对任何一条从根到叶子简单路径上的颜色来约束,红黑树保证最长路径不超过最短路径的两倍,因而近似于平衡。

红黑树是满足下面红黑性质的二叉搜索树:

  1. 每个节点,不是红色就是黑色的;
  2. 根节点是黑色的;
  3. 如果一个节点是红色的,则它的两个子节点是黑色的;
  4. 对每个节点,从该节点到其所有后代叶节点的简单路径上,均包含相同数目的黑色节点;
  5. 每个叶子节点都是黑色的(这里的叶子节点是指的空节点)

思考:为什么满足上面的颜色约束性质,红黑树能保证最长路径不超过最短路径的两倍?

如图:所能增加的红节点数最多和黑节点数目一样多,故红黑树能保证最长路径不超过最短路径的两倍。
在这里插入图片描述
一、判断是否是红黑树:


//判断是否是红黑树
	bool isRBTree()
	{
		int BlackNodeNum = 0;
		int curBlackNodeNum = 0;
		Node* cur = _root;
 
		while (cur)
		{
			if (cur->_col == BLACK)
			{
				BlackNodeNum++;
			}
 
			cur = cur->_left;
		}
		return _isRBTree(_root, BlackNodeNum, curBlackNodeNum);

}
bool _isRBTree(Node* root, int BlackNodeNum, int curBlackNodeNum)
	{
		if (root == NULL)
		{
			return true;
		}
 
		if (root->_col == BLACK)
		{
			curBlackNodeNum++;
		}
 
		if (BlackNodeNum == curBlackNodeNum)
		{
			if (root->_parent == NULL)
			{
				return true;
			}
			else if (root->_col == RED && root->_col == root->_parent->_col)
			{
				return false;
			}
			else
			{
				return true;
			}
		}
 
		return _isRBTree(root->_left, BlackNodeNum, curBlackNodeNum) && _isRBTree(root->_right, BlackNodeNum, curBlackNodeNum);
	}


二、红黑树的中序遍历:

//中序遍历
	void InOrder()
	{
		_InOrder(_root);
	}
void _InOrder(Node* root)
	{
		if (root == NULL)
		{
			return;
		}
 
		_InOrder(root->_left);
		cout << root->_key << " ";
		_InOrder(root->_right);
	}



void _InOrder(Node* root)
	{
		if (root == NULL)
		{
			return;
		}
 
		_InOrder(root->_left);
		cout << root->_key << " ";
		_InOrder(root->_right);
	}

三、红黑树的删除:

//删除 
	bool remove(const K& key){
		Node *cur = NULL;
		Node *parent = NULL;
		cur = _root;
		while(cur != NULL){
			if(key < cur->_key) cur = cur->_left;
			if(key > cur->_key) cur = cur->_right;
			if(key == cur->_key){
				break;
			} 
		}
		
		if(cur == NULL) return false;
		
		Node *instead = NULL;
		instead = cur;
		
		if(instead->_left != NULL){
		    instead = instead->_left;
			while(instead != NULL){  
			   parent = instead;
			   instead = instead->_right;   
		    }
		    cur->_key = parent->_key;
		    cur->_value = parent->_value;
		    parent->_parent->_right = NULL;
		    free(parent);
				
		}
		else if(instead->_right != NULL){
			instead = instead->_right;
			while(instead != NULL){
				parent = instead;
				instead = instead->_left;
			}
			cur->_key = parent->_key;
			cur->_value = parent->_value;
			parent->_parent->_left = NULL;
			free(parent);
	 
		}
		else if(instead->_left == NULL && instead->_right == NULL){
			Node *gradfather = NULL;
			gradfather = instead->_parent;
			if(gradfather->_key > instead->_key) gradfather->_left = NULL;
			else if(gradfather->_key < instead->_key) gradfather->_right = NULL;
			free(instead);
			
		}
		 
	}
    
    

四、左单旋
在这里插入图片描述

//左单旋
	void RotateL(Node*& parent)
	{
		Node* subR = parent->_right;
		Node* subRL = subR->_left;
 
		parent->_right = subRL;
		if (subRL)
		{
			subRL->_parent = parent;
		}
 
		subR->_left = parent;
		subR->_parent = parent->_parent;
		parent->_parent = subR;
		parent = subR;
 
		if (parent->_parent == NULL)
		{
			_root = parent;
		}
		else if (parent->_parent->_key > parent->_key)
		{
			parent->_parent->_left = parent;
		}
		else if ( parent->_parent->_key<parent->_key )
		{
			parent->_parent->_right = parent;
		}
	}


五、右单旋
在这里插入图片描述

//右单旋
	void RotateR(Node*& parent)
	{
		Node* subL = parent->_left;
		Node* subLR = subL->_right;
 
		parent->_left = subLR;
		if (subLR)
		{
			subLR->_parent = parent;
		}
 
		subL->_right = parent;
		subL->_parent = parent->_parent;
		parent->_parent = subL;
 
		parent = subL;
 
		if (parent->_parent == NULL)
		{
			_root = parent;
		}
		else if (parent->_parent->_key > parent->_key)
		{
			parent->_parent->_left = parent;
		}
		else if (parent->_parent->_key < parent->_key)
		{
			parent->_parent->_right = parent;
		}
 
	}


六、插入的三种情况
ps:cur为当前节点,p为父节点,g为祖父节点,u为叔叔节点
1.第一种情况
cur为红,p为红,g为黑,u存在且为红,则将p,u改为黑,g改为红,然后把g当成cur,继续向上调整。
在这里插入图片描述
2.第二种情况
cur为红,p为红,g为黑,u不存在/u为黑
p为g的左孩子,cur为p的左孩子,则进行右单旋转;相反,p为g的右孩子,cur为p的右孩子,则进行左单旋转,p、g变色–p变黑,g变红
在这里插入图片描述
3.第三种情况
cur为红,p为红,g为黑,u不存在/u为黑
p为g的左孩子,cur为p的右孩子,则针对p做左单旋转;相反,p为g的右孩子,cur为p的左孩子,则针对p做右单旋转,则转换成了情况2
在这里插入图片描述
上面已经把每种情况基本列出来了,其他相反的情况类似,反过来写一下就行了,具体详细过程参考代码。


//红黑树的插入操作
	bool Insert(const K& key, const V& value)
	{
		if (_root == NULL)
		{
			_root = new Node(key, value);
			_root->_col = BLACK;
			return true;
		}
 
		Node* parent = NULL;
		Node* cur = _root;
		while (cur)
		{
			if (cur->_key > key)
			{
				parent = cur;
				cur = cur->_left;
			}
			else if (cur->_key < key)
			{
				parent = cur;
				cur = cur->_right;
			}
			else
			{
				return false;
			}
		}
 
		//插入位置  
		if (parent->_key >key)
		{
			cur = new Node(key, value);
			parent->_left = cur;
			cur->_parent = parent;
		}
		else if (parent->_key < key)
		{
			cur = new Node(key, value);
			parent->_right = cur;
			cur->_parent = parent;
		}
 
		//插入以后,进行调整  
		while (cur != _root && parent->_col == RED)
		{
			Node* grandfather = parent->_parent;
			Node* uncle = NULL;
			//左边的情况  
			if (parent == grandfather->_left)
			{
				//情况一  
				uncle = grandfather->_right;
				if (uncle && uncle->_col == RED)
				{
					//1. 不需要旋转  
					if (cur == parent->_left)
					{
						grandfather->_col = RED;
						parent->_col = BLACK;
						uncle->_col = BLACK;
 
						cur = grandfather;
						parent = cur->_parent;
					}
 
					//2.需要旋转  
					else if (cur == parent->_right)
					{
						RotateL(parent);
						grandfather->_col = RED;
						parent->_col = BLACK;
						uncle->_col = BLACK;
 
						cur = grandfather;
						parent = cur->_parent;
					}
 
				}
 
				//情况二,三 
				else if (uncle == NULL || (uncle && uncle->_col == BLACK))
				{
					if (cur == parent->_right)
					{
						RotateL(parent);
					}
					parent->_col = BLACK;
					grandfather->_col = RED;
					RotateR(grandfather);
					break;
				}
			}
			//右边的情况  
			else if (parent == grandfather->_right)
			{
				//情况一
				uncle = grandfather->_left;
				if (uncle && uncle->_col == RED)
				{
					//1.不需要旋转  
					if (cur == parent->_right)
					{
						uncle->_col = BLACK;
						grandfather->_col = RED;
						parent->_col = BLACK;
 
						cur = grandfather;
						parent = cur->_parent;
					}
 
					//2.需要旋转  
					else if (cur == parent->_left)
					{
						uncle->_col = BLACK;
						grandfather->_col = RED;
						parent->_col = BLACK;
						RotateR(parent);
 
						cur = grandfather;
						parent = cur->_parent;
					}
				}
				//情况二,三
				else if (uncle == NULL || (uncle && uncle->_col == BLACK))
				{
					if (cur == parent->_left)
					{
						RotateR(parent);
					}
					parent->_col = BLACK;
					grandfather->_col = RED;
					RotateL(grandfather);
					break;
				}
			}
		}
		_root->_col = BLACK;
		return true;
	}

七、删除操作:请点击参考大牛的博客

红黑树和AVL树的比较:

红黑树和AVL树都是高效的平衡二叉树,增删查改的时间复杂度都是O(lg(N)),红黑树的不追求完全平衡,保证最长路径不超过最短路径的2倍,相对而言,降低了旋转的要求,所以性能会优于AVL树,所以实际运用中红黑树更多。

完整代码及测试用例:

#ifndef __RBTree_h__
#define __RBTree_h__

#include<iostream>
using namespace std;
 
enum colour
{
	RED,
	BLACK,
};
 
template<class K, class V>
struct RBTreeNode
{
	int _col;
	K _key;
	V _value;
	long long _value_cot;
	long long _sum_cot;
	RBTreeNode<K, V>* _left;
	RBTreeNode<K, V>* _right;
	RBTreeNode<K, V>* _parent;
 
	RBTreeNode(const K& key, const V& value):
		_col(RED),
		_key(key),
	    _value(value),
		_value_cot(1),
		_sum_cot(0),
		_left(NULL),
		_right(NULL),
		_parent(NULL)
	{}
 
};
 
 
template<class K, class V>
class RBTree
{
	typedef RBTreeNode<K, V> Node;
public:
	RBTree()
		:_root(NULL)
	{}
 
	//红黑树的插入操作
	bool Insert(const K& key, const V& value)
	{
		if (_root == NULL)
		{
			_root = new Node(key, value);
			_root->_col = BLACK;
			return true;
		}
 
		Node* parent = NULL;
		Node* cur = _root;
		while (cur)
		{
			if (cur->_key > key)
			{
				parent = cur;
				cur = cur->_left;
			}
			else if (cur->_key < key)
			{
				parent = cur;
				cur = cur->_right;
			}
			else
			{
				cur->_value_cot++;
				return true;
			}
		}
 
		//插入位置  
		if (parent->_key >key)
		{
			cur = new Node(key, value);
			parent->_left = cur;
			cur->_parent = parent;
		}
		else if (parent->_key < key)
		{
			cur = new Node(key, value);
			parent->_right = cur;
			cur->_parent = parent;
		}
 
		//插入以后,进行调整  
		while (cur != _root && parent->_col == RED)
		{
			Node* grandfather = parent->_parent;
			Node* uncle = NULL;
			//左边的情况  
			if (parent == grandfather->_left)
			{
				//情况一  
				uncle = grandfather->_right;
				if (uncle && uncle->_col == RED)
				{
					//1. 不需要旋转  
					if (cur == parent->_left)
					{
						grandfather->_col = RED;
						parent->_col = BLACK;
						uncle->_col = BLACK;
 
						cur = grandfather;
						parent = cur->_parent;
					}
 
					//2.需要旋转  
					else if (cur == parent->_right)
					{
						RotateL(parent);
						grandfather->_col = RED;
						parent->_col = BLACK;
						uncle->_col = BLACK;
 
						cur = grandfather;
						parent = cur->_parent;
					}
 
				}
 
				//情况二,三 
				else if (uncle == NULL || (uncle && uncle->_col == BLACK))
				{
					if (cur == parent->_right)
					{
						RotateL(parent);
					}
					parent->_col = BLACK;
					grandfather->_col = RED;
					RotateR(grandfather);
					break;
				}
			}
			//右边的情况  
			else if (parent == grandfather->_right)
			{
				//情况一
				uncle = grandfather->_left;
				if (uncle && uncle->_col == RED)
				{
					//1.不需要旋转  
					if (cur == parent->_right)
					{
						uncle->_col = BLACK;
						grandfather->_col = RED;
						parent->_col = BLACK;
 
						cur = grandfather;
						parent = cur->_parent;
					}
 
					//2.需要旋转  
					else if (cur == parent->_left)
					{
						uncle->_col = BLACK;
						grandfather->_col = RED;
						parent->_col = BLACK;
						RotateR(parent);
 
						cur = grandfather;
						parent = cur->_parent;
					}
				}
				//情况二,三
				else if (uncle == NULL || (uncle && uncle->_col == BLACK))
				{
					if (cur == parent->_left)
					{
						RotateR(parent);
					}
					parent->_col = BLACK;
					grandfather->_col = RED;
					RotateL(grandfather);
					break;
				}
			}
		}
		_root->_col = BLACK;
		return true;
	}
	
	//删除 
	bool remove(const K& key){
		Node *cur = NULL;
		Node *parent = NULL;
		cur = _root;
		while(cur != NULL){
			if(key < cur->_key) cur = cur->_left;
			if(key > cur->_key) cur = cur->_right;
			if(key == cur->_key){
				break;
			} 
		}
		
		if(cur == NULL) return false;
		
		Node *instead = NULL;
		instead = cur;
		
		if(instead->_left != NULL){
		    instead = instead->_left;
			while(instead != NULL){  
			   parent = instead;
			   instead = instead->_right;   
		    }
		    cur->_key = parent->_key;
		    cur->_value = parent->_value;
		    parent->_parent->_right = NULL;
		    free(parent);
				
		}
		else if(instead->_right != NULL){
			instead = instead->_right;
			while(instead != NULL){
				parent = instead;
				instead = instead->_left;
			}
			cur->_key = parent->_key;
			cur->_value = parent->_value;
			parent->_parent->_left = NULL;
			free(parent);
	 
		}
		else if(instead->_left == NULL && instead->_right == NULL){
			Node *gradfather = NULL;
			gradfather = instead->_parent;
			if(gradfather->_key > instead->_key) gradfather->_left = NULL;
			else if(gradfather->_key < instead->_key) gradfather->_right = NULL;
			free(instead);
			
		}
		 
	}
    
    
    
    
    //查询第 k 大 
    K query(long long k){
    	_updata_value_cot(_root);
    	
    	if(k < _root->_sum_cot) return false;
    	Node *cur = NULL;
    	cur = _root;
    	long long cur_left_sum = 0;
    	while(cur != NULL){
    		if(cur->_left == NULL)  cur_left_sum = 0;
    		else cur_left_sum = cur->_left->_sum_cot;
			
			if(k == cur_left_sum + 1) return cur->_key;
    		else if(k < cur_left_sum + 1){
    			cur = cur->left;
			}
			else if(k > cur_left_sum + 1){
				cur = cur->_right;
				k -= cur_left_sum + 1;
			}
		}
	}
	
	
	
	//中序遍历
	void InOrder()
	{
		_InOrder(_root);
	}
	
	
	
	
	
	
 
protected:
	
	void _InOrder(Node* root)
	{
		if (root == NULL)
		{
			return;
		}
 
		_InOrder(root->_left);
		for(int i = 0; i < root->_value_cot; i++)
		cout << root->_key << " ";
		_InOrder(root->_right);
	}
 
	//左单旋
	void RotateL(Node*& parent)
	{
		Node* subR = parent->_right;
		Node* subRL = subR->_left;
 
		parent->_right = subRL;
		if (subRL)
		{
			subRL->_parent = parent;
		}
 
		subR->_left = parent;
		subR->_parent = parent->_parent;
		parent->_parent = subR;
		parent = subR;
 
		if (parent->_parent == NULL)
		{
			_root = parent;
		}
		else if (parent->_parent->_key > parent->_key)
		{
			parent->_parent->_left = parent;
		}
		else if ( parent->_parent->_key<parent->_key )
		{
			parent->_parent->_right = parent;
		}
	}
 
	//右单旋
	void RotateR(Node*& parent)
	{
		Node* subL = parent->_left;
		Node* subLR = subL->_right;
 
		parent->_left = subLR;
		if (subLR)
		{
			subLR->_parent = parent;
		}
 
		subL->_right = parent;
		subL->_parent = parent->_parent;
		parent->_parent = subL;
 
		parent = subL;
 
		if (parent->_parent == NULL)
		{
			_root = parent;
		}
		else if (parent->_parent->_key > parent->_key)
		{
			parent->_parent->_left = parent;
		}
		else if (parent->_parent->_key < parent->_key)
		{
			parent->_parent->_right = parent;
		}
 
	}
	
	
    long long updata_value_cot(Node *root){
    	
    	if(root == NULL) return 0;
    	root->_sum_cot = 1;
    	root->_sum_cot += updata_value_cot(root->_left) + updata_value_cot(root->_right);
    	  	
	}
	
	
    
    	
	
protected:
	Node* _root;       
};

#endif   /*__RBTree_h__*/ 
#include<bits/stdc++.h>

#include "RBTree.h"


void TestRBtree()
{
	RBTree<int, int>RBT;
	int n;
	
    cin>>n;
	for (int i = 0; i < n; i++)
	{
		RBT.Insert(rand(), i);
	}
	
	RBT.InOrder();
	cout << endl;
	
	cin>>n;
	RBT.remove(n);
	
	
	RBT.InOrder();
	cout << endl;
 
	
}
 
int main()
{
	TestRBtree();
	system("pause");
	return 0;
}

 

这个代码是有bug的,我暂时懒得改了,debug太累了qwq

下面放一个带插入、删除的普通二叉排序树

/*
*文件名:RBTree.h
*/
#ifndef __sorttree_h__
#define __sorttree_h__


struct stree ;   //存根节点 
struct stree_node;  //二叉树的节点单元 

struct stree *sorttree_init();   //初始化 
bool sorttree_insert(struct stree *root, int key);  //插入 


struct stree_node *Lmax(struct stree_node *root);  //找左子树最大节点 
struct stree_node *Rmin(struct stree_node *root);  //找右子树最小节点 
bool sorttree_remove(struct stree *root, int key);  //删除节点 
void _inorder(struct stree_node *node);   //中序遍历 
void sorttree_inorder(struct stree *root) ;

void _clear(struct stree_node * node);  //清空二叉树 
void sorttree_clear(struct stree *root);   
void sorttree_free(struct stree *root);  //释放二叉树 




#endif /*__sorttree_h__*/ 


/*
*文件名:RBTree.c
*/
#include<stdlib.h> 
#include<assert.h>
#include<stdio.h>
#include "RbTree.h"
 
struct stree_node{
	int _key;
	int _cot;
	struct stree_node *_parent;
	struct stree_node *_left;
	struct stree_node *_right; 
};
struct stree{
	struct stree_node *_root;
	int count;
}; 

struct stree *sorttree_init(){
	struct stree *root = NULL;
	root = (struct stree *)malloc(sizeof(struct stree));
	assert(root != NULL);
	if(root == NULL) return NULL;
	 root->count = 0;
	 root->_root = NULL;
	 return root;
} 


bool sorttree_insert(struct stree *root, int key){	
	assert(root != NULL);
	if(root ==NULL) return false;
	
	root->count++;
	struct stree_node *son = NULL;                           //创建节点 
	son = (struct stree_node *)malloc(sizeof(struct stree_node));
	son->_key = key;
	son->_cot = 1;
	son->_parent = NULL;
	son->_left = NULL;
	son->_right = NULL;
	
	if(root->_root == NULL) {         // 头节点为空,插入 
		root->_root = son;
		return true; 
	}
		
	struct stree_node *cur = NULL;           //找到 
	struct stree_node *parent = NULL;
	cur = root->_root;
	while(cur != NULL){
		parent = cur;
		if(key < cur->_key) cur = cur->_left;
		else if(key > cur->_key) cur = cur->_right;
		else if(key == cur->_key) {
			break;
		}
	}
	
	if(key == parent->_key) parent->_cot++;
	if(key < parent->_key){
		parent->_left = son ;
		son->_parent = parent;
	} 
	if(key > parent->_key){
		parent->_right = son;
		son->_parent = parent;
	}
	return true;
}


struct stree_node *Lmax(struct stree_node *root){
	struct stree_node *cur = NULL;
	cur = root;
	while(cur->_right != NULL){
		cur = cur->_right;
	}
	return cur;
	
}
struct stree_node *Rmin(struct stree_node *root){
	struct stree_node *cur = NULL;
	cur = root;
	while(cur->_left != NULL){
		cur = cur->_left;
	}
	return cur;
}

bool sorttree_remove(struct stree *root, int key){
	assert(root != NULL);
	if(root == NULL) return false;

	root->count--;
	struct stree_node *cur = NULL;
	struct stree_node *parent = NULL;
	cur = root->_root;
	 
	while(cur != NULL){          //找到要删除的节点
//		parent = cur;
		if(key < cur->_key) cur = cur->_left;
		else if(key > cur->_key) cur = cur->_right;
		else if(key == cur->_key) break; 
	}
	
	if(cur == NULL) return false;   //找不到要删除的节点 
	parent = cur; 
	struct stree_node *replace = NULL;
	replace = parent;    //被删节点 - replace 
	
	if(replace != root->_root)  parent = replace->_parent;  //记录被删节点的父亲节点, 
	else parent = NULL; //被删节点为根节点,把父亲节点赋空 
	
	if(replace->_left == NULL && replace->_right == NULL){  //没有左右子树 
		if(replace == root->_root) {   //要删除节点为根节点 
		     root->_root = NULL;
		     delete replace;
		     return true;
		}
		if(replace == parent->_left) parent->_left = NULL;
		else parent->_right = NULL;
		delete replace;
	}
	else if(replace->_left != NULL){       //从左子树找到替换节点替换被删节点,删掉替换节点 
		struct stree_node *sub = NULL;
		struct stree_node *subL = NULL; 
		sub =  Lmax(replace->_left);	//替换节点 
		parent = sub->_parent;         //替换节点的父亲 
		subL = sub->_left;        //替换节点的左儿子 
		
		replace->_key = sub->_key;
		replace->_cot = sub->_cot;
		
		if(sub == parent->_left) parent->_left = subL;  //删掉替换节点 
		else parent->_right = subL;
		if(subL != NULL) subL->_parent = parent;
		delete sub;
		
		}
	else if(replace->_right != NULL){     //从右子树找到替换节点替换被删节节点,删掉替换节点 
		struct stree_node *sub = NULL;
		struct stree_node *subR = NULL;
		sub = Rmin(replace->_right);   //找到替换节 
		parent = sub->_parent;        //替换节点的父亲 
		subR = sub->_right;           //替换节点的右儿子 
		
		replace->_key = sub->_key;      
		replace->_cot = sub->_cot;
		
		if(sub == parent->_left) parent->_left = subR;
		else parent->_right = subR;
		if(subR != NULL) subR->_parent = parent;
		delete sub;
	} 
}

void _inorder(struct stree_node *node){
	if(node == NULL) return;
	_inorder(node->_left);
	printf("%d ", node->_key);
	_inorder(node->_right);
}

void sorttree_inorder(struct stree *root){
	assert(root != NULL);
	if(root == NULL) return;
	_inorder(root->_root);
}


void _clear(struct stree_node * node){
	if(node == NULL) return;
	_clear(node->_left);
	_clear(node->_right);
	free(node);
	
}

void sorttree_clear(struct stree *root){
	assert(root != NULL);
	if(root == NULL) return;
	_clear(root->_root);
	root->_root = NULL;
	
}

void sorttree_free(struct stree *root){
	assert(root != NULL);
	if(root == NULL) return;
	_clear(root->_root);
	free(root);
	return;
}
#include<bits/stdc++.h>

#include "RBTree.h"
using namespace std;
struct stree *mroot = NULL;
void remove(){
	int  cnt, vis;
	int tmp[1000];
	printf("\n排序后的随机数:\n");
	sorttree_inorder(mroot);
	printf("\n\n\n");
	memset(tmp, 0, sizeof(tmp));
	printf("请输入您要删除的个数: ");
	scanf("%d", &cnt);
	printf("\n请输入您要删除的数: ");
	int t = 0;
	for(int i = 1; i <= cnt; i++){
		scanf("%d", &vis);
		if(!sorttree_remove(mroot, vis)) tmp[t++] = i;
	} 
	printf("\n");
	for(int i = 0; i < t; i++)
	printf("第 %d 个数不存在\n", tmp[i]);
	
	printf("\n删除后的有序数:\n");
	sorttree_inorder(mroot);
	printf("\n");
	
	
	return; 
	 
	
} 



void look(){
	printf("排序后的随机数:\n");
	sorttree_inorder(mroot); 
	printf("\n");
}

void insert(){
	int vis, cnt;
	printf("请输入您要增加的随机数个数:"); 
	scanf("%d", &cnt);
	for(int i = 0; i < cnt; i++)
	{
		vis = rand();
		sorttree_insert(mroot, vis);
	}
	printf("\n\n排序后的随机数为: \n");
	sorttree_inorder(mroot);
	printf("\n");
	
}
 
int main()
{
	
	mroot = sorttree_init();
	
	while(1){
		system("cls");
		cout<<"************************************************\n";
		cout<<"               退出        0\n";
		cout<<"               插入        1\n";
		cout<<"               查看        2\n";
		cout<<"               删除        3\n"; 
		cout<<"************************************************\n";
		cout<<"\n请选择您要进行的操作:\n";
		char x;
		cin>>x;	
		switch(x){
			
			case '0': return 0;
			case '1':insert(); break;
		    case '2':look(); break;
		    case '3':remove(); break;    
		    default:cout<<"无此功能,请重新选择\n"; break; 
		    }
		    cout<<"\n\n按 0 返回上一级菜单\n";
	        system("pause");	        
		
		
	}

  
	
}

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 红黑树有下面几种操作:1. 插入:在红黑树插入一个新的节点;2. 删除:从红黑树删除一个节点;3. 查找:在红黑树中查找一个特定的节点;4. 遍历遍历红黑树中的所有节点。使用C语言实现红黑树操作代码可以参考下面的示例:1. 插入:/* Insert a node into the Red-Black Tree */ void rb_insert(struct rb_node *node, struct rb_root *root) { struct rb_node *parent = NULL; struct rb_node **link = &root->rb_node; /* Find the place to insert the new node */ while (*link) { parent = *link; if (node->key < parent->key) link = &(*link)->rb_left; else link = &(*link)->rb_right; } /* Insert the new node into the tree */ node->rb_parent = parent; node->rb_left = node->rb_right = NULL; *link = node; /* Fix any violations of the Red-Black Tree properties */ rb_insert_fixup(node, root); }2. 删除:/* Delete a node from the Red-Black Tree */ void rb_delete(struct rb_node *node, struct rb_root *root) { struct rb_node *child, *parent; /* If the node to be deleted has two children, replace it with its successor */ if (node->rb_left && node->rb_right) { struct rb_node *successor = rb_successor(node); node->key = successor->key; node = successor; } /* Get a pointer to the node's parent and child */ parent = node->rb_parent; child = node->rb_left ? node->rb_left : node->rb_right; /* Remove the node from the tree */ if (child) child->rb_parent = parent; if (parent) { if (node == parent->rb_left) parent->rb_left = child; else parent->rb_right = child; } else root->rb_node = child; /* Fix any violations of the Red-Black Tree properties */ rb_delete_fixup(node, root); }3. 查找:/* Find a node in the Red-Black Tree */ struct rb_node *rb_search(int key, struct rb_root *root) { struct rb_node *node = root->rb_node; while (node) { if (key < node->key) node = node->rb_left; else if (key > node->key) node = node->rb_right; else return node; } return NULL; }4. 遍历:/* Traverse the Red-Black Tree in-order */ void rb_inorder_traverse(struct rb_node *node, void (*func)(struct rb_node *)) { if (!node) return; rb_inorder_traverse(node->rb_left, func); func(node); rb_inorder_traverse(node->rb_right, func); } ### 回答2: 红黑树是一种自平衡的二叉搜索树,它具有以下特点:节点要么是红色,要么是黑色;根节点是黑色;每个叶子节点(NIL节点,空节点)是黑色;如果一个节点是红色,则它的两个子节点都是黑色;对于每个节点,从该节点到其所有后代叶子节点的简单路径上,均包含相同数量的黑色节点。 以下是使用C语言实现红黑树的所有操作的代码: ```c #include <stdio.h> #include <stdlib.h> enum Color {RED, BLACK}; typedef struct Node { int data; enum Color color; struct Node *left, *right, *parent; } Node; Node* createNode(int data) { Node* newNode = (Node*)malloc(sizeof(Node)); newNode->data = data; newNode->color = RED; newNode->left = newNode->right = newNode->parent = NULL; return newNode; } Node* bstInsert(Node* root, Node* newNode) { if (root == NULL) return newNode; if (newNode->data < root->data) { root->left = bstInsert(root->left, newNode); root->left->parent = root; } else if (newNode->data > root->data) { root->right = bstInsert(root->right, newNode); root->right->parent = root; } return root; } void swapColors(Node* a, Node* b) { enum Color temp; temp = a->color; a->color = b->color; b->color = temp; } void rotateLeft(Node** root, Node* x) { Node* y = x->right; x->right = y->left; if (y->left != NULL) y->left->parent = x; y->parent = x->parent; if (x->parent == NULL) *root = y; else if (x == x->parent->left) x->parent->left = y; else x->parent->right = y; y->left = x; x->parent = y; } void rotateRight(Node** root, Node* x) { Node* y = x->left; x->left = y->right; if (y->right != NULL) y->right->parent = x; y->parent = x->parent; if (x->parent == NULL) *root = y; else if (x == x->parent->right) x->parent->right = y; else x->parent->left = y; y->right = x; x->parent = y; } void fixViolation(Node** root, Node* newNode) { Node* parent = NULL; Node* grandParent = NULL; while ((newNode != *root) && (newNode->color != BLACK) && (newNode->parent->color == RED)) { parent = newNode->parent; grandParent = newNode->parent->parent; if (parent == grandParent->left) { Node* uncle = grandParent->right; if (uncle != NULL && uncle->color == RED) { grandParent->color = RED; parent->color = BLACK; uncle->color = BLACK; newNode = grandParent; } else { if (newNode == parent->right) { rotateLeft(root, parent); newNode = parent; parent = newNode->parent; } rotateRight(root, grandParent); swapColors(parent, grandParent); newNode = parent; } } else { Node* uncle = grandParent->left; if ((uncle != NULL) && (uncle->color == RED)) { grandParent->color = RED; parent->color = BLACK; uncle->color = BLACK; newNode = grandParent; } else { if (newNode == parent->left) { rotateRight(root, parent); newNode = parent; parent = newNode->parent; } rotateLeft(root, grandParent); swapColors(parent, grandParent); newNode = parent; } } } (*root)->color = BLACK; } Node* insertNode(Node* root, int data) { Node* newNode = createNode(data); root = bstInsert(root, newNode); fixViolation(&root, newNode); return root; } void inOrderTraversal(Node* root) { if (root == NULL) return; inOrderTraversal(root->left); printf("%d ", root->data); inOrderTraversal(root->right); } int main() { Node* root = NULL; root = insertNode(root, 10); root = insertNode(root, 20); root = insertNode(root, 30); root = insertNode(root, 40); root = insertNode(root, 50); root = insertNode(root, 60); printf("In-order traversal of the constructed Red-Black tree is: "); inOrderTraversal(root); return 0; } ``` 上述代码实现红黑树插入操作,并进行了适当的旋转和颜色交换以保证红黑树的特性。在主函数中,将节点插入红黑树中,并进行中序遍历输出。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值