-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathAVL Tree.java
More file actions
177 lines (144 loc) · 4.24 KB
/
AVL Tree.java
File metadata and controls
177 lines (144 loc) · 4.24 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
176
177
import java.io.*;
import java.util.*;
public 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 element)
{
if (node == null) {
return new Node(element);
}
if (node.data > element) {
node.left = this.insertToAVL(node.left, element);
} else if (node.data < element) {
node.right = this.insertToAVL(node.right, element);
}
int balanceFactor = calculateHeight(node.left) - calculateHeight(node.right);
if (balanceFactor >= 2) {
if (node.left.data > element) {
return this.rightRotation(node);
} else {
node.left = this.leftRotation(node.left);
return this.rightRotation(node);
}
} else if (balanceFactor <= -2) {
if (node.right.data < element) {
return this.leftRotation(node);
} else {
node.right = this.rightRotation(node.right);
return this.leftRotation(node);
}
}
return node;
}
private Node leftRotation(Node node) {
Node newRoot = node.right;
Node newRootLeft = newRoot.left;
node.right = newRootLeft;
newRoot.left = node;
return newRoot;
}
private Node rightRotation(Node node) {
Node newRoot = node.left;
Node newRootRight = newRoot.right;
node.left = newRootRight;
newRoot.right = node;
return newRoot;
}
private int calculateHeight(Node node) {
if (node == null) {
return 0;
}
return 1 + Math.max(calculateHeight(node.left), calculateHeight(node.right));
}
}