C++构建红黑树

Posted shqhdmr

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C++构建红黑树相关的知识,希望对你有一定的参考价值。

#include "iostream"

using namespace std;

class Node

public:
	int data;
	bool red;
	Node *parent = NULL;
	Node *lchild = NULL;
	Node *rchild = NULL;

	Node(int data, bool red = true)
	
		this->data = data;
		this->red = red;
	

	void toggleColor()
	
		this->red = !this->red;
	
;

class RBTree

public:
	Node *root;

	RBTree(int data)
	
		this->root = new Node(data, false);
	

	Node* insert(int data)
	
		Node *current = this->root;
		Node *previous = NULL;

		while (current != NULL)
		
			previous = current;
			if (current->lchild != NULL && current->rchild != NULL &&
				current->red == false && current->lchild->red == true && current->rchild->red == true)
			
				this->flip(current);
				this->solveCollision(current->parent, current);
			
			current = data >= current->data ? current->rchild : current->lchild;
		

		current = new Node(data);
		if (data >= previous->data)
		
			previous->rchild = current;
		
		else
		
			previous->lchild = current;
		
		current->parent = previous;

		this->solveCollision(previous, current);

		return current;
	

protected:
	void flip(Node *root)
	
		if (root != this->root)
		
			root->toggleColor();
		
		root->lchild->toggleColor();
		root->rchild->toggleColor();
	

	void solveCollision(Node *parent, Node *current)
	
		if (parent != NULL && current != NULL && current->red && parent->red)
		
			Node *granpa = parent->parent;
			Node *oldGranpa = granpa->parent;

			if (granpa->lchild == parent)
			
				if (parent->lchild == current)
				
					this->leftOutterRotate(oldGranpa, granpa, parent, current);
				
				else
				
					this->leftInnerRotate(oldGranpa, granpa, parent, current);
				
			
			if (granpa->rchild == parent)
			
				if (parent->rchild == current)
				
					this->rightOutterRotate(oldGranpa, granpa, parent, current);
				
				else
				
					this->rightInnerRotate(oldGranpa, granpa, parent, current);
				
			
		
	

	void leftOutterRotate(Node* oldGranpa, Node* granpa, Node* parent, Node* current)
	
		//toggle color
		granpa->toggleColor();
		parent->toggleColor();
		//rotate
		if (oldGranpa == NULL)
		
			this->root = parent;
		
		else
		
			oldGranpa->lchild = parent;
		
		granpa->lchild = parent->rchild;
		parent->rchild = granpa;
		parent->parent = oldGranpa;
		granpa->parent = parent;
	

	void leftInnerRotate(Node* oldGranpa, Node* granpa, Node* parent, Node* current)
	
		parent->rchild = current->lchild;
		current->lchild = parent;
		granpa->lchild = current;
		current->parent = granpa;
		parent->parent = current;
		this->leftOutterRotate(oldGranpa, granpa, current, parent);
	

	void rightOutterRotate(Node* oldGranpa, Node* granpa, Node* parent, Node* current)
	
		//toggle color
		granpa->toggleColor();
		parent->toggleColor();
		//rotate
		if (oldGranpa == NULL)
		
			this->root = parent;
		
		else
		
			oldGranpa->rchild = parent;
		
		granpa->rchild = parent->lchild;
		parent->lchild = granpa;
		parent->parent = oldGranpa;
		granpa->parent = parent;
	

	void rightInnerRotate(Node* oldGranpa, Node* granpa, Node* parent, Node* current)
	
		parent->lchild = current->rchild;
		current->rchild = parent;
		granpa->rchild = current;
		current->parent = granpa;
		parent->parent = current;
		this->rightOutterRotate(oldGranpa, granpa, current, parent);
	
;


void main()

	RBTree *rbt = new RBTree(50);

	rbt->insert(25);
	rbt->insert(75);
	rbt->insert(90);
	rbt->insert(80);

  

以上是关于C++构建红黑树的主要内容,如果未能解决你的问题,请参考以下文章

C++ 红黑树

C++红黑树

C++红黑树

C++红黑树

C++之红黑树

C++进阶第十九篇——红黑树(概念+代码实现)