Skip to content

Commit 2bb2307

Browse files
Merge pull request #29 from theycallmemac/patch-2
Create AVLTree.java
2 parents ca4305a + f674f9d commit 2bb2307

File tree

1 file changed

+212
-0
lines changed

1 file changed

+212
-0
lines changed

data_structures/AVLTree.java

Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
public class AVLtree {
2+
3+
private Node root;
4+
5+
private class Node {
6+
private int key;
7+
private int balance;
8+
private int height;
9+
private Node left, right, parent;
10+
11+
Node(int k, Node p) {
12+
key = k;
13+
parent = p;
14+
}
15+
}
16+
17+
public boolean insert(int key) {
18+
if (root == null)
19+
root = new Node(key, null);
20+
else {
21+
Node n = root;
22+
Node parent;
23+
while (true) {
24+
if (n.key == key)
25+
return false;
26+
27+
parent = n;
28+
29+
boolean goLeft = n.key > key;
30+
n = goLeft ? n.left : n.right;
31+
32+
if (n == null) {
33+
if (goLeft) {
34+
parent.left = new Node(key, parent);
35+
} else {
36+
parent.right = new Node(key, parent);
37+
}
38+
rebalance(parent);
39+
break;
40+
}
41+
}
42+
}
43+
return true;
44+
}
45+
46+
private void delete(Node node){
47+
if(node.left == null && node.right == null){
48+
if(node.parent == null) root = null;
49+
else{
50+
Node parent = node.parent;
51+
if(parent.left == node){
52+
parent.left = null;
53+
}else parent.right = null;
54+
rebalance(parent);
55+
}
56+
return;
57+
}
58+
if(node.left!=null){
59+
Node child = node.left;
60+
while (child.right!=null) child = child.right;
61+
node.key = child.key;
62+
delete(child);
63+
}else{
64+
Node child = node.right;
65+
while (child.left!=null) child = child.left;
66+
node.key = child.key;
67+
delete(child);
68+
}
69+
}
70+
71+
public void delete(int delKey) {
72+
if (root == null)
73+
return;
74+
Node node = root;
75+
Node child = root;
76+
77+
while (child != null) {
78+
node = child;
79+
child = delKey >= node.key ? node.right : node.left;
80+
if (delKey == node.key) {
81+
delete(node);
82+
return;
83+
}
84+
}
85+
}
86+
87+
private void rebalance(Node n) {
88+
setBalance(n);
89+
90+
if (n.balance == -2) {
91+
if (height(n.left.left) >= height(n.left.right))
92+
n = rotateRight(n);
93+
else
94+
n = rotateLeftThenRight(n);
95+
96+
} else if (n.balance == 2) {
97+
if (height(n.right.right) >= height(n.right.left))
98+
n = rotateLeft(n);
99+
else
100+
n = rotateRightThenLeft(n);
101+
}
102+
103+
if (n.parent != null) {
104+
rebalance(n.parent);
105+
} else {
106+
root = n;
107+
}
108+
}
109+
110+
private Node rotateLeft(Node a) {
111+
112+
Node b = a.right;
113+
b.parent = a.parent;
114+
115+
a.right = b.left;
116+
117+
if (a.right != null)
118+
a.right.parent = a;
119+
120+
b.left = a;
121+
a.parent = b;
122+
123+
if (b.parent != null) {
124+
if (b.parent.right == a) {
125+
b.parent.right = b;
126+
} else {
127+
b.parent.left = b;
128+
}
129+
}
130+
131+
setBalance(a, b);
132+
133+
return b;
134+
}
135+
136+
private Node rotateRight(Node a) {
137+
138+
Node b = a.left;
139+
b.parent = a.parent;
140+
141+
a.left = b.right;
142+
143+
if (a.left != null)
144+
a.left.parent = a;
145+
146+
b.right = a;
147+
a.parent = b;
148+
149+
if (b.parent != null) {
150+
if (b.parent.right == a) {
151+
b.parent.right = b;
152+
} else {
153+
b.parent.left = b;
154+
}
155+
}
156+
157+
setBalance(a, b);
158+
159+
return b;
160+
}
161+
162+
private Node rotateLeftThenRight(Node n) {
163+
n.left = rotateLeft(n.left);
164+
return rotateRight(n);
165+
}
166+
167+
private Node rotateRightThenLeft(Node n) {
168+
n.right = rotateRight(n.right);
169+
return rotateLeft(n);
170+
}
171+
172+
private int height(Node n) {
173+
if (n == null)
174+
return -1;
175+
return n.height;
176+
}
177+
178+
private void setBalance(Node... nodes) {
179+
for (Node n : nodes)
180+
reheight(n);
181+
n.balance = height(n.right) - height(n.left);
182+
}
183+
184+
public void printBalance() {
185+
printBalance(root);
186+
}
187+
188+
private void printBalance(Node n) {
189+
if (n != null) {
190+
printBalance(n.left);
191+
System.out.printf("%s ", n.balance);
192+
printBalance(n.right);
193+
}
194+
}
195+
196+
private void reheight(Node node){
197+
if(node!=null){
198+
node.height=1 + Math.max(height(node.left), height(node.right));
199+
}
200+
}
201+
202+
public static void main(String[] args) {
203+
AVLtree tree = new AVLtree();
204+
205+
System.out.println("Inserting values 1 to 10");
206+
for (int i = 1; i < 10; i++)
207+
tree.insert(i);
208+
209+
System.out.print("Printing balance: ");
210+
tree.printBalance();
211+
}
212+
}

0 commit comments

Comments
 (0)