C++构建红黑树
2020-12-13 01:57
标签:using include void ota tree pre turn clu err C++构建红黑树 标签:using include void ota tree pre turn clu err 原文地址:https://www.cnblogs.com/SHQHDMR/p/11020415.html#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);
}