Repository navigation
Expand file tree
/
Copy pathRedBlackTree.java
More file actions
188 lines (168 loc) · 4.57 KB
/
Copy pathRedBlackTree.java
File metadata and controls
188 lines (168 loc) · 4.57 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
/*
Author : priyanka bhadoriya
Date : 02/23/2017
Implementing Red black tree in java
created functions
insertion
fix up insertion
calling left rotate and right rotate
created additional function to print the inorder traversal of tree.
*/
public class RedBlackTree {
private final int RED = 0;
private final int BLACK = 1;
private class Node {
int key = -1, color = BLACK;
Node left = nil, right = nil, parent = nil;
Node(int key)
{
this.key = key;
}
}
private final Node nil = new Node(-1);
private Node root = nil;
void printTreeInorder()
{
Inorder(root);
}
public void Inorder(Node node)
{
if (node == nil)
{
return;
}
Inorder(node.left);
System.out.print(((node.color==RED)?"Red:":"Black:")+node.key+" ");
Inorder(node.right);
}
void Insert(int key)
{
root = RBinsert(root,key);
}
private Node RBinsert(Node node, int key)
{
node = new Node(key);
Node y = nil;
Node x = root;
while(x!=nil)
{
y=x;
if(node.key<x.key)
x=x.left;
else
x=x.right;
}
node.parent = y;
if(y==nil)
root = node;
else if (node.key < y.key)
y.left = node;
else
y.right = node;
node.left = nil;
node.right = nil;
node.color = RED;
RBInsertfixup(node);
return node;
}
//Takes as argument the newly inserted node
private void RBInsertfixup(Node node)
{
while (node.parent.color == RED)
{
Node uncle = nil;
if (node.parent == node.parent.parent.left)
{
uncle = node.parent.parent.right;
if (uncle != nil && uncle.color == RED)
{
node.parent.color = BLACK;
uncle.color = BLACK;
node.parent.parent.color = RED;
node = node.parent.parent;
continue;
}
if (node == node.parent.right)
{
//Double rotation needed
node = node.parent;
rotateLeft(node);
}
node.parent.color = BLACK;
node.parent.parent.color = RED;
//if the "else if" code hasn't executed, this
//is a case where we only need a single rotation
rotateRight(node.parent.parent);
}
else
{
uncle = node.parent.parent.left;
if (uncle != nil && uncle.color == RED)
{
node.parent.color = BLACK;
uncle.color = BLACK;
node.parent.parent.color = RED;
node = node.parent.parent;
continue;
}
if (node == node.parent.left)
{
//Double rotation needed
node = node.parent;
rotateRight(node);
}
node.parent.color = BLACK;
node.parent.parent.color = RED;
//if the "else if" code hasn't executed, this
//is a case where we only need a single rotation
rotateLeft(node.parent.parent);
}
}
root.color = BLACK;
}
void rotateLeft(Node node)
{
Node y = node.right;
node.right = y.left;
if(y.left !=nil)
{
y.left.parent = node;
}
y.parent = node.parent;
if(node.parent == nil)
root = y;
else if (node == node.parent.left)
node.parent.left = y;
else
node.parent.right = y;
y.left = node;
node.parent = y;
}
void rotateRight(Node node)
{
Node x = node.left;
node.left = x.right;
if(x.right !=nil)
{
x.right.parent = node;
}
x.parent = node.parent;
if(node.parent == nil)
root = x;
else if (node == node.parent.left)
node.parent.left = x;
else
node.parent.right = x;
x.right = node;
node.parent = x;
}
public static void main(String[] args)
{
RedBlackTree rbt = new RedBlackTree();
rbt.Insert(30);
rbt.Insert(10);
rbt.Insert(15);
System.out.println("Inorder traversal of tree :");
rbt.printTreeInorder();
}
}