-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAVL Tree Insertion.java
More file actions
175 lines (151 loc) · 4.42 KB
/
AVL Tree Insertion.java
File metadata and controls
175 lines (151 loc) · 4.42 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
170
171
172
173
174
175
//{ 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 node, int data) {
if (node == null) {
return new Node(data);
}
if (node.data > data) {
node.left = insertToAVL(node.left, data);
} else if (node.data < data) {
node.right = insertToAVL(node.right, data);
}
node.height = Math.max(height(node.left), height(node.right)) + 1;
int diff = height(node.left) - height(node.right);
if (diff > 1) { // Left subtree is higher
if (data < node.left.data) {
return rightRotate(node);
} else if (data > node.left.data) { // LR rotation
node.left = leftRotate(node.left);
return rightRotate(node);
}
} else if (diff < -1) { // Right subtree is higher
if (data < node.right.data) { // RL rotation
node.right = rightRotate(node.right);
return leftRotate(node);
} else if (data > node.right.data) {
return leftRotate(node);
}
}
return node;
}
public Node leftRotate(Node a) {
Node b = a.right;
Node t2 = b.left;
b.left = a;
a.right = t2;
a.height = Math.max(height(a.left), height(a.right)) + 1;
b.height = Math.max(height(b.left), height(b.right)) + 1;
return b;
}
public Node rightRotate(Node a) {
Node b = a.left;
Node t2 = b.right;
b.right = a;
a.left = t2;
a.height = Math.max(height(a.left), height(a.right)) + 1;
b.height = Math.max(height(b.left), height(b.right)) + 1;
return b;
}
public int height(Node node) {
if (node == null) {
return 0;
} else {
return node.height;
}
}
}