红黑树RBTree
#pragma once
enum colour //子节点的颜色
{
RED,
BLANK,
};
template<class K,class V>
struct RBTreeNode
{
K _key;
V _value;
RBTreeNode<K, V>* _left;
RBTreeNode<K, V>* _right;
RBTreeNode<K, V>* _parent;
colour _col;
RBTreeNode(const K& key, const V& value)
:_key(key)
, _value(value)
, _left(NULL)
, _right(NULL)
, _parent(NULL)
, _col(RED)
{}
};
template<class K,class V>
class RBTree
{
typedef RBTreeNode<K, V> Node;
public:
RBTree()
:_root(NULL)
{}
~RBTree()
{
Destory(_root);
}
bool Insert(const K& key, const V& value) //插入节点
{
if (_root == NULL)
{
_root = new Node(key, value);
_root->_col = BLANK;
return true;
}
Node* parent = NULL;
Node* cur = _root;
while (cur) //寻找插入的位置,如果当前节点的key值比要查找的值小,则往树的右边走,如果当前节点的key值比查找的值大,则往树的左边走。否则就是 树中已经有这个值了,不能插入了
{
if (cur->_key < key)
{
parent = cur;
cur = cur->_right;
}
else if (cur->_key>key)
{
parent = cur;
cur = cur->_left;
}
else
{
return false;
}
}
cur = new Node(key, value);
if (parent->_key<key) //选择将节点插入到cur的左边还是右边,需要跟parent->_key比较
{
parent->_right = cur;
cur->_parent = parent;
}
else
{
parent->_left = cur;
cur->_parent = parent;
}
while (cur != _root&&parent->_col == RED)
{
Node* grandparent = parent->_parent;
//uncle存在且为红
if (grandparent->_left == parent)
{
Node* uncle = grandparent->_right;
if (uncle&&uncle->_col == RED)
{
parent->_col = uncle->_col = BLANK;
grandparent->_col = RED;
cur = parent;
parent = cur->_parent;
}
else //uncle不存在或者uncle存在且为黑
{
if (parent->_right == cur)
{
_RotateL(parent);
//parent = cur;
swap(parent, cur);
}
_RotateR(grandparent);
parent->_col = BLANK;
grandparent->_col = RED;
cur = grandparent;
parent = cur->_parent;
//break;
}
}
else if (parent == grandparent->_right)
{
Node* uncle =grandparent->_left;
if (uncle&&uncle->_col==RED)
{
parent->_col = uncle->_col = BLANK;
grandparent->_col = RED;
cur = grandparent;
parent = cur->_parent;
}
else //uncle不存在或者uncle为黑
{
if (parent->_left == cur)
{
_RotateR(parent);
//cur = parent;
swap(cur, parent);
}
_RotateL(grandparent);
parent->_col = BLANK;
grandparent->_col = RED;
cur = parent;
parent = cur->_parent;
//break;
}
}
}
_root->_col = BLANK;
return true;
}
bool Isblance()
{
if (_root == NULL)
return true;
int count = 0;
Node* cur = _root;
while (cur)
{
if (cur->_col == BLANK)
count++;
cur = cur->_left;
}
int k = 0;
return _Isblance(_root, count, k);
}
void InOrder()
{
_InOrder(_root);
}
protected:
void _InOrder(Node* root)
{
if (root == NULL)
return;
_InOrder(root->_left);
cout << root->_key << " " << root->_value << " ";
if (root->_col == RED)
cout << "红" << endl;
else
cout << "黑" << endl;
_InOrder(root->_right);
}
void _RotateR(Node* root) //右旋
{
Node* subL = root->_left;
Node* subLR = subL->_right;
Node* ppNOde = root->_parent;
root->_left = subLR;
if (subLR)
subLR->_parent = root;
subL->_right = root;
//root->_parent = subL;
if (ppNOde==NULL)
{
_root = subL;
subL->_parent = NULL;
}
else
{
if (ppNOde->_left == root)
{
ppNOde->_left = subL;
subL->_parent = ppNOde;
}
else
{
ppNOde->_right = subL;
subL->_parent = ppNOde;
}
}
}
void _RotateL(Node* root) //左旋
{
Node* subR = root->_right;
Node* subRL = subR->_left;
Node* ppNOde = root->_parent;
root->_right = subRL;
if (subRL)
subRL->_parent = root;
subR->_left = root;
//root->_parent = subR;
if (ppNOde==NULL)
{
_root = subR;
subR->_parent = NULL;
}
else
{
if (ppNOde->_left == root)
{
ppNOde->_left = subR;
subR->_parent = ppNOde;
}
else
{
ppNOde->_right = subR;
subR->_parent = ppNOde;
}
}
}
bool _Isblance(Node* root, int count, int k)
{
if (root == NULL)
{
return count == k;
}
Node* parent = root->_parent;
if (parent && (parent->_col == RED&&root->_col == RED))
return false;
if (root->_col == BLANK)
k++;
return _Isblance(root->_left, count, k) && _Isblance(root->_right, count, k);
}
void Destory(Node* root)
{
if (root == NULL)
return;
Destory(root->_left);
Destory(root->_right);
delete root;
root = NULL;
}
private:
Node* _root;
};
红黑树RBTree的更多相关文章
- 高级搜索树-红黑树(RBTree)解析
目录 红黑树的定义 节点与树的定义 旋转操作 插入操作 情况1:p的兄弟u为黑色 情况2: p的兄弟u为红色 插入操作性能分析 代码实现 删除操作 情况1:x的接替者succ为红色 情况2:x的接替者 ...
- 平衡搜索树--红黑树 RBTree
红黑树是一棵二叉搜索树,它在每个节点上增加了一个存储位来表示节点的颜色,可以是Red或Black. 通过对任何一条从根到叶子节点简单路径上的颜色来约束树的高度,红黑树保证最长路径不超过最短路径的两倍, ...
- java——红黑树 RBTree
对于完全随机的数据,普通的二分搜索树就很好用,只是在极端情况下会退化成链表. 对于查询较多的情况,avl树很好用. 红黑树牺牲了平衡性,但是它的统计性能更优(综合增删改查所有的操作). 红黑树java ...
- 红黑树(RBTREE)之上-------构造红黑树
该怎么说呢,现在写代码的速度还是很快的,很高兴,o(^▽^)o. 光棍节到了,早上没忍住,手贱了一般,看到*D的优惠,买了个机械键盘,晚上就到了,敲着还是很舒服的,和老婆炫耀了一把哈哈. 光棍节再去* ...
- 高级搜索树-红黑树(RBTree)代码实现
代码实现 代码参考了<数据结构(c++语言版)>--清华大学邓俊辉 "RBTree.h" #pragma once //#include"pch.h" ...
- 【转】B树、B-树、B+树、B*树、红黑树、 二叉排序树、trie树Double Array 字典查找树简介
B 树 即二叉搜索树: 1.所有非叶子结点至多拥有两个儿子(Left和Right): 2.所有结点存储一个关键字: 3.非叶子结点的左指针指向小于其关键字的子树,右指针指向大于其关键字的子树: 如: ...
- RBTree 红黑树
红黑树 一.红黑树概述 红黑树不仅是一个二叉搜索树,并且满足以下规则: 1>每个节点不是红的就是黑的, 2>根结点为黑色, 3>如果节点为红色,其子节点必须为黑色, 4>任一节 ...
- 红黑树(RB-tree)比AVL树的优势在哪?
1. 如果插入一个node引起了树的不平衡,AVL和RB-Tree都是最多只需要2次旋转操作,即两者都是O(1):但是在删除node引起树的不平衡时,最坏情况下,AVL需要维护从被删node到root ...
- RB-tree (红黑树)相关问题
今天被问到了红黑树的规则,简述总结一下: 1.每个节点不是红色就是黑色. 2.根节点为黑色. 3.如果节点为红,其子节点必须为黑. 4.任一节点至NULL(树尾端)的任何路径,所含之黑节点数必须相同. ...
随机推荐
- 【hdu6148】Valley Numer【数位dp模板题】
题意 对于每组数据给出一个整数n(length(n)<=100),找出不大于n的数字中有多少是Valley Numer.对于Valley的定义是它每一位的数字要么是递增,要么是递减,要么是先递减 ...
- Nginx源码完全注释(6)core/murmurhash
下面是摘自 Google Code 的 Murmurhash 开源项目主页上的 Murmurhash2,Nginx 就是采用的这个. uint32_t MurmurHash2 ( const void ...
- coding创建项目
在本地,使用git 需要创建一个pom.xml文件,就可以导入到工作空间了! 在需要项目工作空间里,依次使用git命令执行 mkdir test //创建文件夹,项目名称cd test //切换 ...
- 微软人工智能公开课 https://mva.microsoft.com/colleges/microsoftai#!jobf=Developer
https://mva.microsoft.com/colleges/microsoftai#!jobf=Developer
- Tensorflow CIFAR10 (二分类)
数据的下载: (共有三个版本:python,matlab,binary version 适用于C语言) http://www.cs.toronto.edu/~kriz/cifar-10-python. ...
- 关于《Spark快速大数据分析》运行例子遇到的报错及解决
一.描述 在书中第二章,有一个例子,构建完之后,运行: ${SPARK_HOME}/bin/spark-submit --class com.oreilly.learningsparkexamples ...
- SQL Compare数据库版本比较工具
Red Gate系列文章: Red Gate系列之一 SQL Compare 10.4.8.87 Edition 数据库比较工具 完全破解+使用教程 Red Gate系列之二 SQL Source C ...
- web.xml配置详解[转]
引文: 对于一个J2EE领域的程序员而言,基本上每天都会和web应用打交道. 什么是web应用?最简单的web应用什么样?给你一个web应用你该从何入手? 1.什么是web应用? web应用是一种可以 ...
- oracle数据库学习记录(持续更新中...)
--------------------------------------------day1------------------------------------------------- 1. ...
- Pandas -- Merge,join and concatenate
Merge, join, and concatenate pandas provides various facilities for easily combining together Series ...