java 红黑树_JAVA 红黑树源码

class RBTree>{

private static final String RedColor = "red";

private static final String BlackColor = "black";

//定义一个根结点

RBNode root;

//定义结点数量

private int size = 0;

//返回总结点数量

public int getSize(){

return this.size;

}

//根据key查找对应的结点

public RBNode findData(int key){

RBNode p;

p = root;

while(p != null){

if (key > p.getKey()){

p = p.getRight();

}

else if (key < p.getKey()){

p = p.getLeft();

}

else{

return p;

}

}

return null;

}

//查询每个结点的颜色

public void findColor(RBNode node){

if (node != null) {

System.out.println("for test ==>> " + " key => " + node.getKey() + ", color => " + node.getColor() + ", data => " + node.getData() + ", left => " + node.getLeft() + ", right => " + node.getRight() + ", parent => " + node.getParent());

findColor(node.getLeft());

findColor(node.getRight());

}

}

//返回根结点

public RBNode findRoot(){

return root;

}

//查找后继结点

public RBNode findAfterNode(RBNode node){

if (node.getRight() != null) {

RBNode trbNode = node.getRight();

while (trbNode.getLeft() != null) {

trbNode = trbNode.getLeft();

}

return trbNode;

}

return null;

}

//查找前驱结点

public RBNode findBeforeNode(RBNode node){

if (node.getLeft() != null) {

RBNode trbNode = node.getLeft();

while (trbNode.getRight() != null) {

trbNode = trbNode.getRight();

}

return trbNode;

}

return null;

}

//插入数据

public T putData(int key, T data){

size ++;

RBNode node;

node = root;

//如果根结点为空,则直接新建一个结点为根结点

if (node == null){

node = new RBNode(key, data, BlackColor, null, null, null);

root = node;

return node.getData();

}

else{

RBNode p = node;

//查找插入新结点的位置

while(node != null) {

p = node;

if (key < node.getKey()) {

node = node.getLeft();

}

else if (key > node.getKey()){

node = node.getRight();

}

else{

//插入结点的key已存在,原有的红黑树已经是平衡的,则直接更新结点的data

node.setData(data);

return node.getData();

}

}

//插入新结点,新插入的结点为红色

RBNode e = new RBNode(key, data, RedColor, null, null, p);

if (key > p.getKey()){

p.setRight(e);

e.setParent(p);

}

else{

p.setLeft(e);

e.setParent(p);

}

if (p.getParent() == null){

//插入新结点时只有根结点

return e.getData();

}

else{

/**

* 插入新结点的父结点为黑结点,由于插入的结点是红色的;

* 当新插入结点的父结点为黑结点时,直接插入即可,并不会影响红黑树的平衡,无需做自平衡;

*/

if (e.getParent().getColor().equals(BlackColor)){

return e.getData();

}

/**

* 插入新结点的父结点为红结点,由于插入的结点也是红结点

* 如果插入的父结点为红结点,那么该父结点不可能为根结点,所以插入结点总是存在祖父结点

*/

else {

/**

* 插入的新结点的父结点为红结点

* 并且叔叔结点存在并且为红结点(叔叔结点是插入新结点的父结点的兄弟结点)

*/

//当前结点的祖父结点的父结点

RBNode ttPParent = null;

//当前结点的父结点的兄弟结点

RBNode tPBrother = null;

//当前结点

RBNode t = e;

//当前结点的父结点

RBNode tParent = t.getParent();

//当前结点的祖父结点

RBNode tPParent = t.getParent().getParent();

if (tPParent != root){

// 保存当前结点祖父结点的父结点

ttPParent = tPParent.getParent();

}

while (t != null) {

// 新增结点的父结点不为空

if (tParent != null){

//判断新增结点的父结点的颜色,若为黑色直接退出

if(tParent.getColor().equals(BlackColor)){

break;

}

if (tPParent != null) {

if (tPParent != root){

// 保存当前结点祖父结点的父结点

ttPParent = tPParent.getParent();

}

else{

ttPParent = null;

}

if (tParent == tPParent.getLeft()) {

//当前结点的父结点的兄弟结点

tPBrother = tPParent.getRight();

}

else if(tParent == tPParent.getRight()){

//当前结点的父结点的兄弟结点

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值