-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainClass.java
More file actions
197 lines (170 loc) · 7.19 KB
/
MainClass.java
File metadata and controls
197 lines (170 loc) · 7.19 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
public class MainClass{
public static void main(String[] args){
RedBlackTree t = new RedBlackTree();
t.insertNode(3);t.insertNode(2); t.insertNode(1); t.insertNode(4);
System.out.println(t);
}
}
class Node{
int value;
boolean color; // true = black, false = red
Node leftChild; // a < b
Node rightChild; // a > b
Node parent;
Node uncle;
public Node(int v){
this.value = v;
this.color = false; //new nodes are red
this.leftChild = null;
this.rightChild = null;
this.parent = null;
this.uncle = null;
}
public String toString(){
return this.value + ": " + (this.color ? "black" : "red");
}
}
class RedBlackTree{
Node root;
int size;
final int COUNT = 10;
/***
* initialize tree:
* - root set to null
* - root color set to black (true)
* - size is number of nodes in tree
*/
public RedBlackTree(){
this.root = null;
this.size = 0;
}
/***
*
* @param v: value assoc with new node
* Find the parent of the node we wish to insert, then insert new node as correct child of the parent
* Check balancing
*/
public void insertNode(int v){
this.size++;
if(this.root == null){
this.root = new Node(v);
this.root.color = true;
return;
}
Node newNode = new Node(v);
Node p = findParent(v);
if(p.parent != null){ // re-init uncle
if(p.value < p.parent.value){ newNode.uncle = p.parent.rightChild; }
else{ newNode.uncle = p.parent.leftChild; }
}
newNode.parent = p;
recolor(newNode);
if(v < p.value){
//System.out.println("insert left: "+ newNode);
p.leftChild = newNode;
return;
}
//System.out.println("insert right: "+ this.root.rightChild.parent);
p.rightChild = newNode;
return;
}
/***
* Find the parent node of the node we wish to add based on the input value
*/
public Node findParent(int v){
Node tmp = this.root;
while(tmp.leftChild != null
|| tmp.rightChild != null){
if(v < tmp.value){
if(tmp.leftChild == null){ return tmp; }
tmp = tmp.leftChild;
}else{
if(tmp.rightChild == null){ return tmp; }
tmp = tmp.rightChild;
}
}
return tmp;
}
/**
* Logic on cases pulled from GeeksforGeeks: https://www.geeksforgeeks.org/red-black-tree-set-2-insert/?ref=lbp
* @param x: current node being added to the tree
*/
public void recolor(Node x){
// if x is root, then do nothing
if(x.parent == null){ return; }
if(!x.parent.color){ // parent is RED
if(x.uncle == null || x.uncle.color){ //uncle is BLACK
// is parent a left child of grandp?
if(x.parent.value < x.parent.parent.value){
if(x.value < x.parent.value){ // LEFT-LEFT CASE
System.out.println("left-left: "+ x);
if(x.parent.parent.parent != null){// gp.parent = p
if(x.parent.parent.value < x.parent.parent.parent.value){ x.parent.parent.parent.leftChild = x.parent;} // left
else{x.parent.parent.parent.rightChild = x.parent;} // right
}
// if gp is ROOT, then p becomes ROOT
if(this.root.value == x.parent.parent.value){
this.root = x.parent;
x.parent.parent.color = false; // make OLD gp RED
}
if(x.parent.rightChild != null){ x.parent.rightChild.parent = x.parent.parent; }// p.right.parent = gp
x.parent.parent.leftChild = x.parent.rightChild; // gp.leftChild = p.rightChild
x.parent.rightChild = x.parent.parent; // p.rightChild = gp
x.parent.rightChild.parent = x.parent; // set parent for OLD gp
x.parent.color = true; // p.color = BLACK
return;
}else{ // LEFT-RIGHT CASE
System.out.println("left-right: "+ x);
return;
}
}else{
if(x.value < x.parent.value){ // RIGHT-LEFT CASE
System.out.println("right-left: "+ x); return;
}else{ // RIGHT-RIGHT CASE
System.out.println("right-right: "+ x);
if(x.parent.parent.parent != null){// gp.parent = p
if(x.parent.parent.value < x.parent.parent.parent.value){ x.parent.parent.parent.leftChild = x.parent;} // left
else{x.parent.parent.parent.rightChild = x.parent;} // right
}
// if gp is ROOT, then p becomes ROOT
if(this.root.value == x.parent.parent.value){
this.root = x.parent;
x.parent.parent.color = false; // make OLD gp RED
}
if(x.parent.leftChild != null){ x.parent.leftChild.parent = x.parent.parent; }// p.right.parent = gp
x.parent.parent.rightChild = x.parent.leftChild; // gp.leftChild = p.rightChild
x.parent.leftChild = x.parent.parent; // p.rightChild = gp
x.parent.leftChild.parent = x.parent; // set parent for OLD gp
x.parent.color = true; // p.color = BLACK
return;
}
}
}else{ //unlce is RED
System.out.println("uncle is red");
x.uncle.color = true; // change uncle to BLACK
x.parent.color = true;// change parent to BLACK
if(x.parent.parent.value != this.root.value){ x.parent.parent.color = false; } // change grandparent to RED
recolor(x.parent.parent);
return;
}
}
return;
}
// code to print tree from GeeksForGeeks - https://www.geeksforgeeks.org/print-binary-tree-2-dimensions/
public void printTree(Node root, int space){
// Base case
if (root == null){ return; }
// Increase distance between levels
space += COUNT;
// Process right child first
printTree(root.rightChild, space);
// Print current node after space
System.out.println("\n"+" ".repeat(space-COUNT) + root);
// Process left child
printTree(root.leftChild, space);
}
public String toString(){
printTree(this.root, 0);
return "\n";
}
}