21K-3828 Lab9

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

Question 1

class node{

node right;

node left;

double data;

node(double data){

this.data=data;

node insert(double key,node n){

if (n==null){

n=new node(key);

return n;

}else if (key>n.data){

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

}else if (key<n.data){

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

return n;

void print(node node) {

if (node == null)

return;

/* first recur on left child */

print(node.left);

/* then print the data of node */

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


/* now recur on right child */

print(node.right);

node delete(node root, double key)

if (root == null)

return root;

if (key < root.data)

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

else if (key > root.data)

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

else {

if (root.left == null)

return root.right;

else if (root.right == null)

return root.left;

root.data = minValue(root.right);

root.right = delete(root.right, root.data);

return root;

}
double minValue(node root)

{ double minv = root.data;

while (root.left != null)

minv = root.left.data;

root = root.left;

return minv;

boolean check(node n){

if (n==null){

return false;

}else if (n.right.data>n.data && n.left.data<n.data){

return true;

return false;

public class lab8 {

public static void main(String[] args) {

node newnode=new node(7);

newnode.insert(1,newnode);

newnode.insert(13,newnode);

newnode.insert(5,newnode);

newnode.insert(9,newnode);

newnode.insert(14,newnode);
newnode.insert(12,newnode);

newnode.insert(8,newnode);

newnode.insert(4,newnode);

newnode.insert(6,newnode);

newnode.insert(3,newnode);

newnode.insert(11,newnode);

newnode.insert(3.5,newnode);

newnode.insert(4.5,newnode);

newnode.print(newnode);

System.out.println( );

newnode.delete(newnode,3);

newnode.delete(newnode,5);

newnode.print(newnode);

Question 2
public class lab9 {
public static void main(String[] args) {
node n1 = null;
bst b1 = new bst();
int[] value = {10, 5, 3, 7, 15, 18, 20, 25};
for (int i = 0; i < value.length; i++) {
n1 = b1.create(n1, value[i]);
}
inorder(n1);
boolean flag;
flag = true;
flag = checkbst(n1, flag);

if (flag) {
System.out.println("\nBST found");
} else {
System.out.println("\nBST not found");
}

public static void inorder(node root) {


if (root == null) {
return;
}
inorder(root.left);
System.out.print(root.key + " ");
inorder(root.right);
}

public static boolean checkbst(node root, boolean flag) {


if (root == null) {
return flag;
}
if (root.right != null && root.left != null) {
if (root.left != null) {
if (root.left.key > root.key) {
return false;
} else {
flag = checkbst(root.left, flag);
}
}
if (root.right != null) {
if (root.key > root.right.key) {
return false;
} else {
flag = checkbst(root.right, flag);
}
}
}
if (root.right == null) {
if (root.left != null) {
if (root.left.key > root.key) {
return false;
} else {
flag = checkbst(root.left, flag);
}
}
}
if (root.left == null && root.right != null) {
if (root.right.key < root.key) {
return false;
} else {
flag = checkbst(root.right, flag);
}
}
if (root.left == null && root.right == null) {
return flag;
}
return flag;
}

}
class node {
int key;
node left, right;

node() {

node(int data) {
this.key = data;
}

}
class bst {
node create(node root, int key) {
if (root == null) {
root = new node(key);
return root;
}
if (root.key > key) {
root.left = create(root.left, key);
} else {
root.right = create(root.right, key);
}
return root;
}

Question 3
public class lab9 {
public static void main(String[] args) {
node n1 = null;
bst b1 = new bst();
int[] value = {10, 5, 3, 7, 15, 18};
for (int i = 0; i < value.length; i++) {
n1 = b1.insert(n1, value[i]);
}
inorder(n1);
System.out.println();
multiplenode(n1);

public static void inorder(node root) {


if (root == null) {
return;
}
inorder(root.left);
System.out.print(root.key + " ");
inorder(root.right);
}

public static void multiplenode(node root) {


if (root == null ) {
return;
}

multiplenode(root.left);
multiplenode(root.right);
if (root.key % 5 == 0 && root.left != null && root.right != null) {
System.out.println("" + root.left.key + " " + root.right.key);
} else if (root.key % 5 == 0 && root.left != null && root.right == null) {
System.out.println(" " + root.left.key);
} else if (root.key % 5 == 0 && root.left == null && root.right != null) {
System.out.println(" " + root.right.key);
}

}
class node {
int key;
node left, right;
node() {

node(int data) {
this.key = data;
}

}
class bst {
node insert(node root, int key) {
if (root == null) {
root = new node(key);
return root;
}
if (root.key > key) {
root.left = insert(root.left, key);
} else {
root.right = insert(root.right, key);
}
return root;
}
}

Question 4
public class lab9 {
public static void main(String[] args) {
node n1 = null;
bst b1 = new bst();
int [] value = {10,5,3,7,15,18,20,25};
for(int i=0 ; i< value.length;i++){
n1 = b1.insert(n1, value[i]);
}
inorder(n1);
System.out.println();

count(n1, 18);
System.out.println("nodes having value less than or equal to key are: " + count );

}
public static void inorder(node root){
if(root == null){
return;
}
inorder(root.left);
System.out.print(root.key+" ");
inorder(root.right);
}
static int count =0;
public static void count(node root , int key) {
if (root == null) {
return;
}
if (root.key <= key) {
count++;
}
count(root.left, key);
count(root.right, key);
}
}

class node{
int key ;
node left ,right;
node(){

}
node(int data){
this.key = data;
}

}
class bst {
node insert(node root, int key) {
if (root == null) {
root = new node(key);
return root;
}
if (root.key > key) {
root.left = insert(root.left, key);
} else {
root.right = insert(root.right, key);
}
return root;
}
}

Question 5
import java.util.Scanner;

public class lab9 {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the nymber of nodes");
int a = sc.nextInt();
System.out.println("The number of unique BSTs are "+uniBST(a));

}
public static int uniBST(int n){
if(n<0){
return 0;
}
if(n<=1){
return 1;
}
else{
int sum =0;
for(int i =1 ;i<=n ;i++){
int left = uniBST(i-1);
int right = uniBST(n-i);
sum = sum + left*right;
}
return sum;
}

}
}

You might also like