-
Notifications
You must be signed in to change notification settings - Fork 0
/
AVLTree.java
146 lines (143 loc) · 3.91 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
public class AVLTree {
static class AVLNode {
int data;
int height;
AVLNode left;
AVLNode right;
AVLNode(){}
AVLNode(int d){
this.data = d;
this.height = 1;
this.left = null;
this.right = null;
}
};
static AVLNode root;
private static int balance(AVLNode root) {
return getHeight(root.left) - getHeight(root.right);
}
private static int getHeight(AVLNode root){
if(root == null){
return 0;
}
return root.height;
}
private static AVLNode rotateLeft(AVLNode z, AVLNode y){
int bf = balance(y);
if(y.height < 0 && bf < -1){
y = LeftRight(y, y.right);
}
z = LeftLeft(z, y);
return z;
}
private static AVLNode rotateRight(AVLNode z, AVLNode y){
int bf = balance(y);
if(y.height > 0 && bf > 1){
y = RightLeft(y, y.left);
}
z = RightRight(z,y);
return z;
}
private static AVLNode LeftLeft(AVLNode z, AVLNode y){
z.left = y.right;
y.right = z;
z.height = Math.max(getHeight(z.left), getHeight(z.right))+1;
y.height = Math.max(getHeight(y.left), getHeight(y.right))+1;
return y;
}
private static AVLNode LeftRight(AVLNode y, AVLNode x){
y.right = x.left;
x.left = y;
y.height = Math.max(getHeight(y.left), getHeight(y.right))+1;
x.height = Math.max(getHeight(x.left), getHeight(x.right))+1;
return x;
}
private static AVLNode RightLeft(AVLNode y, AVLNode x){
y.left = x.right;
x.right = y;
y.height = Math.max(getHeight(y.left), getHeight(y.right))+1;
x.height = Math.max(getHeight(x.left), getHeight(x.right))+1;
return x;
}
private static AVLNode RightRight(AVLNode z, AVLNode y){
z.right = y.left;
y.left = z;
z.height = Math.max(getHeight(z.left), getHeight(z.right))+1;
y.height = Math.max(getHeight(y.left), getHeight(y.right))+1;
return y;
}
public static AVLNode insert(AVLNode root, int d){
if(root == null){
root = new AVLNode(d);
return root;
}
if(root.data > d){
root.left = insert(root.left, d);
} else if(root.data < d){
root.right = insert(root.right, d);
}
root.height = Math.max(getHeight(root.left), getHeight(root.right))+1;
int bf = balance(root);
if(bf > 1){
return rotateLeft(root, root.left);
} else if(bf < -1){
return rotateRight(root, root.right);
}
return root;
}
private static int getMin(AVLNode root){
if(root != null && root.left == null){
return root.data;
}
AVLNode ptr = root.left;
while(ptr != null && ptr.left != null){
ptr = ptr.left;
}
return ptr.data;
}
public static AVLNode delete(AVLNode root, int d){
if(root == null){
return null;
}
if(root.data > d){
root.left = delete(root.left, d);
} else if(root.data < d){
root.right = delete(root.right, d);
} else if(root.data == d) {
if(root.left != null && root.right != null){
int min = getMin(root.right);
root.data = min;
root.right = delete(root.right, root.data);
} else if(root.left == null){
return root.right;
} else if(root.right == null){
return root.left;
}
}
root.height = Math.max(getHeight(root.left), getHeight(root.right))+1;
int bf = balance(root);
if(bf > 1){
return rotateLeft(root, root.left);
} else if(bf < -1){
return rotateRight(root, root.right);
}
return root;
}
public static void preorder(AVLNode root){
if(root == null){
return;
}
System.out.print(root.data+" ");
preorder(root.left);
preorder(root.right);
}
public static void main(String args[]){
int arr[] = {6,5,1,11,10,9,8,7,2,3,4};
for(int i = 0; i < arr.length; i++){
root = insert(root, arr[i]);
}
preorder(root);
delete(root, 10);
preorder(root);
}
}