红黑树——删除操作(未完成)

#include <iostream>
#include <stack>
using namespace std;

typedef enum {
	Red,
	Black,
}Color;

typedef struct Node {
	int key;
	Color color;
	struct Node *parent;
	struct Node *left;
	struct Node *right;
}Node, *ptrNode;

class RedBlackTree {
private:
	ptrNode tree;
	ptrNode NIL;
	void init();
	void leftRotate(ptrNode p);
	void rightRotate(ptrNode p);
	void insertFixup(ptrNode p);
public:
	RedBlackTree();
	void insert(int key);
	void traverse();
};

void RedBlackTree::insertFixup(ptrNode p) {
	ptrNode uncle;
	while(p->parent->color == Red) {
		if (p->parent == p->parent->parent->left) {
			uncle = p->parent->parent->right;
			if (uncle->color == Red) {
				p->parent->color = Black;
				uncle->color = Black;
				p->parent->parent->color = Red;
				p = p->parent->parent;
			} else {
				if (p == p->parent->right) {
					p = p->parent;
					leftRotate(p);
				}
				p->parent->color = Black;
				p->parent->parent->color = Red;
				rightRotate(p->parent->parent);
			}
		} else {
			uncle = p->parent->parent->left;
			if (uncle->color == Red) {
				p->parent->color = Black;
				uncle->color = Black;
				p->parent->parent->color = Red;
				p = p->parent->parent;
			} else {
				if (p == p->parent->left) {
					p = p->parent;
					rightRotate(p);
				}
				p->parent->color = Black;
				p->parent->parent->color = Red;
				leftRotate(p->parent->parent);
			}
		}
	}
	tree->color = Black;
}

void RedBlackTree::traverse() {
	ptrNode p = tree;
	stack<ptrNode> s;
	while (p!=NIL || !s.empty()) {
		if (p!=NIL) {
			s.push(p);
			p = p->left;
		} else {
			std::cout << s.top()->key << " ";
			p = s.top()->right;
			s.pop();
		}
	}
}

void RedBlackTree::insert(int key) {
	ptrNode p = new Node();
	p->key = key;
	p->color = Red;
	p->parent = NIL;
	p->left = NIL;
	p->right = NIL;
	ptrNode parent = NIL;
	ptrNode cur = tree;
	while (cur != NIL) {
		parent = cur;
		if (key < cur->key) {
			cur = cur->left;
		} else {
			cur = cur->right;
		}
	}
	p->parent = parent;
	if (parent == NIL) {
		tree = p;
	} else {
		if (key < parent->key) {
			parent->left = p;
		} else {
			parent->right = p;
		}
	}
	insertFixup(p);
}

void RedBlackTree::rightRotate(ptrNode p) {
	ptrNode lChild = p->left;
	p->left = lChild->right;
	lChild->right->parent = p;
	lChild->parent = p->parent;
	if (p->parent == NIL) {
		tree = lChild;
	} else {
		if (p == p->parent->left) {
			p->parent->left = lChild;
		} else {
			p->parent->right = lChild;
		}
	}
	lChild->right = p;
	p->parent = lChild;
}

void RedBlackTree::leftRotate(ptrNode p) {
	ptrNode rChild = p->right;
	p->right = rChild->left;
	rChild->left->parent = p;
	rChild->parent = p->parent;
	if (p->parent == NIL) {
		tree = rChild;
	} else {
		if (p == p->parent->left) {
			p->parent->left = rChild;
		} else {
			p->parent->right = rChild;
		}
	}
	rChild->left = p;
	p->parent = rChild;
}

RedBlackTree::RedBlackTree() {
	init();
}

void RedBlackTree::init() {
	NIL = new Node();
	NIL->color = Black;
	NIL->parent = NULL;
	NIL->left = NULL;
	NIL->right = NULL;
	tree = NIL;
}

int main() {
	RedBlackTree *rbt = new RedBlackTree();
	rbt->insert(11);
	rbt->insert(2);
	rbt->insert(14);
	rbt->insert(1);
	rbt->insert(7);
	rbt->insert(15);
	rbt->insert(5);
	rbt->insert(8);
	rbt->insert(4);
	rbt->traverse();
	delete rbt;
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值