学习日记04
算法
树:实现插入
#include <iostream>
#include <cstdlib>
struct TreeNode {
int value;
int color;
TreeNode* parent;
TreeNode* rightchild;
TreeNode* leftchild;
};
class myMap {
public:
TreeNode* root;
public:
myMap(int _value) {
root = new TreeNode();
root->color = 2;
root->parent = nullptr;
root->value = _value;
root->leftchild = nullptr;
root->rightchild = nullptr;
}
TreeNode* init(int _value) {
root = new TreeNode();
root->color = 2;
root->parent = nullptr;
root->value = _value;
root->leftchild = nullptr;
root->rightchild = nullptr;
return root;
}
TreeNode* SearchNode(int _value, TreeNode* search) {
TreeNode* serachNode = search;
if (search == nullptr) {
return nullptr;
}
if (search->value == _value) {
return search;
}
else {
if (search->value < _value) {
return SearchNode(_value, search->rightchild);
}
else {
return SearchNode(_value, search->leftchild);
}
}
}
TreeNode* IndexSearch(int _value, TreeNode* search,TreeNode* parent) {
if (search == nullptr) {
return parent;
}
if (search->value == _value) {
return parent;
}
else {
if (search->value < _value) {
return IndexSearch(_value, search->rightchild, search);
}
else {
return IndexSearch(_value, search->leftchild, search);
}
}
}
void TurnLeft(TreeNode* _root) {
if (_root == nullptr || _root->rightchild == nullptr) {
return;
}
TreeNode* _parent = _root->parent;
TreeNode* _rightchild = _root->rightchild;
TreeNode* rc_leftchild = _rightchild->leftchild;
_root->rightchild = rc_leftchild;
if (rc_leftchild != nullptr) {
rc_leftchild->parent = _root;
}
_rightchild->leftchild = _root;
_root->parent = _rightchild;
_rightchild->parent = _parent;
if (_parent == nullptr) {
root = _rightchild;
}
else {
if (_parent->leftchild == _root) {
_parent->leftchild = _rightchild;
}
else {
_parent->rightchild = _rightchild;
}
}
}
void TurnRight(TreeNode* _root) {
if (_root == nullptr || _root->leftchild == nullptr) {
return;
}
TreeNode* _parent = _root->parent;
TreeNode* _leftchild = _root->leftchild;
TreeNode* lc_rightchild = _leftchild->rightchild;
_root->leftchild = lc_rightchild;
if (lc_rightchild != nullptr) {
lc_rightchild->parent = _root;
}
_leftchild->rightchild = _root;
_root->parent = _leftchild;
_leftchild->parent = _parent;
if (_parent == nullptr) {
root = _leftchild;
}
else {
if (_parent->leftchild == _root) {
_parent->leftchild = _leftchild;
}
else {
_parent->rightchild = _leftchild;
}
}
}
void insert(int _value) {
TreeNode* _parent = IndexSearch(_value, root, root);
int parentTag = 0;
if (_parent->value == _value) {
return;
}
else {
if (_parent->value < _value){
parentTag = 1;
}
else {
parentTag = -1;
}
}
TreeNode* _grand = _parent->parent;
int grandTag = 0;
if (_grand != nullptr) {
if (_grand->leftchild == _parent) {
grandTag = -1;
}
else {
grandTag = 1;
}
}
TreeNode* uncle = nullptr;
if (_grand != nullptr) {
if (grandTag == -1) {
if (_grand->rightchild != nullptr) {
uncle = _grand->rightchild;
}
}
else if (grandTag == 1) {
if (_grand->leftchild != nullptr) {
uncle = _grand->leftchild;
}
}
}
TreeNode* newNode = new TreeNode();
newNode->value = _value;
newNode->color = 1;
newNode->leftchild = nullptr;
newNode->parent = _parent;
newNode->rightchild = nullptr;
if (_parent->color == 2 || _grand == nullptr)
{
if (parentTag == -1) {
_parent->leftchild = newNode;
}
else if (parentTag == 1) {
_parent->rightchild = newNode;
}
}
else {
if (uncle == nullptr || uncle->color == 2)
{
if (grandTag == 1) {
if (parentTag == 1) {
_parent->rightchild = newNode;
_grand->color = 1;
_parent->color = 2;
TurnLeft(_grand);
}
else if (parentTag == -1) {
_parent->leftchild = newNode;
_grand->color = 1;
newNode->color = 2;
TurnRight(_parent);
TurnLeft(_grand);
}
}
if (grandTag == -1) {
if (parentTag == 1) {
_parent->rightchild = newNode;
_grand->color = 1;
_parent->color = 2;
TurnRight(_grand);
}
else if (parentTag == -1) {
_parent->leftchild = newNode;
_grand->color = 1;
newNode->color = 2;
TurnLeft(_parent);
TurnRight(_grand);
}
}
}
else {
if (parentTag == -1) {
_parent->leftchild = newNode;
}
else if (parentTag == 1) {
_parent->rightchild = newNode;
}
_grand->color = 1;
uncle->color = 2;
_parent->color = 2;
if (_grand->parent == nullptr) {
_grand->color = 2;
}
else {
if (_grand->parent->leftchild == _grand) {
insert(_grand->parent, _grand, -1);
}
else {
insert(_grand->parent, _grand, 1);
}
}
}
}
}
void insert(TreeNode* _parent,TreeNode* _newNode, int parentTag) {
_newNode->parent = _parent;
_newNode->color = 1;
TreeNode* _grand = _parent->parent;
int grandTag = 0;
if (_grand != nullptr) {
if (_grand->leftchild == _parent) {
grandTag = -1;
}
else {
grandTag = 1;
}
}
TreeNode* uncle = nullptr;
if (_grand != nullptr) {
if (grandTag == -1) {
if (_grand->rightchild != nullptr) {
uncle = _grand->rightchild;
}
}
else if (grandTag == 1) {
if (_grand->leftchild != nullptr) {
uncle = _grand->leftchild;
}
}
}
if (_parent->color == 2 || _grand == nullptr)
{
if (parentTag == -1) {
_parent->leftchild = _newNode;
}
else if (parentTag == 1) {
_parent->rightchild = _newNode;
}
}
else {
if (uncle == nullptr || uncle->color == 2)
{
if (grandTag == 1) {
if (parentTag == 1) {
_grand->color = 1;
_parent->color = 2;
TurnLeft(_grand);
}
else if (parentTag == -1) {
_grand->color = 1;
_newNode->color = 2;
TurnRight(_parent);
TurnLeft(_grand);
}
}
if (grandTag == -1) {
if (parentTag == 1) {
_grand->color = 1;
_parent->color = 2;
TurnRight(_grand);
}
else if (parentTag == -1) {
_grand->color = 1;
_newNode->color = 2;
TurnLeft(_parent);
TurnRight(_grand);
}
}
}
else {
_grand->color = 1;
uncle->color = 2;
_parent->color = 2;
if (_grand->parent == nullptr) {
_grand->color = 2;
}
else {
if (_grand->parent->leftchild == _grand) {
insert(_grand->parent, _grand, -1);
}
else {
insert(_grand->parent, _grand, 1);
}
}
}
}
return;
}
};
int main() {
myMap* map = new myMap(1);
map->insert(2);
map->insert(3);
map->insert(4);
map->insert(5);
map->insert(6);
map->insert(7);
system("pause");
}