Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit bbf6152

Browse files
AVL-tree in Java
1 parent 45358cb commit bbf6152

File tree

3 files changed

+457
-0
lines changed

3 files changed

+457
-0
lines changed

‎Trees/avltree/AVLTree.java

Lines changed: 383 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,383 @@
1+
/*
2+
Copyright (C) Deepali Srivastava - All Rights Reserved
3+
This code is part of DSA course available on CourseGalaxy.com
4+
*/
5+
6+
package avltree;
7+
8+
public class AVLTree
9+
{
10+
private Node root;
11+
12+
static boolean taller;
13+
static boolean shorter;
14+
15+
public AVLTree()
16+
{
17+
root = null;
18+
}
19+
20+
public boolean isEmpty()
21+
{
22+
return (root==null);
23+
}
24+
25+
public void display()
26+
{
27+
display(root,0);
28+
System.out.println();
29+
}
30+
31+
private void display(Node p,int level)
32+
{
33+
int i;
34+
if(p==null)
35+
return;
36+
37+
display(p.rchild, level+1);
38+
System.out.println();
39+
40+
for(i=0; i<level; i++)
41+
System.out.print(" ");
42+
System.out.print(p.info);
43+
44+
display(p.lchild, level+1);
45+
}
46+
47+
public void inorder()
48+
{
49+
inorder(root);
50+
System.out.println();
51+
}
52+
53+
private void inorder(Node p)
54+
{
55+
if(p==null )
56+
return;
57+
inorder(p.lchild);
58+
System.out.print(p.info + " ");
59+
inorder(p.rchild);
60+
}
61+
62+
public void insert(int x)
63+
{
64+
root = insert(root,x);
65+
}
66+
67+
private Node insert(Node p, int x)
68+
{
69+
if(p==null)
70+
{
71+
p=new Node(x);
72+
taller = true;
73+
}
74+
else if(x < p.info)
75+
{
76+
p.lchild = insert(p.lchild,x);
77+
if(taller==true)
78+
p = insertionLeftSubtreeCheck(p);
79+
}
80+
else if(x > p.info)
81+
{
82+
p.rchild = insert(p.rchild,x);
83+
if(taller==true)
84+
p = insertionRightSubtreeCheck(p);
85+
}
86+
else
87+
{
88+
System.out.println(x + " already present in tree");
89+
taller = false;
90+
}
91+
return p;
92+
}
93+
94+
private Node insertionLeftSubtreeCheck(Node p)
95+
{
96+
switch(p.balance)
97+
{
98+
case 0: //Case L_1 : was balanced
99+
p.balance = 1; // now left heavy
100+
break;
101+
case -1: //Case L_2 : was right heavy
102+
p.balance = 0; // now balanced
103+
taller = false;
104+
break;
105+
case 1: // Case L_3 : was left heavy
106+
p = insertionLeftBalance(p); //Left Balancing
107+
taller = false;
108+
}
109+
return p;
110+
}
111+
112+
private Node insertionRightSubtreeCheck(Node p)
113+
{
114+
switch(p.balance)
115+
{
116+
case 0: // Case R_1 : was balanced
117+
p.balance = -1; // now right heavy
118+
break;
119+
case 1: // Case R_2 : was left heavy
120+
p.balance = 0; // now balanced
121+
taller = false;
122+
break;
123+
case -1: // Case R_3: Right heavy
124+
p = insertionRightBalance(p); // Right Balancing
125+
taller = false;
126+
}
127+
return p;
128+
}
129+
130+
private Node insertionLeftBalance(Node p)
131+
{
132+
Node a,b;
133+
134+
a = p.lchild;
135+
if(a.balance == 1) // Case L_3A : Insertion in AL
136+
{
137+
p.balance = 0;
138+
a.balance = 0;
139+
p = rotateRight(p);
140+
}
141+
else // Case L_3B : Insertion in AR
142+
{
143+
b = a.rchild;
144+
switch(b.balance)
145+
{
146+
case 1: // Case L_3B2 : Insertion in BL
147+
p.balance = -1;
148+
a.balance = 0;
149+
break;
150+
case -1: // Case L_3B2 : Insertion in BR
151+
p.balance = 0;
152+
a.balance = 1;
153+
break;
154+
case 0: // Case L_3B2 : B is the newly inserted node
155+
p.balance = 0;
156+
a.balance = 0;
157+
}
158+
b.balance = 0;
159+
p.lchild = rotateLeft(a);
160+
p = rotateRight(p);
161+
}
162+
return p;
163+
}
164+
165+
private Node insertionRightBalance(Node p)
166+
{
167+
Node a, b;
168+
169+
a = p.rchild;
170+
if(a.balance == -1) // Case R_3A : Insertion in AR
171+
{
172+
p.balance = 0;
173+
a.balance = 0;
174+
p = rotateLeft(p);
175+
}
176+
else // Case R_3B : Insertion in AL
177+
{
178+
b = a.lchild;
179+
switch(b.balance)
180+
{
181+
case -1: // Insertion in BR
182+
p.balance = 1;
183+
a.balance = 0;
184+
break;
185+
case 1: // Insertion in BL
186+
p.balance = 0;
187+
a.balance = -1;
188+
break;
189+
case 0: // B is the newly inserted node
190+
p.balance = 0;
191+
a.balance = 0;
192+
}
193+
b.balance = 0;
194+
p.rchild = rotateRight(a);
195+
p = rotateLeft(p);
196+
}
197+
return p;
198+
}
199+
200+
private Node rotateLeft(Node p)
201+
{
202+
Node a = p.rchild;
203+
p.rchild = a.lchild;
204+
a.lchild = p;
205+
return a;
206+
}
207+
208+
private Node rotateRight(Node p)
209+
{
210+
Node a = p.lchild;
211+
p.lchild = a.rchild;
212+
a.rchild = p;
213+
return a;
214+
}
215+
216+
public void delete(int x)
217+
{
218+
root = delete(root,x);
219+
}
220+
221+
private Node delete(Node p, int x)
222+
{
223+
Node ch,s;
224+
225+
if(p==null)
226+
{
227+
System.out.println(x + " not found");
228+
shorter = false;
229+
return p;
230+
}
231+
if(x < p.info) //delete from left subtree
232+
{
233+
p.lchild = delete(p.lchild, x);
234+
if(shorter == true)
235+
p = deletionLeftSubtreeCheck(p);
236+
}
237+
else if(x > p.info) //delete from right subtree
238+
{
239+
p.rchild = delete(p.rchild, x);
240+
if(shorter==true)
241+
p = deletionRightSubtreeCheck(p);
242+
}
243+
else
244+
{
245+
//key to be deleted is found
246+
if( p.lchild!=null && p.rchild!=null ) //2 children
247+
{
248+
s=p.rchild;
249+
while(s.lchild!=null)
250+
s=s.lchild;
251+
p.info=s.info;
252+
p.rchild = delete(p.rchild,s.info);
253+
if( shorter == true )
254+
p = deletionRightSubtreeCheck(p);
255+
}
256+
else //1 child or no child
257+
{
258+
if(p.lchild != null) //only left child
259+
ch=p.lchild;
260+
else //only right child or no child
261+
ch=p.rchild;
262+
p=ch;
263+
shorter = true;
264+
}
265+
}
266+
return p;
267+
}
268+
269+
private Node deletionLeftSubtreeCheck(Node p)
270+
{
271+
switch(p.balance)
272+
{
273+
case 0: // Case L_1 : was balanced
274+
p.balance = -1; // now right heavy
275+
shorter = false;
276+
break;
277+
case 1: // Case L_2 : was left heavy
278+
p.balance = 0; // now balanced
279+
break;
280+
case -1: // Case L_3 : was right heavy
281+
p = deletionRightBalance(p); //Right Balancing
282+
}
283+
return p;
284+
}
285+
286+
private Node deletionRightSubtreeCheck(Node p)
287+
{
288+
switch(p.balance)
289+
{
290+
case 0: // Case R_1 : was balanced
291+
p.balance = 1; // now left heavy
292+
shorter = false;
293+
break;
294+
case -1: // Case R_2 : was right heavy
295+
p.balance = 0; // now balanced
296+
break;
297+
case 1: // Case R_3 : was left heavy
298+
p = deletionLeftBalance(p); //Left Balancing
299+
}
300+
return p;
301+
}
302+
303+
private Node deletionLeftBalance(Node p)
304+
{
305+
Node a,b;
306+
a = p.lchild;
307+
if( a.balance == 0) // Case R_3A
308+
{
309+
a.balance = -1;
310+
shorter = false;
311+
p = rotateRight(p);
312+
}
313+
else if( a.balance == 1 ) // Case R_3B
314+
{
315+
p.balance = 0;
316+
a.balance = 0;
317+
p = rotateRight(p);
318+
}
319+
else // Case R_3C
320+
{
321+
b = a.rchild;
322+
switch(b.balance)
323+
{
324+
case 0: // Case R_3C1
325+
p.balance = 0;
326+
a.balance = 0;
327+
break;
328+
case -1: // Case R_3C2
329+
p.balance = 0;
330+
a.balance = 1;
331+
break;
332+
case 1: // Case R_3C3
333+
p.balance = -1;
334+
a.balance = 0;
335+
}
336+
b.balance = 0;
337+
p.lchild = rotateLeft(a);
338+
p = rotateRight(p);
339+
}
340+
return p;
341+
}
342+
343+
private Node deletionRightBalance(Node p)
344+
{
345+
Node a, b;
346+
347+
a = p.rchild;
348+
if (a.balance == 0) // Case L_3A
349+
{
350+
a.balance = 1;
351+
shorter = false;
352+
p = rotateLeft(p);
353+
}
354+
else if(a.balance == -1 ) // Case L_3B
355+
{
356+
p.balance = 0;
357+
a.balance = 0;
358+
p = rotateLeft(p);
359+
}
360+
else // Case L_3C
361+
{
362+
b = a.lchild;
363+
switch(b.balance)
364+
{
365+
case 0: // Case L_3C1
366+
p.balance = 0;
367+
a.balance = 0;
368+
break;
369+
case 1: // Case L_3C2
370+
p.balance = 0;
371+
a.balance = -1;
372+
break;
373+
case -1: // Case L_3C3
374+
p.balance = 1;
375+
a.balance = 0;
376+
}
377+
b.balance = 0;
378+
p.rchild = rotateRight(a);
379+
p = rotateLeft(p);
380+
}
381+
return p;
382+
}
383+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /