0% found this document useful (0 votes)
27 views10 pages

DSA Assignment - 6

Uploaded by

aryan.23bce8252
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views10 pages

DSA Assignment - 6

Uploaded by

aryan.23bce8252
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

DSA Assignment - 6

Thopireddy Aryan Reddy


23BCE8252
Q1.Create AVL Tree (Balanced BST) for the following
sequence 3,2,1,4,5,6,7,8,9

class AVLTree {

class Node {

int key, height;

Node left, right;

Node(int d) {

key = d;

height = 1;

Node root;

int height(Node N) {

if (N == null)

return 0;

return N.height;

int max(int a, int b) {


return (a > b) ? a : b;

Node rightRotate(Node y) {

Node x = y.left;

Node T2 = x.right;

x.right = y;

y.left = T2;

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

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

return x;

Node leftRotate(Node x) {

Node y = x.right;

Node T2 = y.left;

y.left = x;

x.right = T2;

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

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

return y;
}

int getBalance(Node N) {

if (N == null)

return 0;

return height(N.left) - height(N.right);

Node insert(Node node, int key) {

if (node == null)

return (new Node(key));

if (key < node.key)

node.left = insert(node.left, key);

else if (key > node.key)

node.right = insert(node.right, key);

else

return node;

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

int balance = getBalance(node);

if (balance > 1 && key < node.left.key)

return rightRotate(node);

if (balance < -1 && key > node.right.key)

return leftRotate(node);

if (balance > 1 && key > node.left.key) {


node.left = leftRotate(node.left);

return rightRotate(node);

if (balance < -1 && key < node.right.key) {

node.right = rightRotate(node.right);

return leftRotate(node);

return node;

void preOrder(Node node) {

if (node != null) {

System.out.print(node.key + " ");

preOrder(node.left);

preOrder(node.right);

public static void main(String[] args) {

AVLTree tree = new AVLTree();

int[] keys = {3, 2, 1, 4, 5, 6, 7, 8, 9};

for (int key : keys) {

tree.root = tree.insert(tree.root, key);

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 left, right;

Node(int item) {

key = item;

left = right = null;

Node root;

BST() {

root = null;

void insert(int key) {


root = insertRec(root, key);

Node insertRec(Node root, int key) {

if (root == null) {

root = new Node(key);

return root;

if (key < root.key)

root.left = insertRec(root.left, key);

else if (key > root.key)

root.right = insertRec(root.right, key);

return root;

void delete(int key) {

root = deleteRec(root, key);

Node deleteRec(Node root, int key) {

if (root == null)

return root;

if (key < root.key)

root.left = deleteRec(root.left, key);


else if (key > root.key)

root.right = deleteRec(root.right, key);

else {

if (root.left == null)

return root.right;

else if (root.right == null)

return root.left;

root.key = minValue(root.right);

root.right = deleteRec(root.right, root.key);

return root;

int minValue(Node root) {

int minv = root.key;

while (root.left != null) {

minv = root.left.key;

root = root.left;

return minv;

boolean search(int key) {

return searchRec(root, key);


}

boolean searchRec(Node root, int key) {

if (root == null)

return false;

if (key == root.key)

return true;

if (key < root.key)

return searchRec(root.left, key);

return searchRec(root.right, key);

void inorder() {

inorderRec(root);

void inorderRec(Node root) {

if (root != null) {

inorderRec(root.left);

System.out.print(root.key + " ");

inorderRec(root.right);

}
public static void main(String[] args) {

BST bst = new BST();

Scanner sc = new Scanner(System.in);

int[] numbers = new int[20];

for (int i = 0; i < 20; i++) {

numbers[i] = sc.nextInt();

bst.insert(numbers[i]);

bst.inorder();

System.out.println("Enter element to insert:");

int insertElement = sc.nextInt();

bst.insert(insertElement);

bst.inorder();

System.out.println("Enter element to delete:");

int deleteElement = sc.nextInt();

bst.delete(deleteElement);

bst.inorder();

System.out.println("Enter element to search:");

int searchElement = sc.nextInt();

if (bst.search(searchElement))

System.out.println("Element found");

else
System.out.println("Element not found");

You might also like