-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrbtree.java
178 lines (166 loc) · 4.4 KB
/
rbtree.java
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
class Node{
int data;
int color;
Node left;
Node right;
Node parent;
}
public class rbtree {
Node TNULL;
Node root;
public rbtree(){
TNULL = new Node();
TNULL.color = 0;
TNULL.left = null;
TNULL.right = null;
TNULL.parent = null;
root = TNULL;
}
public void leftrotation(Node x)
{
Node y = x.right;
x.right = y.left;
if(y.left!=TNULL){
y.left.parent = x;
}
y.parent = x.parent;
if(x.parent == null){
this.root = y;
} else if(x == x.parent.left){
x.parent.left = y;
} else {
x.parent.right = y;
}
y.left = x;
x.parent = y;
}
public void rightrotation(Node x)
{
Node y = x.left;
x.left = y.right;
if(y.right!=TNULL){
y.right.parent = x;
}
y.parent = x.parent;
if(x.parent == null){
this.root = y;
} else if (x == x.parent.right){
x.parent.right = y;
} else {
x.parent.left = y;
}
y.right = x;
x.parent = y;
}
public void fixInsert(Node node)
{
System.out.println(node.data);
Node uncle;
while(node.parent.color == 1){
if(node.parent==node.parent.parent.right){
uncle = node.parent.parent.right;
if(uncle.color==1){
uncle.color = 0;
node.parent.color = 0;
node.parent.parent.color = 1;
node = node.parent.parent;
}
else{
if(node == node.parent.left){
node = node.parent;
rightrotation(node);
}
node.parent.color = 0;
node.parent.parent.color = 1;
leftrotation(node.parent.parent);
}
}
else{
uncle = node.parent.parent.left;
if(uncle.color==1){
uncle.color = 0;
node.parent.color = 0;
node.parent.parent.color = 1;
node = node.parent.parent;
}
else{
if(node == node.parent.right){
node = node.parent;
leftrotation(node);
}
node.parent.color = 0;
node.parent.parent.color = 1;
rightrotation(node.parent.parent);
}
}
if(node==root){
break;
}
}
root.color=0;
}
public void insertion(int key){
Node node = new Node();
node.data = key;
node.color = 1;
node.left = TNULL;
node.right = TNULL;
node.parent = TNULL;
Node y = null;
Node x = this.root;
while(x!=TNULL){
y=x;
if(node.data<x.data){
x=x.left;
}
else{
x=x.right;
}
}
node.parent = y;
if(y==null){
root=node;
}
else if(node.data<y.data){
y.left = node;
} else {
y.right = node;
}
if(node.parent == null){
node.color = 0;
return;
}
if(node.parent.parent == null){
return;
}
fixInsert(node);
}
private void printHelper(Node root, String indent, boolean last) {
if (root != TNULL) {
System.out.print(indent);
if (last) {
System.out.print("R----");
indent += " ";
} else {
System.out.print("L----");
indent += "| ";
}
String sColor = root.color == 1 ? "RED" : "BLACK";
System.out.println(root.data + "(" + sColor + ")");
printHelper(root.left, indent, false);
printHelper(root.right, indent, true);
}
}
public void printTree() {
printHelper(this.root, "", true);
}
public static void main(String args[]){
rbtree r = new rbtree();
r.insertion(10);
r.insertion(18);
r.insertion(7);
r.insertion(15);
r.insertion(16);
r.printTree();
}
}