-
Notifications
You must be signed in to change notification settings - Fork 107
Expand file tree
/
Copy pathRedBlackTree.java
More file actions
195 lines (167 loc) · 5.59 KB
/
RedBlackTree.java
File metadata and controls
195 lines (167 loc) · 5.59 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
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
194
195
package Programs;
public class RedBlackTree {
private Node root;
private static final boolean RED = true;
private static final boolean BLACK = false;
// Node class
private class Node {
int data;
Node left, right, parent;
boolean color;
Node(int data) {
this.data = data;
this.color = RED; // New nodes are always red
}
}
// Left rotate
private void leftRotate(Node x) {
Node y = x.right;
x.right = y.left;
if (y.left != null)
y.left.parent = x;
y.parent = x.parent;
if (x.parent == null)
root = y;
else if (x == x.parent.left)
x.parent.left = y;
else
x.parent.right = y;
y.left = x;
x.parent = y;
}
// Right rotate
private void rightRotate(Node y) {
Node x = y.left;
y.left = x.right;
if (x.right != null)
x.right.parent = y;
x.parent = y.parent;
if (y.parent == null)
root = x;
else if (y == y.parent.left)
y.parent.left = x;
else
y.parent.right = x;
x.right = y;
y.parent = x;
}
// Insert a node into the Red-Black Tree
public void insert(int data) {
Node newNode = new Node(data);
root = bstInsert(root, newNode);
fixInsert(newNode);
}
// Standard BST insertion
private Node bstInsert(Node root, Node node) {
if (root == null)
return node;
if (node.data < root.data) {
root.left = bstInsert(root.left, node);
root.left.parent = root;
} else if (node.data > root.data) {
root.right = bstInsert(root.right, node);
root.right.parent = root;
}
return root;
}
// Fix Red-Black Tree violations after insertion
private void fixInsert(Node node) {
Node parent, grandparent;
while (node != root && node.color == RED && node.parent.color == RED) {
parent = node.parent;
grandparent = parent.parent;
if (parent == grandparent.left) {
Node uncle = grandparent.right;
if (uncle != null && uncle.color == RED) { // Case 1: Uncle red
parent.color = BLACK;
uncle.color = BLACK;
grandparent.color = RED;
node = grandparent;
} else {
if (node == parent.right) { // Case 2: node is right child
leftRotate(parent);
node = parent;
parent = node.parent;
}
// Case 3: node is left child
rightRotate(grandparent);
boolean tmpColor = parent.color;
parent.color = grandparent.color;
grandparent.color = tmpColor;
node = parent;
}
} else {
Node uncle = grandparent.left;
if (uncle != null && uncle.color == RED) { // Case 1
parent.color = BLACK;
uncle.color = BLACK;
grandparent.color = RED;
node = grandparent;
} else {
if (node == parent.left) { // Case 2
rightRotate(parent);
node = parent;
parent = node.parent;
}
// Case 3
leftRotate(grandparent);
boolean tmpColor = parent.color;
parent.color = grandparent.color;
grandparent.color = tmpColor;
node = parent;
}
}
}
root.color = BLACK; // root must always be black
}
// Inorder traversal (sorted order)
public void inorder() {
inorderHelper(root);
System.out.println();
}
private void inorderHelper(Node node) {
if (node != null) {
inorderHelper(node.left);
System.out.print(node.data + (node.color == RED ? "(R) " : "(B) "));
inorderHelper(node.right);
}
}
// Preorder traversal (tree structure)
public void preorder() {
preorderHelper(root);
System.out.println();
}
private void preorderHelper(Node node) {
if (node != null) {
System.out.print(node.data + (node.color == RED ? "(R) " : "(B) "));
preorderHelper(node.left);
preorderHelper(node.right);
}
}
// Search for a value in the tree
public boolean search(int key) {
Node current = root;
while (current != null) {
if (key < current.data)
current = current.left;
else if (key > current.data)
current = current.right;
else
return true;
}
return false;
}
// Main method to test the Red-Black Tree
public static void main(String[] args) {
RedBlackTree tree = new RedBlackTree();
int[] values = { 10, 20, 30, 15, 25, 5, 1, 50, 60 };
for (int val : values)
tree.insert(val);
System.out.println("Inorder traversal (sorted with colors):");
tree.inorder();
System.out.println("Preorder traversal (tree structure with colors):");
tree.preorder();
System.out.println("Search for 25: " + tree.search(25));
System.out.println("Search for 100: " + tree.search(100));
}
}