学习日记#04

学习日记04


算法

树:实现插入

#include <iostream>
#include <cstdlib>
/// 规则
/// 1.Node is black or red
/// 2. root is black
/// 3.leaf node is black 
/// 4. red node's parent is black, red node's child is black, don't have continuous red node
/// 5. any node have same path to leaf node with black path
/// 
/// 
/// 
/// 
struct TreeNode {
	int value;/**/
	int color;/*2 == black 1 == red*/
	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;
			}
		}
	}



	// first insert color is red
	// if parent is black ,direct insert 4
	/// else parent is red , it dont't have uncle
	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) // 4 condition
		{
			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/*color is 1*/, 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) // 4 condition
		{
			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");
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值