DSA Assignment - 6
DSA Assignment - 6
class AVLTree {
class Node {
Node(int d) {
key = d;
height = 1;
Node root;
int height(Node N) {
if (N == null)
return 0;
return N.height;
Node rightRotate(Node y) {
Node x = y.left;
Node T2 = x.right;
x.right = y;
y.left = T2;
return x;
Node leftRotate(Node x) {
Node y = x.right;
Node T2 = y.left;
y.left = x;
x.right = T2;
return y;
}
int getBalance(Node N) {
if (N == null)
return 0;
if (node == null)
else
return node;
node.height = 1 + max(height(node.left),
height(node.right));
return rightRotate(node);
return leftRotate(node);
return rightRotate(node);
node.right = rightRotate(node.right);
return leftRotate(node);
return node;
if (node != null) {
preOrder(node.left);
preOrder(node.right);
tree.preOrder(tree.root);
}
Q2.Get 20 numbers from user and store in array. Create a Binary
search tree in the sequence of input. Perform the following:
1) Insert an element into the BST.
2) Delete an element from BST.
3) Search an element in BST.
import java.util.Scanner;
class BST {
class Node {
int key;
Node(int item) {
key = item;
Node root;
BST() {
root = null;
if (root == null) {
return root;
return root;
if (root == null)
return root;
else {
if (root.left == null)
return root.right;
return root.left;
root.key = minValue(root.right);
return root;
minv = root.left.key;
root = root.left;
return minv;
if (root == null)
return false;
if (key == root.key)
return true;
void inorder() {
inorderRec(root);
if (root != null) {
inorderRec(root.left);
inorderRec(root.right);
}
public static void main(String[] args) {
numbers[i] = sc.nextInt();
bst.insert(numbers[i]);
bst.inorder();
bst.insert(insertElement);
bst.inorder();
bst.delete(deleteElement);
bst.inorder();
if (bst.search(searchElement))
System.out.println("Element found");
else
System.out.println("Element not found");