-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAvls.java
More file actions
243 lines (196 loc) · 5.8 KB
/
Copy pathAvls.java
File metadata and controls
243 lines (196 loc) · 5.8 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
class AVLNode {
int data;
AVLNode left;
AVLNode right;
int height;
AVLNode(int value) {
this.data = value;
this.height = 1;
}
}
class AVLTree {
AVLNode root;
int height(AVLNode node) {
if (node == null) return 0;
return node.height;
}
int balancing(AVLNode node) {
if (node == null) return 0;
return height(node.left) - height(node.right);
}
AVLNode rightRotate(AVLNode y) {
AVLNode x = y.left;
AVLNode c = x.right;
x.right = y;
y.left = c;
y.height = Math.max(height(y.left), height(y.right))+1;
x.height = Math.max(height(x.left), height(x.right))+1;
return x;
}
AVLNode leftRotate(AVLNode x) {
AVLNode y = x.right;
AVLNode c = y.left;
y.left = x;
x.right = c;
x.height = Math.max(height(x.left), height(x.right)) + 1;
y.height = Math.max(height(y.left), height(y.right)) + 1;
return y;
}
AVLNode insert(AVLNode node, int value) {
if (node == null) {
return new AVLNode(value);
}
if (value < node.data) {
node.left = insert(node.left, value);
} else if (value > node.data) {
node.right = insert(node.right, value);
} else {
return node;
}
node.height = Math.max(height(node.left), height(node.right))+ 1;
int blnc = balancing(node);
if (blnc > 1 && value < node.left.data) {
return rightRotate(node);
}
if (blnc < -1 && value > node.right.data) {
return leftRotate(node);
}
if (blnc > 1 && value > node.left.data) {
node.left = leftRotate(node.left);
return rightRotate(node);
}
if (blnc < -1 && value < node.right.data) {
node.right = rightRotate(node.right);
return leftRotate(node);
}
return node;
}
void insert(int value) {
root = insert(root, value);
}
AVLNode minVal(AVLNode node) {
AVLNode curr = node;
while (curr.left != null) {
curr = curr.left;
}
return curr;
}
AVLNode deleteNode(AVLNode root, int value) {
if (root == null) {
return root;
}
if (value < root.data) {
root.left = deleteNode(root.left, value);
} else if (value > root.data) {
root.right = deleteNode(root.right, value);
} else {
if ((root.left == null) || (root.right == null)) {
AVLNode tmp = null;
if (tmp == root.left) {
tmp = root.right;
} else {
tmp = root.left;
}
if (tmp == null) {
tmp = root;
root = null;
} else {
root = tmp;
}
} else {
AVLNode temp = minVal(root.right);
root.data = temp.data;
root.right = deleteNode(root.right, temp.data);
}
}
if (root == null) {
return root;
}
root.height = Math.max(height(root.left), height(root.right)) + 1;
int blnc = balancing(root);
if (blnc > 1 && balancing(root.left) >= 0) {
return rightRotate(root);
}
if (blnc > 1 && balancing(root.left) < 0) {
root.left = leftRotate(root.left);
return rightRotate(root);
}
if (blnc < -1 && balancing(root.right) <= 0) {
return leftRotate(root);
}
if (blnc < -1 && balancing(root.right) > 0) {
root.right = rightRotate(root.right);
return leftRotate(root);
}
return root;
}
void delete(int value) {
root = deleteNode(root, value);
}
boolean search(AVLNode root, int value) {
if (root == null) {
return false;
}
if (value == root.data) {
return true;
}
else if (value < root.data) {
return search(root.left, value);
}
else {
return search(root.right, value);
}
}
boolean search(int value) {
return search(root, value);
}
void inorder(AVLNode root) {
if (root != null) {
inorder(root.left);
System.out.print(root.data + " ");
inorder(root.right);
}
}
void inorder() {
inorder(root);
System.out.println();
}
int findMax(AVLNode root) {
if (root == null) return -1;
while (root.right != null) {
root = root.right;
}
return root.data;
}
int findMax() {
return findMax(root);
}
int countNodes(AVLNode root) {
if (root == null) return 0;
return 1 + countNodes(root.left) + countNodes(root.right);
}
int countNodes() {
return countNodes(root);
}
}
public class Avls {
public static void main(String[] args) {
AVLTree avl = new AVLTree();
avl.insert(6);
avl.insert(2);
avl.insert(3);
avl.insert(5);
avl.inorder();
avl.delete(5);
avl.delete(3);
avl.inorder();
System.out.println(avl.findMax());
System.out.println(avl.countNodes());
avl.insert(8);
avl.insert(11);
avl.insert(9);
avl.inorder();
System.out.println(avl.search(2));
System.out.println(avl.countNodes());
}
}