-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRedBlackBST.java
More file actions
151 lines (144 loc) · 4.3 KB
/
RedBlackBST.java
File metadata and controls
151 lines (144 loc) · 4.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
public class RedBlackBST {
//Root initialized to nil
private RedBlackNode nil=new RedBlackNode();
private RedBlackNode root=nil;
public static void main(String[] args) {
RedBlackBST tree=new RedBlackBST();
int a[]={86,108,111,74,99,72,78};
for(int i=0;i<a.length;i++){
RedBlackNode temp=new RedBlackNode(a[i]);
tree.RB_insert(tree,temp);
}
inorder(tree,tree.root);
}
public RedBlackBST(){
nil.setColor(0);
root.left= nil;
root.right=nil;
root.parent=nil;
}
private static void inorder(RedBlackBST T,RedBlackNode root){//left-Root-right
if(!root.equals(T.nil)){
inorder(T,root.left);
System.out.println(root.data+" "+root.color);
inorder(T, root.right);
}
}
private void left_rotate(RedBlackBST T,RedBlackNode x){//x is to be left-rotated
RedBlackNode y;
y=x.right;
x.right=y.left;
if(!y.left.equals(T.nil))
y.left.parent=x;
y.parent=x.parent;
if(x.parent.equals(T.nil))
T.root=y;
else if(x.equals(x.parent.left))
x.parent.left=y;
else
x.parent.right=y;
y.left=x;
x.parent=y;
}
private void right_rotate(RedBlackBST T,RedBlackNode y){
RedBlackNode x;
x=y.left;
y.left=x.right;
if(!x.right.equals(T.nil))
x.right.parent=y;
x.parent=y.parent;
if(y.parent.equals(T.nil))
T.root=x;
else if(y.equals(y.parent.left))
y.parent.left=x;
else
y.parent.right=x;
x.right=y;
y.parent=x;
}
private void RB_insert(RedBlackBST T,RedBlackNode z){
RedBlackNode y=T.nil;
RedBlackNode x=T.root;
while(!x.equals(T.nil)){
y=x;
if(z.data<x.data)
x=x.left;
else
x=x.right;
}
z.parent=y;
if(y.equals(T.nil))
T.root=z;
else if(z.data<y.data)
y.left=z;
else
y.right=z;
z.right=z.left=T.nil;
z.color=RedBlackNode.RED;
RB_fixup(T,z);
}
private void RB_fixup(RedBlackBST T,RedBlackNode z){
RedBlackNode y=null;
while(z.parent.color== RedBlackNode.RED){
if(z.parent.equals(z.parent.parent.left)){
y= z.parent.parent.right; //uncle
if(y.color==RedBlackNode.RED){ //case 1
z.parent.color=y.color=RedBlackNode.BLACK;
z.parent.parent.color=RedBlackNode.RED;
z=z.parent.parent;
}
else{
if(z.equals(z.parent.right)){ //case 2
z=z.parent;
left_rotate(T,z);
}
z.parent.color=RedBlackNode.BLACK; //case 3
z.parent.parent.color=RedBlackNode.RED;
right_rotate(T,z.parent.parent);
}
}
else{
y= z.parent.parent.left; //uncle
if(y.color==RedBlackNode.RED){ //case 1
z.parent.color=y.color=RedBlackNode.BLACK;
z.parent.parent.color=RedBlackNode.RED;
z=z.parent.parent;
}
else{
if(z.equals(z.parent.left)){ //case 2
z=z.parent;
right_rotate(T,z);
}
z.parent.color=RedBlackNode.BLACK; //case 3
z.parent.parent.color=RedBlackNode.RED;
left_rotate(T,z.parent.parent);
}
}
}
T.root.color=RedBlackNode.BLACK;
}
}
class RedBlackNode{
public static final int BLACK=0;
public static final int RED=1;
RedBlackNode parent;
RedBlackNode left,right;
int data;
int color;
RedBlackNode(){
color=RED;
parent=null;
left=null;
right=null;
}
RedBlackNode(int key){
super();
this.data=key;
}
public void setColor(int val){
if(val==0)
this.color=BLACK;
else
this.color=RED;
}
}