-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathAVLTree.java
More file actions
234 lines (187 loc) · 5.79 KB
/
AVLTree.java
File metadata and controls
234 lines (187 loc) · 5.79 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
// Metadata Header (MANDATORY)
// -----------------------------
// Program Title: AVL Tree Implementation
// Author: [Madipadige-ManishKumar]
// Date: 2025-10-10
//
// Description: Implements the AVL Self-Balancing Binary Search Tree.
//
// Language: Java
//
// Time Complexity: O(log n).
// Space Complexity: O(n).
// -----------------------------
class AVLTree {
// Node class - Declared as private for better encapsulation
private class Node {
int key, height;
Node left, right;
Node(int data) {
key = data;
height = 1;
}
}
private Node root; // Should be private
// ... (rest of the excellent code follows)
class AVLTree {
// Node class
class Node {
int key, height;
Node left, right;
Node(int data) {
key = data;
height = 1;
}
}
private Node root;
// Utility: get height of a node
int height(Node n) {
if (n == null) return 0;
return n.height;
}
// Utility: get balance factor of a node
int getBalance(Node n) {
if (n == null) return 0;
return height(n.left) - height(n.right);
}
// Right rotation (LL rotation)
Node rotateRight(Node y) {
Node x = y.left;
Node T2 = x.right;
// Perform rotation
x.right = y;
y.left = T2;
// Update heights
y.height = Math.max(height(y.left), height(y.right)) + 1;
x.height = Math.max(height(x.left), height(x.right)) + 1;
// Return new root
return x;
}
// Left rotation (RR rotation)
Node rotateLeft(Node x) {
Node y = x.right;
Node T2 = y.left;
// Perform rotation
y.left = x;
x.right = T2;
// Update heights
x.height = Math.max(height(x.left), height(x.right)) + 1;
y.height = Math.max(height(y.left), height(y.right)) + 1;
// Return new root
return y;
}
// Insert a key
Node insert(Node node, int key) {
// 1️⃣ Perform normal BST insertion
if (node == null) return new Node(key);
if (key < node.key)
node.left = insert(node.left, key);
else if (key > node.key)
node.right = insert(node.right, key);
else
return node; // Duplicate keys not allowed
// 2️⃣ Update height
node.height = 1 + Math.max(height(node.left), height(node.right));
// 3️⃣ Get balance factor
int balance = getBalance(node);
// 4️⃣ Balance the tree using rotations
// LL
if (balance > 1 && key < node.left.key)
return rotateRight(node);
// RR
if (balance < -1 && key > node.right.key)
return rotateLeft(node);
// LR
if (balance > 1 && key > node.left.key) {
node.left = rotateLeft(node.left);
return rotateRight(node);
}
// RL
if (balance < -1 && key < node.right.key) {
node.right = rotateRight(node.right);
return rotateLeft(node);
}
return node;
}
// Find node with minimum key
Node minValueNode(Node node) {
Node current = node;
while (current.left != null)
current = current.left;
return current;
}
// Delete a key
Node delete(Node root, int key) {
if (root == null)
return root;
// Perform standard BST delete
if (key < root.key)
root.left = delete(root.left, key);
else if (key > root.key)
root.right = delete(root.right, key);
else {
// Node with one or no child
if ((root.left == null) || (root.right == null)) {
Node temp = (root.left != null) ? root.left : root.right;
root = (temp == null) ? null : temp;
} else {
// Node with two children
Node temp = minValueNode(root.right);
root.key = temp.key;
root.right = delete(root.right, temp.key);
}
}
// If the tree had only one node
if (root == null)
return root;
// Update height
root.height = Math.max(height(root.left), height(root.right)) + 1;
// Check balance
int balance = getBalance(root);
// Perform rotations
if (balance > 1 && getBalance(root.left) >= 0)
return rotateRight(root);
if (balance > 1 && getBalance(root.left) < 0) {
root.left = rotateLeft(root.left);
return rotateRight(root);
}
if (balance < -1 && getBalance(root.right) <= 0)
return rotateLeft(root);
if (balance < -1 && getBalance(root.right) > 0) {
root.right = rotateRight(root.right);
return rotateLeft(root);
}
return root;
}
// Public methods for user
public void insert(int key) {
root = insert(root, key);
}
public void delete(int key) {
root = delete(root, key);
}
// Inorder Traversal
public void inorder() {
inorderTraversal(root);
System.out.println();
}
private void inorderTraversal(Node node) {
if (node != null) {
inorderTraversal(node.left);
System.out.print(node.key + " ");
inorderTraversal(node.right);
}
}
// Driver Example
public static void main(String[] args) {
AVLTree tree = new AVLTree();
int[] keys = {10, 20, 30, 40, 50, 25};
for (int key : keys)
tree.insert(key);
System.out.println("Inorder traversal after insertions:");
tree.inorder();
tree.delete(40);
System.out.println("After deleting 40:");
tree.inorder();
}
}