-
Notifications
You must be signed in to change notification settings - Fork 0
/
a_168_AVL.java
261 lines (194 loc) · 6.46 KB
/
a_168_AVL.java
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
public class a_168_AVL {
static class Node{
int data , height ;
Node left , right ;
Node(int data) {
this.data = data ;
height = 1 ;
}
}
public static Node root ;
public static int height(Node root){
if(root == null){
return 0 ;
}
return root.height ;
}
// Right rotate subtree rooted with y
public static Node rightRotate(Node y){
Node x = y.left ;
Node T2 = x.right ;
// rotation using 3 nodes
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 ;
// x is new root
return x ;
}
// Left rotate subtree rooted with x
public static Node leftRotate(Node x){
Node y = x.right ;
Node T2 = y.left ;
// rotation using 3 nodes
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 ;
// y is new root
return y ;
}
// Get Balance factor of node
public static int getBalance(Node root){
if(root == null){
return 0 ;
}
return height(root.left) - height(root.right) ;
}
public static Node insertToAVL(Node root,int key) {
if(root == null) {
return new Node(key) ;
}
if(key < root.data) {
root.left = insertToAVL(root.left, key) ;
}
else if(key > root.data) {
root.right = insertToAVL(root.right, key) ;
}
else {
return root ;
}
// Duplicate keys not allowed
// Update root height
root.height = 1 + Math.max(height(root.left), height(root.right ) ) ;
// Get root's balance factor
int bf = getBalance(root) ;
// Left Left Case
if(bf > 1 && key < root.left.data){
return rightRotate(root) ;
}
// Right Right Case
if(bf < -1 && key > root.right.data){
return leftRotate(root) ;
}
// Left Right Case
if( bf > 1 && key > root.left.data){
root.left = leftRotate(root.left) ;
return rightRotate(root) ;
}
// Right Left Case
if(bf < -1 && key < root.right.data){
root.right = rightRotate(root.right) ;
return leftRotate(root) ;
}
return root ;
}
public static void preorder(Node root){
if(root == null){
return ;
}
System.out.print(root.data + " ") ;
preorder(root.left) ;
preorder(root.right) ;
}
// ********* DELETE AVL **********
//for non-empty BST, return the node with MIN data
public static Node getMinNode(Node root){
Node curr = root ;
//MIN data is at left-most node
while(curr.left != null){
curr = curr.left ;
}
return curr ;
}
public static Node deleteNode(Node root, int key){
// perform usual BST delete
if(root == null){
return root ;
}
// key < root's data => then it lies in left subtree
if(key < root.data){
root.left = deleteNode(root.left, key) ;
}
// key > root's data => then it lies in right subtree
else if(key > root.data) {
root.right = deleteNode(root.right, key);
}
// key = root's data => then this is the node to be deleted
else{
// node with only one child or no child
if((root.left == null) || (root.right == null)){
Node temp = null ;
if(temp == root.left){
temp = root.right ;
}
else {
temp = root.left ;
}
// No child case
if(temp == null){
temp = root ;
root = null ;
}
else // One child case
root = temp ; // Copy the contents of the non-empty child
}
else {
// node with two children: Get the inorder
// successor (smallest in the right subtree)
Node temp = getMinNode(root.right) ;
// Copy the inorder successor's data to this node
root.data = temp.data ;
// Delete the inorder successor
root.right = deleteNode(root.right, temp.data) ;
}
}
// If the tree had only one node then return
if(root == null){
return root ;
}
// update height of curr node
root.height = Math.max(height(root.left), height(root.right) ) + 1 ;
// get balance factor of this node (to checkfor unbalanced)
int bf = getBalance(root);
// If this node becomes unbalanced, then thereare 4 cases
// Left Left Case
if(bf > 1 && getBalance(root.left) >= 0){
return rightRotate(root) ;
}
// Left Right Case
if(bf > 1 && getBalance(root.left) < 0){
root.left = leftRotate(root.left) ;
return rightRotate(root) ;
}
// Right Right Case
if(bf < -1 && getBalance(root.right) <= 0){
return leftRotate(root) ;
}
// Right Left Case
if(bf < -1 && getBalance(root.right) > 0){
root.right = rightRotate(root.right) ;
return leftRotate(root);
}
return root ;
}
public static void main(String[] args) {
root = insertToAVL(root,10) ;
root = insertToAVL(root,20) ;
root = insertToAVL(root,30) ;
root = insertToAVL(root,40) ;
root = insertToAVL(root,50) ;
root = insertToAVL(root,25) ;
/*
AVL Tree
30
/ \
20 40
/ \ \
10 25 50
*/
preorder(root);
}
}