-
Notifications
You must be signed in to change notification settings - Fork 0
/
AVL_Tree.java
180 lines (139 loc) · 4.01 KB
/
AVL_Tree.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
public class AVL_Tree {
static class Node{
int data , height;
Node left , right ;
Node (int data){
this.data = data;
height = 1;
}
}
public static Node root;
public static int height(Node root ){
if(root== null){
return 0;
}
return root.height;
}
public static Node rightRotate(Node y){
Node x = y.left;
Node t2 = x.right;
x.right = y;
y.left = t2;
y.height = Math.max(height(y.left), height(y.right))+1;
x.height = Math.max(height(x.left),height(x.right))+1;
return x;
}
public static Node leftRotate(Node x){
Node y = x.right;
Node t2 = y.left;
y.left = x;
x.right = t2;
x.height = Math.max(height(x.left),height(x.right))+1;
y.height = Math.max(height(y.left), height(y.right))+1;
return y;
}
public static int getBalance(Node root){
if(root==null){
return 0 ;
}
return height(root.left)-height(root.right);
}
public static Node insert(Node root , int key){
if(root == null){
return new Node(key);
}
if(key<root.data){
root.left = insert(root.left, key);
}
else if(key>root.data){
root.right = insert(root.right, key);
}
else{
return root;
}
root.height = 1+Math.max(height(root.left),height(root.right));
int bf = getBalance(root);
if(bf>1 && key<root.left.data){
return rightRotate(root);
}
if(bf<-1 && key >root.right.data){
return leftRotate(root);
}
if(bf>1 && key>root.left.data){
root.left = leftRotate(root.left);
return rightRotate(root);
}
if(bf<-1 && key<root.right.data){
root.right = rightRotate(root.right);
return leftRotate(root);
}
return root;
}
public static void preorder(Node root){
if(root == null){
return;
}
System.out.print(root.data+" ");
preorder(root.left);
preorder(root.right);
}
public static Node getMinNode(Node root){
Node curr = root;
while(curr.left != null){
curr = curr.left;
}
return curr;
}
public static Node dltNode(Node root , int key){
if(root == null){
return null;
}
if(key<root.data){
root.left = dltNode(root.left, key);
}
else if(key>root.data){
root.right = dltNode(root.right, key);
}else{
if((root.left == null)|| (root.right == null)){
return null;
}
if(root.right == null){
return root.left;
}
if(root.left == null){
return root.right;
}
else{
Node temp = getMinNode(root.right);
root.data = temp.data;
root.right = dltNode(root.right, temp.data);
}
}
root.height = 1+Math.max(height(root.left),height(root.right));
int bf = getBalance(root);
if(bf>1 && key<root.left.data){
return rightRotate(root);
}
if(bf<-1 && key >root.right.data){
return leftRotate(root);
}
if(bf>1 && key>root.left.data){
root.left = leftRotate(root.left);
return rightRotate(root);
}
if(bf<-1 && key<root.right.data){
root.right = rightRotate(root.right);
return leftRotate(root);
}
return root;
}
public static void main(String arg[]){
root = insert(root, 10);
root = insert(root, 20);
root = insert(root, 30);
root = insert(root, 40);
root = insert(root, 50);
root = insert(root, 25);
preorder(root);
}
}