-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAVLTree.java
More file actions
149 lines (128 loc) · 4.52 KB
/
AVLTree.java
File metadata and controls
149 lines (128 loc) · 4.52 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
// Make sure the Node class is either in its own file or defined here
public class AVLTree {
private Node root;
// --- Utility functions (height, getBalance) ---
private int height(Node N) {
return N == null ? 0 : N.height;
}
private int getBalance(Node N) {
return (N == null) ? 0 : height(N.left) - height(N.right);
}
// --- Rotations (left, right) ---
private Node rightRotate(Node y) {
Node x = y.left;
Node T2 = x.right;
x.right = y;
y.left = T2;
y.height = Math.max(height(y.left), height(y.right)) + 1;
x.height = Math.max(height(x.left), height(x.right)) + 1;
return x;
}
private Node leftRotate(Node x) {
Node y = x.right;
Node T2 = y.left;
y.left = x;
x.right = T2;
x.height = Math.max(height(x.left), height(x.right)) + 1;
y.height = Math.max(height(y.left), height(y.right)) + 1;
return y;
}
// --- Insert ---
public void insert(String key) {
root = insertRec(root, key);
}
private Node insertRec(Node node, String key) {
if (node == null)
return new Node(key);
if (key.compareTo(node.key) < 0)
node.left = insertRec(node.left, key);
else if (key.compareTo(node.key) > 0)
node.right = insertRec(node.right, key);
else
return node; // Duplicate keys not allowed
node.height = 1 + Math.max(height(node.left), height(node.right));
int balance = getBalance(node);
// Rebalance
if (balance > 1 && key.compareTo(node.left.key) < 0)
return rightRotate(node);
if (balance < -1 && key.compareTo(node.right.key) > 0)
return leftRotate(node);
if (balance > 1 && key.compareTo(node.left.key) > 0) {
node.left = leftRotate(node.left);
return rightRotate(node);
}
if (balance < -1 && key.compareTo(node.right.key) < 0) {
node.right = rightRotate(node.right);
return leftRotate(node);
}
return node;
}
// --- Delete (NEWLY ADDED) ---
public void delete(String key) {
root = deleteRec(root, key);
}
private Node deleteRec(Node root, String key) {
if (root == null)
return root;
// Find the node
if (key.compareTo(root.key) < 0)
root.left = deleteRec(root.left, key);
else if (key.compareTo(root.key) > 0)
root.right = deleteRec(root.right, key);
else {
// Node found. Handle cases:
// 1. Node with only one child or no child
if ((root.left == null) || (root.right == null)) {
Node temp = (root.left != null) ? root.left : root.right;
if (temp == null) { // No child
root = null;
} else { // One child
root = temp;
}
} else {
// 2. Node with two children
Node temp = minValueNode(root.right);
root.key = temp.key; // Copy inorder successor's data
root.right = deleteRec(root.right, temp.key); // Delete successor
}
}
if (root == null)
return root; // If tree had only one node
// Update height
root.height = Math.max(height(root.left), height(root.right)) + 1;
// Get balance
int balance = getBalance(root);
// Rebalance (4 cases)
if (balance > 1 && getBalance(root.left) >= 0)
return rightRotate(root);
if (balance > 1 && getBalance(root.left) < 0) {
root.left = leftRotate(root.left);
return rightRotate(root);
}
if (balance < -1 && getBalance(root.right) <= 0)
return leftRotate(root);
if (balance < -1 && getBalance(root.right) > 0) {
root.right = rightRotate(root.right);
return leftRotate(root);
}
return root;
}
// Helper to find the node with the smallest key (inorder successor)
private Node minValueNode(Node node) {
Node current = node;
while (current.left != null)
current = current.left;
return current;
}
// --- Traversal ---
public void inOrder() {
inOrderRec(root);
}
private void inOrderRec(Node node) {
if (node != null) {
inOrderRec(node.left);
System.out.print(node.key + " ");
inOrderRec(node.right);
}
}
}