-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAVLTreeMapWithBalance.java
More file actions
329 lines (269 loc) · 9.13 KB
/
Copy pathAVLTreeMapWithBalance.java
File metadata and controls
329 lines (269 loc) · 9.13 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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/* Author's Name: Mubasshir Al Shahriar
Relevant Course : CSCI 313: Data Structures */
import java.util.*;
public class AVLTreeMapWithBalance
{
static class Node // Since we will need to create node in our AVL tree.
{
int key, value;
int balance; // Balance factor
Node left, right, parent;
Node(int key, int value, Node parent)
{
this.key = key;
this.value = value;
this.parent = parent;
}
}
private Node root; // Root node of the tree.
/* This public method shall be used to insert a new key-value pair. */
public void put(int key, int value)
{
if (root == null)
{
root = new Node(key, value, null); /* It sets the first node as root node. */
}
else
{
insert(root, key, value); /* If there already exists a root and the current one is not the first node, it inserts recursively. */
}
}
/* This is recursive insert function with rebalancing (if necessary). */
private void insert(Node node, int key, int value)
{
if (key < node.key)
{
if (node.left == null)
{
node.left = new Node(key, value, node);
rebalance(node);
}
else
{
insert(node.left, key, value);
}
}
else if (key > node.key)
{
if (node.right == null)
{
node.right = new Node(key, value, node);
rebalance(node);
}
else
{
insert(node.right, key, value);
}
}
else
{
node.value = value; /* If the key already exists, then it just updates the value. */
}
}
/* This method removes a certain key. */
public void remove(int key)
{
delete(root, key);
}
/* This method deletes node recursively and rebalances if needed. */
private void delete(Node node, int key)
{
if (node == null) return;
if (key < node.key)
{
delete(node.left, key);
}
else if (key > node.key)
{
delete(node.right, key);
}
else
{
if (node.left == null && node.right == null) /* Checks if there is no children. */
{
replaceNodeInParent(node, null);
rebalance(node.parent);
}
else if (node.left != null && node.right != null) /* It checks if there are 2 child. */
{
Node successor = findMin(node.right); /* Finds smallest element's node in the right subtree. */
node.key = successor.key;
node.value = successor.value;
delete(successor, successor.key);
}
else // When it is a case of only 1 child.
{
Node child = (node.left != null) ? node.left : node.right;
replaceNodeInParent(node, child);
rebalance(node.parent);
}
}
}
/* This method replaces one node in the tree with another one (used in deletion and rotation). */
private void replaceNodeInParent(Node node, Node child)
{
if (node.parent == null)
{
root = child;
if (child != null) child.parent = null;
}
else if (node == node.parent.left)
{
node.parent.left = child;
if (child != null) child.parent = node.parent;
}
else
{
node.parent.right = child;
if (child != null) child.parent = node.parent;
}
}
/* This method finds the node with the minimum/leftmost key. */
private Node findMin(Node node)
{
while (node.left != null) node = node.left;
return node;
}
/* This method updates and rebalances the tree from this node upward. */
private void rebalance(Node node)
{
updateBalance(node);
if (node.balance == -2)
{
if (height(node.right.right) >= height(node.right.left))
{
rotateLeft(node); // Single left rotation
}
else
{
rotateRight(node.right); // Double rotation (right-left)
rotateLeft(node);
}
}
else if (node.balance == 2)
{
if (height(node.left.left) >= height(node.left.right))
{
rotateRight(node); // Single right rotation
}
else
{
rotateLeft(node.left);
rotateRight(node); // Double left-right rotation.
}
}
if (node.parent != null)
{
rebalance(node.parent); /* Keeps going upward and rebalances. */
}
}
/* This method updates the balance factor of a node. */
private void updateBalance(Node node)
{
node.balance = height(node.left) - height(node.right); /* As we learnt, as well as given in the question: "Balance Factor = height of its left subtree - the height of its right subtree." */
}
/* This method computes the height of a node recursively. */
private int height(Node node)
{
if (node == null) return 0;
return 1 + Math.max(height(node.left), height(node.right));
}
/* This method performs a left rotation around a node. */
private void rotateLeft(Node node)
{
Node right = node.right;
replaceNodeInParent(node, right); /* It moves right child up. */
node.right = right.left;
if (right.left != null) right.left.parent = node;
right.left = node;
node.parent = right;
updateBalance(node);
updateBalance(right);
}
/* This method performs a right rotation around a node. */
private void rotateRight(Node node)
{
Node left = node.left;
replaceNodeInParent(node, left); /* It moves left child up. */
node.left = left.right;
if (left.right != null) left.right.parent = node;
left.right = node;
node.parent = left;
updateBalance(node);
updateBalance(left);
}
/* Print method to show the correct output as a level by level tree structure. */
public void printTree()
{
if (root == null)
{
System.out.println("(empty)");
return;
}
List<List<String>> lines = new ArrayList<>();
List<Node> level = new ArrayList<>();
level.add(root);
int nonNulls = 1;
int widest = 0;
while (nonNulls != 0)
{
List<String> line = new ArrayList<>();
List<Node> next = new ArrayList<>();
nonNulls = 0;
for (Node node : level)
{
if (node == null)
{
line.add(null);
next.add(null);
next.add(null);
}
else
{
String val = Integer.toString(node.key);
line.add(val);
if (val.length() > widest) widest = val.length();
next.add(node.left);
next.add(node.right);
if (node.left != null) nonNulls++;
if (node.right != null) nonNulls++;
}
}
lines.add(line);
level = next;
}
int perPiece = lines.get(lines.size() - 1).size() * (widest + 4);
for (List<String> line : lines)
{
int halfPiece = (int) Math.floor(perPiece / 2f) - 1;
for (String val : line)
{
if (val == null) val = "";
int gap1 = (int) Math.ceil(perPiece / 2f - val.length() / 2f);
int gap2 = (int) Math.floor(perPiece / 2f - val.length() / 2f);
System.out.print(" ".repeat(gap1));
System.out.print(val);
System.out.print(" ".repeat(gap2));
}
System.out.println("\n\n");
perPiece /= 2;
}
}
/* Main method for this program. */
public static void main(String[] args)
{
AVLTreeMapWithBalance tree = new AVLTreeMapWithBalance();
/* Inserting some nodes as hardcoded. Here I am using the same nodes we saw in the lecture slides' example, so that we can confirm it is showing correct output. */
int[] keys = {44, 17, 62, 32, 50, 78, 48, 54, 88};
for (int key : keys)
{
tree.put(key, key * 10);
}
System.out.println("Tree After Initial Insertion:");
tree.printTree();
System.out.println("\nDeleting 32: ");
tree.remove(32);
System.out.println("Tree After Deletion:");
tree.printTree();
}
}