红黑树增加新节点
来源:互联网 发布:淘宝亿次元 编辑:程序博客网 时间:2024/05/21 10:31
只有增加新结点的!!!只有增加新结点的!!!只有增加新结点的!!!不断修正中
想做某题看了些资料。但是现在某题还是没有思路,然后就写了这个,大体布局就和AVL增加怎结点差不错的代码
l0_r1[2]的l0_r1[0]存放对应值value的左结点的编号,l0_r1[1]存放对应右结点的编号,-1代表指向null,改为指针当是差不多
收藏的某些链接http://www.cnblogs.com/fornever/archive/2011/12/02/2270692.html
http://f.dataguru.cn/thread-515225-1-1.html
联动AVL,看看有多像http://blog.csdn.net/u014646950/article/details/47622789
搜到的测试数据http://www.newsmth.net/nForum/#!article/Algorithm/44698
N=16个,依次输入[1,16],生成的红黑树如下:
4B
/ \
2B 8R
/\ / \
1B 3B 6B 12B
/\ / \
5B 7B 10R 14R
/\ / \
9B 11B 13B 15B
\
16R
#include <iostream> #include<vector>using namespace std;struct List{ int value; int l0_r1[2]; int p; bool red; List(int v) :red(true), value(v) { l0_r1[0]= l0_r1[1] = -1; }};int rotate(vector<List>*RB_tree,int tag, int Root, intchild)/*tag=0,左旋:带入的child为Root的右。[1-tag]→;[tag]←*/{ /*tag=1,右旋:带入的child为Root的左。[1-tag]←;[tag]→*/ int temp; temp = (*RB_tree)[Root].l0_r1[1 -tag] = (*RB_tree)[child].l0_r1[tag]; if (-1 != temp)(*RB_tree)[temp].p =Root; (*RB_tree)[child].l0_r1[tag] =Root; (*RB_tree)[child].p = (*RB_tree)[Root].p; (*RB_tree)[Root].p =child; return child; /*旋转以后Root和Child身份互换了*/}int childUpEnd_RootNeedRotate(vector<List>*RB_tree,int Root, int child, inttag){ int otherChild = (*RB_tree)[Root].l0_r1[1 -tag]; (*RB_tree)[Root].red =true; (*RB_tree)[child].red =false;/*不管单旋还是直接变色,Root和child都需要变色*/ if (otherChild == -1 || !(*RB_tree)[otherChild].red)/*otherChild为黑或者不存在,此时子树最少的变换是【单旋==把Root和child身份互换】*/ { Root = rotate(RB_tree, 1 -tag, Root, child);/*操作后新子树Root黑,黑高度无增加,子树满足红黑性质,整棵树亦然*/ } else { (*RB_tree)[otherChild].red =false;/*otherChild为红时,子树最少的变换是【otherChild黑色child黑Root红】*/ if (-1 == (*RB_tree)[Root].p)/*回溯到整棵红黑树,整棵数的根置为黑,顺道标记为无需在旋转*/ (*RB_tree)[Root].red =false; } return Root;}int insertnew(vector<List>*RB_tree,int Root, int nowindex){ int tag; int child; if (Root == -1)/*只有一个结点,显然这个结点就是Root【PS第一次的Root,在主函数有初始化为最开始的-1】*/ { (*RB_tree)[nowindex].p = -1; (*RB_tree)[nowindex].red =false; return nowindex; } else { tag =((*RB_tree)[nowindex].value < (*RB_tree)[Root].value) ? 0 : 1;/*本来已经存在结点,这里用来判断是往左走tag=0,或者往右走tag=1*/ if ((*RB_tree)[Root].l0_r1[tag] != -1)/*如果此次走的Root的该方向还有下个结点,DFS走下去;否则*/ { child= (*RB_tree)[Root].l0_r1[tag] = insertnew(RB_tree, (*RB_tree)[Root].l0_r1[tag],nowindex); /*这里出来以后的child为新数往当前Root走的方向稳定后的【左tag=0】【右tag=1】结点*/ if ((*RB_tree)[child].red && ((*RB_tree)[child].l0_r1[1 - tag] != -1 && (*RB_tree)[(*RB_tree)[child].l0_r1[1 - tag]].red || (*RB_tree)[child].l0_r1[tag]!=-1&&(*RB_tree)[(*RB_tree)[child].l0_r1[tag]].red)) if ( ((*RB_tree)[nowindex].value<(*RB_tree)[Root].value && (*RB_tree)[nowindex].value<(*RB_tree)[child].value) || ((*RB_tree)[nowindex].value >= (*RB_tree)[Root].value && (*RB_tree)[nowindex].value >= (*RB_tree)[child].value) )/*R L 如果新放入的在这个child和root的同一边左左(右右)*/ { Root = childUpEnd_RootNeedRotate(RB_tree,Root, child, tag);/*保持当前&子树[黑高度]没增加或者&整树为红黑树*/ } else { child= rotate(RB_tree, tag, child, (*RB_tree)[child].l0_r1[1 - tag]); (*RB_tree)[Root].l0_r1[tag] = child; Root = childUpEnd_RootNeedRotate(RB_tree,Root, child, tag);/*保持当前&子树[黑高度]没增加或者&整树为红黑树*/ }/*LR RL 否则在Root左,child右;左旋后右旋;在Root右,在child左,右旋后左旋*/ } else { /*新进的在红黑树中的哪个Root的左tag=0或右tag=1*/ (*RB_tree)[Root].l0_r1[tag] =nowindex; (*RB_tree)[nowindex].p =Root; } } return Root;}void DFSreadln(vector<List>*RB_tree,int Root,boolzuo){ if (Root != -1) { cout<< "root "<< ((*RB_tree)[Root].p == -1 ? -1 : (*RB_tree)[(*RB_tree)[Root].p].value) <<(zuo?"Zuo":"You")<<" S"<< (*RB_tree)[Root].value <<": " << ((*RB_tree)[Root].red ?"R" : "B") << endl; DFSreadln(RB_tree, (*RB_tree)[Root].l0_r1[0],true); DFSreadln(RB_tree, (*RB_tree)[Root].l0_r1[1],false); }}int main(){ int N, Root, value; cin>> N; vector<List>RB_tree; Root = -1; for (int i = 0; i < N; i++) { cin>> value; RB_tree.push_back(List(value));/*List中有构造函数,默认新添加涂红色*/ Root =insertnew(&RB_tree, Root, i); /*vector<List>RB_tree的push_back()里面的序号是从0开始的,相当与RB_tree[i]就是新进的*/ } cout<< RB_tree[Root].value << endl; DFSreadln(&RB_tree,Root,true); system("pause"); return 0;}
- 红黑树增加新节点
- 增加datanode新节点
- 为集群增加新节点
- 【Oracle】RAC增加新节点
- hadoop增加新节点实践
- Zabbix增加win10新节点
- xml文件增加新的一行节点
- 11gr2RAC环境增加新节点
- Hadoop集群中增加新节点
- Hadoop集群中增加新节点
- Hadoop集群中增加新节点
- Hadoop集群中增加新节点总结
- Hadoop集群中增加新节点
- Hadoop集群中增加新节点
- Openstack增加新的compute节点
- 11gr2RAC环境增加新节点(五)
- hadoop集群中动态增加新的DataNode节点
- 学习总结四:hadoop集群动态增加新节点及下架一个节点
- HDU 1003 区间最大和问题(动态规划)
- 关于abc全排列的方法
- 字符串哈希
- 编程的方式操作IIS 参考文档
- 绑定数据 Repeater 与ObjectDataSource的用法
- 红黑树增加新节点
- javascript函数的3种定义方式
- Rqnoj 题目 72拔河比赛 (01背包)
- does not have write access to 'C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET File
- UVA - 10340 All in All
- 高精度乘法(压位)
- 使用json-lib 把javabean转换成json串
- 单链表操作的实验课
- linux线程通信