Skip to content

Commit 1806534

Browse files
committed
Add AVL Tree
1 parent 29bd445 commit 1806534

2 files changed

Lines changed: 128 additions & 0 deletions

File tree

src/main/java/org/alda/Main.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020
import org.alda.structure.stack.linkedList.IStackLinkedList;
2121
import org.alda.structure.stack.linkedList.StackLinkedList;
2222
import org.alda.structure.tree.bst.BinarySearchTree;
23+
import org.alda.structure.tree.bst.bbt.AVL;
24+
25+
import java.util.Arrays;
26+
import java.util.List;
2327

2428
public class Main {
2529
public static void main(String[] args) {
@@ -220,4 +224,13 @@ public static void testBST(){
220224
System.out.println(bst.inorderTraversal());
221225
System.out.println(bst.search(4));
222226
}
227+
228+
public static void testAVL(){
229+
AVL<Integer> avl = new AVL<>();
230+
AVL.Node<Integer> root = null;
231+
List<Integer> keys = Arrays.asList(10, 20, 30, 40, 50, 25);
232+
for(Integer key : keys){
233+
avl.insert(root, key);
234+
}
235+
}
223236
}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
package org.alda.structure.tree.bst.bbt;
2+
3+
/**
4+
* @author bcExpt1123
5+
*
6+
* <h1>BBT: Balanced Binary Tree</h1>
7+
*
8+
* <h2>AVL Tree</h2>
9+
* <p>An <b>AVL Tree</b> is a self-balancing binary search tree where the height difference
10+
* (balance factor) between the left and right subtrees of any node is at most <b>1</b>.</p>
11+
*
12+
* <h2>Balancing Factor</h2>
13+
* <p>The <b>balance factor</b> of a node is calculated as:</p>
14+
* <pre>
15+
* Balance Factor = Height of Right Subtree - Height of Left Subtree
16+
* </pre>
17+
* <p>The balance factor must always be in the range <b>[-1, 0, 1]</b> to maintain balance.</p>
18+
*
19+
* <h2>Operations</h2>
20+
* <ul>
21+
* <li><b>Rotation</b>: Used to restore balance after insertions and deletions. This includes:
22+
* <ul>
23+
* <li><b>Single Rotation</b></li>
24+
* <li><b>Double Rotation</b></li>
25+
* </ul>
26+
* </li>
27+
* <li><b>Insertion</b>: If inserting a node causes an imbalance, rotations are performed to restore balance.</li>
28+
* <li><b>Deletion</b>: Removing a node may also lead to an imbalance, requiring rotations to maintain the AVL property.</li>
29+
* </ul>
30+
*
31+
* <h2>Time Complexity</h2>
32+
* <p>Due to self-balancing, AVL trees provide efficient operations:</p>
33+
* <ul>
34+
* <li><b>Search</b>: O(log n)</li>
35+
* <li><b>Insertion</b>: O(log n)</li>
36+
* <li><b>Deletion</b>: O(log n)</li>
37+
* </ul>
38+
*/
39+
public class AVL<T extends Comparable<T>> {
40+
41+
public Node<T> insert(Node<T> root, T key) {
42+
if (root == null) {
43+
return new Node<>(key);
44+
} else if (key.compareTo(root.key) < 0) {
45+
root.left = insert(root.left, key);
46+
} else {
47+
root.right = insert(root.right, key);
48+
}
49+
50+
root.height = 1 + Math.max(getHeight(root.left), getHeight(root.right));
51+
Integer balance = getBalance(root);
52+
53+
if (balance > 1 && key.compareTo(root.left.key) < 0) {
54+
return rotateRight(root);
55+
}
56+
if (balance < -1 && key.compareTo(root.right.key) > 0) {
57+
return rotateLeft(root);
58+
}
59+
if (balance > 1 && key.compareTo(root.left.key) > 0) {
60+
root.left = rotateLeft(root.left);
61+
return rotateRight(root);
62+
}
63+
if (balance < -1 && key.compareTo(root.right.key) < 0) {
64+
root.right = rotateRight(root.right);
65+
return rotateLeft(root);
66+
}
67+
return root;
68+
}
69+
70+
public Integer getHeight(Node<T> root) {
71+
if(root == null) return 0;
72+
return root.height;
73+
}
74+
75+
public Integer getBalance(Node<T> root) {
76+
if(root == null) return 0;
77+
return getHeight(root.left) + getHeight(root.right);
78+
}
79+
80+
public Node<T> rotateRight(Node<T> z) {
81+
Node<T> y = z.left;
82+
Node<T> T = y.right;
83+
y.right = z;
84+
z.left = T;
85+
z.height = 1 + Math.max(getHeight(z.left), getHeight(z.right));
86+
y.height = 1 + Math.max(getHeight(y.left), getHeight(y.right));
87+
return y;
88+
}
89+
90+
public Node<T> rotateLeft(Node<T> z) {
91+
Node<T> y = z.right;
92+
Node<T> T = y.left;
93+
y.left = z;
94+
z.right = T;
95+
z.height = 1 + Math.max(getHeight(z.left), getHeight(z.right));
96+
y.height = 1 + Math.max(getHeight(y.left), getHeight(y.right));
97+
return y;
98+
}
99+
100+
public static class Node<T> {
101+
T key;
102+
Node<T> left;
103+
Node<T> right;
104+
Integer height;
105+
106+
public Node(T key){
107+
this.key = key;
108+
left = null;
109+
right = null;
110+
height = 1;
111+
}
112+
}
113+
}
114+
115+

0 commit comments

Comments
 (0)