-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRedBlackTree.java
115 lines (95 loc) · 3.12 KB
/
RedBlackTree.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
public class RedBlackTree<T extends Comparable<T>> {
private Node<T> root;
private static final boolean RED = true;
private static final boolean BLACK = false;
private class Node<T> {
T key;
Node<T> left, right;
boolean color;
public Node(T key, boolean color) {
this.key = key;
this.color = color;
}
}
public RedBlackTree() {
root = null;
}
public void insert(T key) {
root = insert(root, key);
root.color = BLACK; // Ensure root remains black
}
private Node<T> insert(Node<T> node, T key) {
if (node == null) return new Node<>(key, RED);
int cmp = key.compareTo(node.key);
if (cmp < 0) node.left = insert(node.left, key);
else if (cmp > 0) node.right = insert(node.right, key);
// Balance the tree
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;
}
private boolean isRed(Node<T> node) {
return node != null && node.color == RED;
}
private Node<T> rotateLeft(Node<T> node) {
Node<T> x = node.right;
node.right = x.left;
x.left = node;
x.color = node.color;
node.color = RED;
return x;
}
private Node<T> rotateRight(Node<T> node) {
Node<T> x = node.left;
node.left = x.right;
x.right = node;
x.color = node.color;
node.color = RED;
return x;
}
private void flipColors(Node<T> node) {
node.color = RED;
node.left.color = BLACK;
node.right.color = BLACK;
}
// In-order traversal to print the tree
public void printInOrder() {
printInOrder(root);
System.out.println();
}
private void printInOrder(Node<T> node) {
if (node != null) {
printInOrder(node.left);
System.out.print(node.key + " ");
printInOrder(node.right);
}
}
// Search function to check if a key exists
public boolean contains(T key) {
return contains(root, key);
}
private boolean contains(Node<T> node, T key) {
if (node == null) return false;
int cmp = key.compareTo(node.key);
if (cmp < 0) return contains(node.left, key);
else if (cmp > 0) return contains(node.right, key);
else return true;
}
// Main function to demonstrate usage
public static void main(String[] args) {
RedBlackTree<Integer> rbt = new RedBlackTree<>();
// Insert sample values
rbt.insert(10);
rbt.insert(20);
rbt.insert(15);
rbt.insert(30);
rbt.insert(25);
// Print the tree in order
System.out.println("In-order traversal of Red-Black Tree:");
rbt.printInOrder();
// Search for specific keys
System.out.println("Does the tree contain 15? " + rbt.contains(15));
System.out.println("Does the tree contain 5? " + rbt.contains(5));
}
}