0% found this document useful (0 votes)
6 views

Message

Uploaded by

silpaganena
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Message

Uploaded by

silpaganena
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

//DO NOT EDIT ANYTHING IN SOLUTION CLASS

import java.util.*;
class AVLTree
{ // Insert, Delete == O(log2N)
Node root;
class Node{
int data, height;
Node left, right;
Node(int v) { this.data = v; height = 1; }
}
int height(Node n){
if( n == null )return 0;
return n.height;
}
int balanceFactor(Node n){
if(n == null) return 0;
return height(n.left) - height(n.right);
}
void insert(int v) {
root = insert(root, v);
}
Node insert(Node n, int v){
if( n == null ) return new Node(v);
if(v < n.data) n.left = insert(n.left, v);
else if(v > n.data) n.right = insert(n.right, v);
else if(v == n.data) return n;
n.height = Integer.max(height(n.left), height(n.right))+1;
int bf = balanceFactor(n);
if(bf < -1){
if( v < n.right.data ) n.right = rightRotate(n.right);
return leftRotate(n);
}
if(bf > 1){
if(v > n.left.data) n.left = leftRotate(n.left);
return rightRotate(n);
}
return n;
}
Node rightRotate(Node r) {
Node x = r.left;
r.left = x.right;
x.right = r;

r.height = Integer.max(height(r.left), height(r.right))+1;


x.height = Integer.max(height(x.left), height(x.right))+1;

return x;
}
Node leftRotate(Node r) {
Node x = r.right;
r.right = x.left;
x.left = r;

r.height = Integer.max(height(r.left), height(r.right))+1;


x.height = Integer.max(height(x.left), height(x.right))+1;

return x;
}
int depth(Node n){
if(n == null) return 0;
return Integer.max(height(n.left), height(n.right))+1;
}
void lvlOrd(Node n) // LevelOrder Traversal - BFS
{
ArrayDeque <Node> q = new ArrayDeque<>();
q.add(n);
while( !q.isEmpty() ){
Node cur = q.poll();
System.out.print(cur.data + " ");
if(cur.left != null) q.add(cur.left);
if(cur.right != null) q.add(cur.right);
}
}
boolean binarySearch(int v){
return binarySearch(root, v);
}
boolean binarySearch(Node n,int v){
if(n == null) return false;
if(n.data == v) return true;
if(v < n.data) return binarySearch(n.left, v);
if(v > n.data) return binarySearch(n.right, v);
return false;
}
void delete(int v){ // Deletes a node
root = delete(root, v);
}
Node minNode(Node n){// Smallest node in a tree
if(n == null || n.left == null) return n;
return minNode(n.left);
}
Node delete(Node n, int v){
if(n == null) return n;
if(v < n.data) n.left = delete(n.left, v);
else if(v > n.data) n.right = delete(n.right, v);
else{
if(n.left == null) return n.right;
if(n.right == null) return n.left;
Node i = minNode(n.right);
n.data = i.data;
n.right = delete(n.right, n.data);
}
n.height = Integer.max(height(n.left), height(n.right))+1;
int bf = balanceFactor(n);
if(bf < -1){
if( balanceFactor(n.right) > 0 )
n.right = rightRotate(n.right);
return leftRotate(n);
}
if(bf > 1){
if( balanceFactor(n.left) < 0 )
n.left = leftRotate(n.left);
return rightRotate(n);
}
return n;
}
void inOrder(Node n) // InOrder Traversal - DFS (Ascending)
{
if(n == null) return;
inOrder(n.left);
System.out.print(n.data + " ");
inOrder(n.right);
}
void preOrder(Node n) // PreOrder Traversal - DFS
{
if(n == null) return;
System.out.print(n.data + " ");
preOrder(n.left);
preOrder(n.right);
}
void postOrder(Node n) // PostOrder Traversal - DFS
{
if(n == null) return;
postOrder(n.left);
postOrder(n.right);
System.out.print(n.data + " ");
}
}
public class Solution
{
public static void main(String[] args)
{

Scanner sc = new Scanner(System.in);


AVLTree bt = new AVLTree();
int t = sc.nextInt();
for(int i=0;i<t;i++){
int c = sc.nextInt();
switch(c){
case 1 -> bt.insert(sc.nextInt());
case 2 -> bt.delete(sc.nextInt());
case 3 -> System.out.println(bt.binarySearch(sc.nextInt()));
}
}
System.out.println("-------------------------AVL
Tree-------------------------");
System.out.println("DFS: ");
System.out.print("IN-ORDER: ");
bt.inOrder(bt.root);
System.out.print("\nPRE-ORDER: ");
bt.preOrder(bt.root);
System.out.print("\nPOST-ORDER: ");
bt.postOrder(bt.root);
System.out.println("\nBFS: ");
System.out.print("Level-Order: ");
bt.lvlOrd(bt.root);
}
}

You might also like