红黑树算法的简单实现



#include "stdafx.h"
 
#include <iostream>
#include <queue>

using namespace std;

void pause() {
	getchar(), getchar();
}
/*
1. 根节点是黑色
2. 空节点是黑色
3. 一个节点是红色节点,其他孩子节点必定是黑色节点
4. 任意一个节点到叶子节点,经过的黑色节点是一样的
5. 红黑树 保证的是黑平衡,即 左右两边黑色节点平衡 ,最大高度是 2LogN , 所以搜索效率是 O(logN)

*/

//template <typename K, typename V>
enum Color{RED,BLACK};
template <typename K,typename V>
 struct RBNode{
	K key;
	V value;
	RBNode<K,V> * left;
	RBNode<K,V> * right;
	Color color;
	RBNode(const K& k,const V& v) :
		key(k), 
		value(v), 
		left(NULL),
		right(NULL),
		color(RED) {}

};
template <class K, class V>
struct  RBTree {
	typedef RBNode<K, V> Node;
	Node* _root;
	int _size;
	
	RBTree() :_size(0),_root(NULL) {}
	~RBTree() { _clear(_root); }

	void _clear(Node *p) {
		if (p) {
			if (p->left) _clear(p->left);
			if (p->right) _clear(p->right);
			delete p;
		}
	}

	Color colorOf(Node* node) {
		if (node) return node->color;
		//NULL 节点是黑色
		return BLACK;
	}
	int size() { return _size; };
	bool empty() {return _size<=0};
	
	Node *add(Node* node, K key, V value) {
		if (node == NULL) {
			_size++;
			return new Node(key, value);
		}
		if (key < node->key) {
			//key 比 当前小,往左边走
			node->left = add(node->left, key, value);
		}
		else if (key > node->key) {
			node->right = add(node->right, key, value);
		}
		else {
			//node->key equals key
			node->value = value;
		}
		//添加完成后要判断是否需要翻转
		if (isRed(node->right) && !isRed(node->left)) {
			node = leftRotate(node);
		}
		if (isRed(node->left) && isRed(node->left->left)) {
			node = rightRotate(node);
		}
		if (isRed(node->left) && isRed(node->right)) {
			//翻转节点颜色
			flipColors(node);
		}
		return node;
	}
	Node * get(Node* node, K key) {
		if (node == NULL) return NULL;
		if (node->key < key) {
			return get(node->right, key);
		}
		else if (node->key > key) {
			return get(node->left, key);
		}
		//if node->key equals key, 返回地址
		return node;
	}
	V* get(K key) {
		Node * res = get(_root, key);
		if (res) return &(node->value);
		return NULL;
	}
	bool isRed(Node *node) {
		return colorOf(node) == RED;
	}
	/*
		  node                 x
		left  x            node    t2
		     t1 t2        left  t1 

	
	
	*/
	Node * leftRotate(Node * node) {
		Node * x = node->right;
		Node *t1 = x->left /*, *t2 = x->right */;
		x->left = node, node->right = t1;
		//x 等于替换了 node 的位置,因此颜色要一样
		x->color = node->color;
		//node 和 x 形成一个 2-3树的3节点
		node->color = RED;
		return x;

	}
	/*
	      node              t1
        t1     t2         t3    node  
       t3  t4                  t4    t2 

	*/
	Node * rightRotate(Node *node) {
		Node * t1 = node->left, *t4 = t1->right;
		t1->right = node, node->left = t4;
		t1->color = node->color;
		node->color = RED;
		return t1;
	}
	//颜色翻转
	void flipColors(Node *node) {
		if (node) {
			node->color = RED;
			if (node->left)node->left->color = BLACK;
			if (node->right) node->right->color = BLACK;
		}
	}
	
	void insert(K key, V value) {
		_root = add(_root,key, value);
		_root->color = BLACK;//根节点必须是黑色
	}

	void  bfs_print() {
		queue<Node*> q;
		if (_root) q.push(_root);
		while (q.size()) {
			Node * x = q.front(); q.pop();
			if (x->left) q.push(x->left);
			if (x->right) q.push(x->right);
			cout << x->value << " " << x->color << endl;
		}
	}
	void inorder_print(Node *node) {
		if (node) {
			inorder_print(node->left);
			cout << node->value << endl;
			inorder_print(node->right);
		}
	}
	void inorder() {
		inorder_print(_root);
	}
	/*void remove(T key);
	
	
	
	Node find_min(); 
	Node find_max();*/
    

};
 

 

int main(void) {
	RBTree<int,int> rbtree;
	for (int i = 0; i < 18; ++i)  rbtree.insert(i, i+1);
	rbtree.inorder();
	//rb_tree<int,int> tree;
	//cout << 1 << endl;
	//cout << p->next << endl;
	pause();
	return 0;
}

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 终极编程指南 设计师:CSDN官方博客 返回首页