-
Notifications
You must be signed in to change notification settings - Fork 0
/
AVLTree.java
273 lines (246 loc) · 7.79 KB
/
AVLTree.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
262
263
264
265
266
267
268
269
270
271
272
import java.util.*;
public class AVLTree<T extends Comparable<? super T>> {
Node<T> root;
int nodeCount;
public AVLTree() {
}
public AVLTree(T value) {
this.root = new Node<>(value);
nodeCount++;
}
// Check if this binary tree is empty
public boolean isEmpty() {
return size() == 0;
}
// Get the number of nodes in this binary tree
public int size() {
return nodeCount;
}
// insert a node in BST
public boolean insert(T value){
// Check if the value already exists in this binary tree, if it does ignore adding it
if (contains(value))
return false;
// Otherwise add this element to the binary tree
root =insert(value,root);
nodeCount++;
return true;
}
private Node<T> insert(T value , Node<T> curRoot){
//tree is empty
if(curRoot==null){
curRoot=new Node<>(value);
return curRoot;
}
//traverse the tree
if(value.compareTo(curRoot.value)<0) //insert in the left subtree
curRoot.left=insert(value,curRoot.left);
else if(value.compareTo(curRoot.value)>0) //insert in the right subtree
curRoot.right=insert(value,curRoot.right);
update(curRoot);
return balance(curRoot);
}
public boolean remove(T value){
if(contains(value)){
remove(root,value);
nodeCount--;
return true;
}
return false;
}
private Node<T> remove(Node<T> node, T value) {
if(node==null)
return node;
int comp=value.compareTo(node.value);
// Dig into left subtree, the value we're looking for is smaller than the current value
if(comp<0) node.left=remove(node.left,value);
// Dig into right subtree, the value we're looking for is greater than the current value
else if(comp>0) node.right=remove(node.right,value);
// Found the node we wish to remove
else {
if(node.left==null)
return node.right;
else if(node.right==null)
return node.left;
else {
if(node.right.height>node.left.height) {
Node<T> temp = minValue(node.right);
node.value = temp.value;
node.right = remove(node.right, temp.value);
}else {
Node<T> temp = maxValue(node.left);
node.value = temp.value;
node.left = remove(node.left, temp.value);
}
}
}
update(node);
return balance(node);
}
// Update a node's height and balance factor.
public void update (Node<T> node){
int leftHeight = (node.left==null?-1 : node.left.height);
int rightHeight = (node.right==null?-1 : node.right.height);
// Update balance factor.
node.bf =leftHeight-rightHeight;
// Update this node's height.
node.height=1+Math.max(leftHeight,rightHeight);
}
// Re-balance a node if its balance factor is +2 or -2.
private Node<T> balance (Node<T> node){
// Left heavy subtree.
if(node.bf ==2){
if(node.left.bf>0)
return leftLeftCase(node);
else return leftRightCase(node);
}
// Right heavy subtree needs balancing.
else if(node.bf==-2){
if(node.right.bf<0)
return rigthRightCase(node);
else return rightLeftCase(node);
}
// Node either has a balance factor of 0, +1 or -1 which is fine.
return node;
}
private Node<T> leftLeftCase(Node<T> node){
return rightRotation(node);
}
private Node<T> rigthRightCase(Node<T> node){
return leftRotation(node);
}
private Node<T> leftRightCase(Node<T> node){
node.left=leftRotation(node.left);
return leftLeftCase(node);
}
private Node<T> rightLeftCase(Node<T> node){
node.right=rightRotation(node.right);
return rigthRightCase(node);
}
private Node<T> rightRotation(Node<T> node) {
Node<T> newParent = node.left;
node.left = newParent.right;
newParent.right = node;
update(node);
update(newParent);
return newParent;
}
private Node<T> leftRotation(Node<T> node) {
Node<T> newParent = node.right;
node.right = newParent.left;
newParent.left = node;
update(node);
update(newParent);
return newParent;
}
private boolean contains(T value) {
return search(value)!=null;
}
public Node<T> search(T value) {
return search(value,root);
}
private Node<T> search(T value, Node<T> curRoot) {
if(curRoot==null||curRoot.value==value )
return curRoot;
if(value.compareTo(curRoot.value)<0)
return search(value,curRoot.left);
else return search(value,curRoot.right);
}
// method to find the leftmost node (which has the smallest value)
public Node<T> minValue(Node <T> cur){
while(cur!=null && cur.left!=null)
cur=cur.left;
return cur;
}
// method to find the rightmost node (which has the largest value)
public Node<T> maxValue(Node<T> cur) {
while(cur!=null && cur.right!=null)
cur=cur.right;
return cur;
}
// Computes the height of the tree, O(n)
public int height() {
return height(root);
}
// Recursive helper method to compute the height of the tree
private int height(Node<T> cur ) {
if(cur==null)
return 0;
return Math.max( height(cur.left), height(cur.right))+1 ;
}
// method for inorder traversal of BST
public void inOrder(){
inOrder(root);
System.out.println();
}
private void inOrder(Node<T> curRoot){
if(curRoot!=null){
inOrder(curRoot.left);
System.out.print(curRoot.value+" ");
inOrder(curRoot.right);
}
}
// method for PreOrder traversal of BST
public void PreOrder(){
PreOrder(root);
System.out.println();
}
private void PreOrder(Node<T> curRoot){
if(curRoot!=null){
System.out.print(curRoot.value+" ");
PreOrder(curRoot.left);
PreOrder(curRoot.right);
}
}
// method for PostOrder traversal of BST
public void PostOrder(){
PostOrder(root);
System.out.println();
}
private void PostOrder(Node<T> curRoot){
if(curRoot!=null){
PostOrder(curRoot.left);
PostOrder(curRoot.right);
System.out.print(curRoot.value+" ");
}
}
//print level by level from left to right (up to down)
public void LevelOrder(){
BFs(root);
System.out.println();
}
private void BFs(Node root){
Queue<Node<T>> q=new LinkedList<>();
HashMap<T,Integer>level=new HashMap<>();
if(root!=null) {
q.add(root);
level.put((T) root.value, 1);
}
while(!q.isEmpty()){
Node<T>node=q.remove();
System.out.print(node.value+" ");
if(node.left!=null) {
q.add(node.left);
level.put((T) node.left.value, level.get(node.value) + 1);
}
if(node.right!=null) {
q.add(node.right);
level.put((T) node.right.value, level.get(node.value) + 1);
}
}
}
public class Node<T extends Comparable<? super T>> {
T value;
int bf;
int height;
Node left;
Node right;
public Node(T value) {
this.value = value;
}
@Override
public String toString() {
return value+"" ;
}
}
}