-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAVLTree.java
More file actions
169 lines (140 loc) · 4.39 KB
/
AVLTree.java
File metadata and controls
169 lines (140 loc) · 4.39 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
public class AVLTree<T extends Comparable<? super T>> extends BST<T> {
protected int height;
public AVLTree() {
super();
height = -1;
}
public AVLTree(BSTNode<T> root) {
super(root);
height = -1;
}
public int getHeight() {
return getHeight(root);
}
private int getHeight(BSTNode<T> node) {
if(node == null)
return -1;
else
return 1 + Math.max(getHeight(node.left), getHeight(node.right));
}
private AVLTree<T> getLeftAVL() {
AVLTree<T> leftsubtree = new AVLTree<T>(root.left);
return leftsubtree;
}
private AVLTree<T> getRightAVL() {
AVLTree<T> rightsubtree = new AVLTree<T>(root.right);
return rightsubtree;
}
protected int getBalanceFactor() {
if(isEmpty())
return 0;
else
return getRightAVL().getHeight() - getLeftAVL().getHeight();
}
public void insertAVL(T el) {
super.insert(el);
this.balance();
}
public void deleteAVL(T el) {
//Q1
// Delete by a BST deletion by copying algorithm.
super.deleteByCopying(el);
// Rebalance the tree if an imbalance occurs.
this.balance();
}
protected void balance()
{
if(!isEmpty())
{
getLeftAVL().balance();
getRightAVL().balance();
adjustHeight();
int balanceFactor = getBalanceFactor();
if(balanceFactor == -2) {
//System.out.println("Balancing node with el: "+root.el);
if(getLeftAVL().getBalanceFactor() < 0)
rotateRight();
else
rotateLeftRight();
}
else if(balanceFactor == 2) {
//System.out.println("Balancing node with el: "+root.el);
if(getRightAVL().getBalanceFactor() > 0)
rotateLeft();
else
rotateRightLeft();
}
}
}
protected void adjustHeight()
{
if(isEmpty())
height = -1;
else
height = 1 + Math.max(getLeftAVL().getHeight(), getRightAVL().getHeight());
}
protected void rotateRight() {
//System.out.println("RIGHT ROTATION");
//Q1: Perform right rotation to rebalance the tree
// Store the reference to the right subtree in a temporary node
BSTNode<T> tempNode = root.right;
// Adjust the references to perform the rotation
root.right = root.left;
root.left = root.right.left;
root.right.left = root.right.right;
root.right.right = tempNode;
// Swap the values of the current node (root) and the right child node
T val = (T) root.el;
root.el = root.right.el;
root.right.el = val;
// Adjust the height of the right subtree
getRightAVL().adjustHeight();
// Adjust the height of the current node after rotation
adjustHeight();
}
protected void rotateLeft() {
//System.out.println("LEFT ROTATION");
BSTNode<T> tempNode = root.left;
root.left = root.right;
root.right = root.left.right;
root.left.right = root.left.left;
root.left.left = tempNode;
T val = (T) root.el;
root.el = root.left.el;
root.left.el = val;
getLeftAVL().adjustHeight();
adjustHeight();
}
protected void rotateLeftRight()
{
//System.out.println("Double Rotation...");
//Q1
// Perform a left rotation on the left subtree
getLeftAVL().rotateLeft();
// Adjust the height of the left subtree after rotation
getLeftAVL().adjustHeight();
// Perform a right rotation on the current node (this)
this.rotateRight();
// Adjust the height of the current node after rotation
this.adjustHeight();
}
protected void rotateRightLeft()
{
//System.out.println("Double Rotation...");
getRightAVL().rotateRight();
getRightAVL().adjustHeight();
this.rotateLeft();
this.adjustHeight();
}
// To calculate the Size
public int size() {
return calculateSize(root);
}
private int calculateSize(BSTNode<T> node) {
if (node == null) {
return 0;
} else {
return 1 + calculateSize(node.left) + calculateSize(node.right);
}
}
}