I am getting Runtime(NZEC) error in the SPOJ Problem based on AVL Tree. Link to the problem is
AVL Tree
Can anyone help me?
My source code :
import java.util.Scanner;
class SDITSAVL{
static class Node{
int val;
int height;
int count;
Node left;
Node right;
Node(int val){
this.val = val;
height = 1;
left = null;
right = null;
count = 1;
}
}
static Node root;
static int count;
public static void main(String []arg){
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
root = null;
while(t-- > 0){
int x = sc.nextInt();
int y = sc.nextInt();
if(x == 1){
root = insert(root,y);
}else{
count = 1;
boolean ans = search(root,y);
if(!ans){
System.out.println("Data tidak ada");
}else{
System.out.println(count);
}
}
}
}
static Node insert(Node root,int val){
if(root == null){
root = new Node(val);
return root;
}else if(val < root.val){
root.left = insert(root.left,val);
}else if(val > root.val){
root.right = insert(root.right,val);
}
root.height = 1 + Math.max(getHeight(root.left),getHeight(root.right));
root.count = 1 + getCount(root.left) + getCount(root.right);
int bFactor = getBFactor(root);
if(bFactor > 1 && val < root.left.val){
return rightRotate(root);
}
if(bFactor > 1 && val > root.left.val){
root.left = leftRotate(root.left);
return rightRotate(root);
}
if(bFactor < -1 && val < root.right.val){
return leftRotate(root);
}
if(bFactor < -1 && val > root.right.val){
root.right = rightRotate(root.right);
return leftRotate(root);
}
return root;
}
static int getHeight(Node root){
if(root == null){
return 0;
}
return root.height;
}
static int getCount(Node root){
if(root == null){
return 0;
}
return root.count;
}
static int getBFactor(Node root){
if(root == null){
return 0;
}
return getHeight(root.left) - getHeight(root.right);
}
static Node rightRotate(Node root){
Node x = root.left;
Node y = x.right;
x.right = root;
root.left = y;
root.height = 1 + Math.max(getHeight(root.left),getHeight(root.right));
root.count = 1 + getCount(root.left) + getCount(root.right);
x.height = 1 + Math.max(getHeight(x.left),getHeight(x.right));
x.count = 1 + getCount(x.left) + getCount(x.right);
return x;
}
static Node leftRotate(Node root){
Node x = root.right;
Node y = x.left;
x.left = root;
root.right = y;
root.height = 1 + Math.max(getHeight(root.left),getHeight(root.right));
root.count = 1 + getCount(root.left) + getCount(root.right);
x.height = 1 + Math.max(getHeight(x.left),getHeight(x.right));
x.count = 1 + getCount(x.left) + getCount(x.right);
return x;
}
static boolean search(Node root,int val){
if(root == null){
return false;
}
if(val == root.val){
count += getCount(root.left);
return true;
}else if(val < root.val){
return search(root.left,val);
}else{
count += getCount(root.left);
return search(root.right,val);
}
}
}