-
Notifications
You must be signed in to change notification settings - Fork 0
/
RBmain.java
193 lines (175 loc) · 5.1 KB
/
RBmain.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import java.util.Random;
public class RBmain {
public static void main(String[] args) {
RBtree rbtree = new RBtree();
// Insert some random keys into the tree
int[] keysinsert={25,26,35,47,50,75};
for (int key :keysinsert) {
rbtree.insert(key);
System.out.println("Inserted key: " + key);
}
// Delete a few keys from the tree
int[] keysToDelete = { 25, 50, 75 };
for (int key : keysToDelete) {
rbtree.delete(key);
System.out.println("Deleted key: " + key);
}
System.out.print(rbtree.search(35));
}
}
class RBtree {
private static final boolean RED = true;
private static final boolean BLACK = false;
private class Node {
int key;
Node left;
Node right;
boolean color;
public Node(int key, boolean color) {
this.key = key;
this.color = color;
}
}
private Node root;
private boolean isRed(Node node) {
if (node == null) {
return false;
}
return node.color == RED;
}
private Node rotateLeft(Node node) {
Node right = node.right;
node.right = right.left;
right.left = node;
right.color = node.color;
node.color = RED;
return right;
}
private Node rotateRight(Node node) {
Node left = node.left;
node.left = left.right;
left.right = node;
left.color = node.color;
node.color = RED;
return left;
}
private void flipColors(Node node) {
node.color = !node.color;
node.left.color = !node.left.color;
node.right.color = !node.right.color;
}
private Node findMin(Node node) {
if (node.left == null) {
return node;
}
return findMin(node.left);
}
private Node moveRedLeft(Node node) {
node.color = !node.color;
if (isRed(node.right.left)) {
node.right = rotateRight(node.right);
node = rotateLeft(node);
flipColors(node);
}else if (!isRed(node.right) && !isRed(node.left)) {
node = rotateLeft(node);
node.color = !node.color;
}
return node;
}
public void insert(int key) {
root = insert(root, key);
root.color = BLACK;
}
private Node insert(Node node, int key) {
if (node == null) {
return new Node(key, RED);
}
if (key < node.key) {
node.left = insert(node.left, key);
} else if (key > node.key) {
node.right = insert(node.right, key);
} else {
// key already exists in the tree, do nothing
return node;
}
if (isRed(node.right) && !isRed(node.left)) {
node = rotateLeft(node);
}
if (isRed(node.left) && isRed(node.left.left)) {
node = rotateRight(node);
}
if (isRed(node.left) && isRed(node.right)) {
flipColors(node);
}
return node;
}
public void delete(int key) {
root = delete(root, key);
if (root != null) {
root.color = BLACK;
}
}
private Node delete(Node node, int key) {
if (node == null) {
return null;
}
if (node.left == null) {
return node.right;
} else if (node.right == null) {
return node.left;
}
if (key < node.key) {
node.left = delete(node.left, key);
} else if (key > node.key) {
node.right = delete(node.right, key);
} else {
// found the node to be deleted
if (node.left == null && node.right == null) {
return null;
} else if (node.left == null) {
return node.right;
} else if (node.right == null) {
return node.left;
} else {
// node has two children
Node min = findMin(node.right);
node.key = min.key;
node.right = delete(node.right, min.key);
}
}
if (!isRed(node.left) && !isRed(node.right)) {
node = moveRedLeft(node);
}
if (isRed(node.right)) {
node = rotateLeft(node);
}
if (isRed(node.left) && isRed(node.left.left)) {
node = rotateRight(node);
}
if (isRed(node.left) && isRed(node.right)) {
flipColors(node);
}
if (isRed(node)) {
node.color = BLACK;
}
if (!isRed(node.left) && !isRed(node.right)) {
node = moveRedLeft(node);
}
return node;
}
public boolean search(int key) {
return search(root, key);
}
private boolean search(Node node, int key) {
if (node == null) {
return false;
}
if (key < node.key) {
return search(node.left, key);
} else if (key > node.key) {
return search(node.right, key);
} else {
return true;
}
}
}