(三)红黑树插入

163 阅读1分钟

一、概述

下面树实现的源代码具体原理参考算法导论对应章节,如下是函数概览和节点的信息:

2.png

1.png

二、TNode节点构造

public class TNode {
    public Color color;
    public int key;
    public TNode left;
    public TNode right;
    public TNode p;

    public TNode() {
    }

    public TNode(int key) {
        this.key = key;
    }

    public TNode(TNode left, TNode right, TNode p) {
        this.left = left;
        this.right = right;
        this.p = p;
    }

    @Override
    public String toString() {
        return "TNode{" +
                "color=" + color +
                ", key=" + key +
                '}';
    }
}
enum Color{
    RED,BLACK;
}

三、插入操作

public class RBTree {
    public TNode NIL;
    public TNode root;

    public static void main(String[] args) {
        RBTree rbTree=new RBTree();
        rbTree.NIL=new TNode();
        rbTree.NIL.color=Color.BLACK;
        rbTree.root= rbTree.NIL;
        int i=0;
        TNode T=rbTree.root;
        int[] nums={12,1,9,2,0,11,7,19,4,15,18,5,14,13,10,16,6,3,8,17};
        while(i<nums.length){
            TNode x=new TNode(nums[i]);
            i++;
            rbTree.RBInsert(T,x);
        }
        T= rbTree.root;
        rbTree.levelOrder(T);
    }
    public void levelOrder(TNode T){
        Deque<TNode> queue=new LinkedList<>();
        queue.addLast(T);
        while(!queue.isEmpty()) {
            int size = queue.size();
            while (size > 0) {
                TNode t = queue.pollFirst();
                if(t==NIL)
                {
                    write("NIL");
                }
                else
                write(String.valueOf(t.key),t.color);
                if (t.left != null)
                    queue.addLast(t.left);
                if (t.right != null)
                    queue.addLast(t.right);
                size--;
            }
        }
    }
    public void write(String  key,Color... color){
      try(Writer writer=new FileWriter(System.getProperty("user.dir")+"/src/algorithm/project3/data.dat",true)){
          if(writer==null){
              System.out.println("null");
          }
          if(color.length==0)
          {
              writer.write(key+"\r\n");
          }else {
              writer.write("(");
              writer.write(key);
              writer.write(",");
              writer.write(String.valueOf(color[0]));
              writer.write(")\r\n");
          }
      } catch (IOException e) {
          System.out.println("null");
          e.printStackTrace();
      }
    }
    public   void RBInsert(TNode T,TNode z){
        TNode y=NIL;
        TNode x=root;
        while(x!=NIL){
            y=x;
            if(z.key<x.key)
                x=x.left;
            else
                x=x.right;
        }
        z.p=y;
        if(y==NIL)
            root=z;
        else if(z.key<y.key)
            y.left=z;
        else
            y.right=z;
        z.left=NIL;
        z.right=NIL;
        z.color=Color.RED;
        RBInsertFixup(T,z);
    }
    public void RBInsertFixup(TNode T,TNode z){
        while (z.p.color==Color.RED){
            if(z.p==z.p.p.left) //case1-3
            {
                TNode y=z.p.p.right;
                if(y.color==Color.RED)  //case1
                {
                    z.p.color=Color.BLACK;
                    y.color=Color.BLACK;
                    z.p.p.color=Color.RED;
                    z=z.p.p;
                    System.out.println("case1");
                }else if(z==z.p.right)  //case2
                {
                    z=z.p;
                    leftRotate(T,z);
                    System.out.println("case2");
                }else                   //case3
                {
                    z.p.color= Color.BLACK;
                    z.p.p.color=Color.RED;
                    rightRotate(T,z.p.p);
                    System.out.println("case3");
                }
            }else               //case4-6
            {
                TNode y=z.p.p.left;
                if(y.color==Color.RED){ //case4
                    z.p.color=Color.BLACK;
                    y.color=Color.BLACK;
                    z.p.p.color=Color.RED;
                    z=z.p.p;
                    System.out.println("case4");
                }else if(z==z.p.left){ //case5
                    z=z.p;
                    rightRotate(T,z);
                    System.out.println("case5");
                }else                   //case6
                {
                    z.p.color=Color.BLACK;
                    z.p.p.color=Color.RED;
                    leftRotate(T,z.p.p);
                    System.out.println("case6");
                }
            }
        }
        root.color=Color.BLACK;
    }
    public void leftRotate(TNode T,TNode x){
        TNode y=x.right;
        x.right=y.left;
        if(y.left!=NIL){
            y.left.p=x;
        }
        y.p=x.p;
        if(x.p==NIL)
            root=y;
        else if(x==x.p.left)
            x.p.left=y;
        else
            x.p.right=y;
        y.left=x;
        x.p=y;
    }
    public void rightRotate(TNode T,TNode x){
        TNode y=x.left;
        x.left=y.right;
        if(y.right!=NIL)
            y.right.p=x;
        y.p=x.p;
        if(x.p==NIL)
            root=y;
        else if(x==x.p.left)
            x.p.left=y;
        else
            x.p.right=y;
        y.right=x;
        x.p=y;
    }

}