-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path23.java
209 lines (170 loc) · 5.03 KB
/
23.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
//{ Driver Code Starts
import java.io.*;
import java.util.*;
class pair
{
int first;
boolean second;
pair(int first, boolean second)
{
this.first = first;
this.second = second;
}
}
class Node
{
int data, height;
Node left, right;
Node(int x)
{
data=x;
left=right=null;
height=1;
}
}
class GfG
{
public static boolean isBST(Node n, int lower, int upper)
{
if(n==null) return true;
if( n.data <= lower || n.data >= upper ) return false;
return isBST(n.left, lower, n.data) && isBST(n.right, n.data, upper) ;
}
public static pair isBalanced(Node n)
{
if(n==null)
{
return new pair(0,true);
}
pair l = isBalanced(n.left);
pair r = isBalanced(n.right);
if( Math.abs(l.first - r.first) > 1 ) return new pair (0,false);
return new pair( 1 + Math.max(l.first , r.first) , l.second && r.second );
}
public static boolean isBalancedBST(Node root)
{
if( isBST(root, Integer.MIN_VALUE , Integer.MAX_VALUE) == false )
System.out.print("BST voilated, inorder traversal : ");
else if ( isBalanced(root).second == false)
System.out.print("Unbalanced BST, inorder traversal : ");
else return true;
return false;
}
public static void printInorder(Node n)
{
if(n==null) return;
printInorder(n.left);
System.out.print(n.data + " ");
printInorder(n.right);
}
public static void main(String args[])
{
int ip[] = new int[2000];
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-->0)
{
int n = sc.nextInt();
for(int i = 0; i < n; i++)
{
ip[i] = sc.nextInt();
}
Node root = null;
Solution obj = new Solution();
for(int i=0; i<n; i++)
{
root = obj.insertToAVL( root, ip[i] );
if ( isBalancedBST(root) == false )
break;
}
printInorder(root);
System.out.println();
}
}
}
// } Driver Code Ends
//User function Template for Java
/*
class Node
{
int data;
Node left;
Node right;
int height;
};
*/
class Solution
{
public Node insertToAVL(Node root, int data) {
if (root == null) {
return new Node(data);
}
if (data < root.data) {
root.left = insertToAVL(root.left, data);
} else if (data > root.data) {
root.right = insertToAVL(root.right, data);
} else {
// Duplicate data is not allowed in AVL tree
return root;
}
// Update height of the current node
root.height = 1 + Math.max(height(root.left), height(root.right));
// Get the balance factor of this ancestor node to check whether
// this node became unbalanced
int balance = getBalance(root);
// Perform rotations if needed
if (balance > 1 && data < root.left.data) {
return rightRotate(root);
}
if (balance < -1 && data > root.right.data) {
return leftRotate(root);
}
if (balance > 1 && data > root.left.data) {
root.left = leftRotate(root.left);
return rightRotate(root);
}
if (balance < -1 && data < root.right.data) {
root.right = rightRotate(root.right);
return leftRotate(root);
}
return root;
}
// Helper function to get the height of a node
private int height(Node node) {
if (node == null) {
return 0;
}
return node.height;
}
// Helper function to get the balance factor of a node
private int getBalance(Node node) {
if (node == null) {
return 0;
}
return height(node.left) - height(node.right);
}
// Helper function to perform a right rotation
private Node rightRotate(Node y) {
Node x = y.left;
Node T2 = x.right;
// Perform rotation
x.right = y;
y.left = T2;
// Update heights
y.height = 1 + Math.max(height(y.left), height(y.right));
x.height = 1 + Math.max(height(x.left), height(x.right));
return x;
}
// Helper function to perform a left rotation
private Node leftRotate(Node x) {
Node y = x.right;
Node T2 = y.left;
// Perform rotation
y.left = x;
x.right = T2;
// Update heights
x.height = 1 + Math.max(height(x.left), height(x.right));
y.height = 1 + Math.max(height(y.left), height(y.right));
return y;
}
}