This repository was archived by the owner on Dec 21, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRedBlackTree.java
More file actions
217 lines (194 loc) · 7.12 KB
/
Copy pathRedBlackTree.java
File metadata and controls
217 lines (194 loc) · 7.12 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/* This is an implementation of a RedBlackTree based on the Tree class.
* The balancing of this class has been partially built on the strategy used on the following webpage: https://www.geeksforgeeks.org/red-black-tree-set-2-insert/
* The RedBlackTree balances a Tree which makes searching the structure much more efficient.
* However the structuring is not entirely perfect (see AVLTree) but this fairly well structured Tree is
* easy to obtain and especially maintain. It is ideal for dynamic Trees which are updated frequently.
* Author: Seppe Lampe
*/
public class RedBlackTree extends Tree{
public class RedBlackTreeNode extends Tree.TreeNode{
private RedBlackTreeNode parentNode;
private boolean colour; // This is the colour of the node. red = false, black = true.
public RedBlackTreeNode(Comparable v) {
super(v);
colour = false;
parentNode = null;
}
public RedBlackTreeNode(Comparable v, RedBlackTreeNode left, RedBlackTreeNode right, RedBlackTreeNode parent) {
super(v, left, right);
parentNode = parent;
colour = false;
}
public RedBlackTreeNode getLeftNode() {
return (RedBlackTreeNode) leftNode;
}
public void setLeftNode(RedBlackTreeNode n) {
leftNode = n;
if (n!= null) {
n.setParentNode(this);
}
}
public RedBlackTreeNode getRightNode() {
return (RedBlackTreeNode) rightNode;
}
public void setRightNode(RedBlackTreeNode n) {
rightNode = n;
if (n!= null) {
n.setParentNode(this);
}
}
public RedBlackTreeNode getParentNode() {
return parentNode;
}
public void setParentNode(RedBlackTreeNode parentNode) {
this.parentNode = parentNode;
}
public boolean isBlack() {
return colour;
}
public void setColourBlack() {
this.colour = true;
}
public void setColourRed() {
this.colour = false;
}
@Override
public int compareTo(Object o) { // O(1)
return value.compareTo(((RedBlackTreeNode) o).value);
}
}
public void insert(Comparable element) { // O(log(n))
insertAtNode(element, (RedBlackTreeNode) root, null);
}
private void insertAtNode(Comparable element, RedBlackTreeNode current, RedBlackTreeNode parent) { // O(log(n))
if (current == null) { // if the node we check is empty
RedBlackTreeNode newNode = new RedBlackTreeNode(element);
if (parent != null) { // the current node is empty, but we have a parent
if (element.compareTo(parent.value) < 0) { // do we add it to the left?
parent.setLeftNode(newNode);
}
else {
parent.setRightNode(newNode); // or do we add it to the right?
}
balance(parent);
}
else { // the current node is empty and it has no parent, we actually have an empty tree
root = newNode;
}
count += 1;
} else if (element.compareTo(current.value) == 0) {
System.out.println("Element is already in tree."); // if the element is already in the tree, what to do?
} else if (element.compareTo(current.value) < 0) { // if the element is smaller than current, go left
insertAtNode(element, current.getLeftNode(), current);
} else // if the element is bigger than current, go right
insertAtNode(element, current.getRightNode(), current);
}
private void splayLeft(RedBlackTreeNode n) { // O(1)
/* p p
/ /
n r
/ \ --> / \
l r n rr
/ \ / \
rl rr l rl
*/
RedBlackTreeNode rightTree = n.getRightNode();
RedBlackTreeNode parent = n.getParentNode();
// The original leftTree of the rightTree of n becomes the new rightTree of n
n.setRightNode(rightTree.getLeftNode());
// n becomes the leftTree of r (its rightTree)
rightTree.setLeftNode(n);
if(n == super.root) {
super.root = rightTree;
}
// The rightTree now comes at the place where n used to be
else {
if(parent.getLeftNode() == n) {
parent.setLeftNode(rightTree);
}
else {
parent.setRightNode(rightTree);
}
}
}
private void splayRight(RedBlackTreeNode n) { // Similar to splayLeft but mirrored
/* p p
/ /
n l
/ \ --> / \
l r ll n
/ \ / \
ll lr lr r
*/
RedBlackTreeNode leftTree = n.getLeftNode();
RedBlackTreeNode parent = n.getParentNode();
n.setLeftNode(leftTree.getRightNode());
leftTree.setRightNode(n);
if(n == root) {
root = leftTree;
}
else {
if(parent.getRightNode() == n) {
parent.setRightNode(leftTree);
}
else {
parent.setLeftNode(leftTree);
}
}
}
/* This part was based on the webpage mentioned in the beginning of the document.
However it has been written to function with pointers to the parentNode as I found this resulted
in more understandable/readable code. */
private void balance(RedBlackTreeNode n) {
if(n==root) {
n.setColourBlack();
}
else if(!n.getParentNode().isBlack()) { // Father is red
if(n.getParentNode().getParentNode().getRightNode() == null || n.getParentNode().getParentNode().getLeftNode() == null) { // Uncle is a leaf
blackUncleBalance(n);
}
else if(!n.getParentNode().getParentNode().getRightNode().isBlack() && !n.getParentNode().getParentNode().getLeftNode().isBlack()) { // Uncle is red as well
redUncleBalance(n);
}
else { // Uncle is black
blackUncleBalance(n);
}
}
}
private void redUncleBalance(RedBlackTreeNode n) {
RedBlackTreeNode parent = n.getParentNode();
RedBlackTreeNode grandparent = parent.getParentNode();
if(grandparent.getLeftNode() == parent) { // if parent is leftNode then uncle must be the RightNode of n's grandparent
grandparent.getRightNode().setColourBlack(); // Colour the uncle of n black
}
else {
grandparent.getLeftNode().setColourBlack(); // Colour the uncle of n black
}
parent.setColourBlack(); // Colour the father of n black
grandparent.setColourRed(); // n must have a grandparent since his parent was red, we color the grandparent red
balance(grandparent); // Balance the grandparent of n
}
private void blackUncleBalance(RedBlackTreeNode n) {
RedBlackTreeNode parent = n.getParentNode();
RedBlackTreeNode grandparent = parent.getParentNode();
if(grandparent.getLeftNode() == parent && parent.getLeftNode() == n) {
splayRight(grandparent);
parent.setColourBlack();
}
if(grandparent.getLeftNode() == parent && parent.getRightNode() == n) {
splayLeft(parent);
splayRight(grandparent);
n.setColourBlack();
}
if(grandparent.getRightNode() == parent && parent.getLeftNode() == n) {
splayRight(parent);
splayLeft(grandparent);
n.setColourBlack();
}
if(grandparent.getRightNode() == parent && parent.getRightNode() == n) {
splayLeft(grandparent);
parent.setColourBlack();
}
grandparent.setColourRed();
}
}