Length: Out Out

You might also like

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

TASK 1

public class Task1 {


public static void main(String[] args) {
float [] value = {7,3,11,1,5,4,6,9,8,13,14,3.5f,4.5f};
binarysearchtree b1 =new binarysearchtree();
node root = null ;
for(int i =0;i< value.length;i++) {
root = b1.create(value[i], root);
}
b1.inorder(root);
System.out.println();
root = b1.delete(3, root);
b1.inorder(root);
System.out.println();
root = b1.delete(5, root);
b1.inorder(root);
}
}
class node {
float key;
node left ,right;
node(){

}
node(float key){
this.key = key;
}
}
class binarysearchtree{
node create(float data,node root){
if(root == null){
root = new node(data);
return root;
}
if(root.key > data){
root.left = create(data,root.left);
}
else {
root.right = create(data, root.right);
}
return root;
}
public void inorder(node root){
if(root == null){
return;
}
inorder(root.left);
System.out.print(root.key+" ");
inorder(root.right);
}
public node delete(float key , node root){
if(root ==null){
return root;
}
if(root.key> key){
root.left = delete(key,root.left);
}
else if(root.key<key){
root.right = delete(key, root.right);
}
else{
if(root.left == null && root.right == null){
return null;
}
if(root.left == null){
return root.right;
}
else if(root.right == null){
return root.left;
}
node is = inordersuccessor(root.right);
root.key = is.key;
root.right = delete(is.key, root.right);

}
return root;
}
public node inordersuccessor(node root){
while(root.left != null){
root = root.left;
}
return root;
}
}
OUTPUT

TASK 2
public class Task2 {
public static void main(String[] args) {
node1 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");
}
multiplenode(n1);
smallcount(n1, 18);
System.out.println("Number of nodes having value less than equal to
key are " + count );

}
public static void inorder(node1 root){
if(root == null){
return;
}
inorder(root.left);
System.out.print(root.key+" ");
inorder(root.right);
}
public static boolean checkbst(node1 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 node1{
int key ;
node1 left ,right;
node1(){

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

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

TASK 3
public class Task2 {
public static void main(String[] args) {
node1 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");
}
multiplenode(n1);
smallcount(n1, 18);
System.out.println("Number of nodes having value less than equal to
key are " + count );

}
public static void inorder(node1 root){
if(root == null){
return;
}
inorder(root.left);
System.out.print(root.key+" ");
inorder(root.right);
}
public static void multiplenode(node1 root){
if(root == null){
return;
}
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);
}
multiplenode(root.left);
multiplenode(root.right);
}
class node1{
int key ;
node1 left ,right;
node1(){

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

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

TASK 4
public class Task2 {
public static void main(String[] args) {
node1 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);
System.out.println();
boolean flag ;
flag = true;
flag = checkbst(n1, flag);

if(flag){
System.out.println("\nBST found");
}
else {
System.out.println("\nBST not found");
}
multiplenode(n1);
smallcount(n1, 18);
System.out.println("Number of nodes having value less than equal to
key are " + count );
}
public static void inorder(node1 root){
if(root == null){
return;
}
inorder(root.left);
System.out.print(root.key+" ");
inorder(root.right);
}
static int count =0;
public static void smallcount(node1 root , int key) {
if (root == null) {
return;
}
if (root.key <= key) {
count++;
}
smallcount(root.left, key);
smallcount(root.right, key);
}
}
class node1{
int key ;
node1 left ,right;
node1(){

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

}
class bst{
node1 create(node1 root , int key){
if(root == null){
root = new node1(key);
return root;
}
if(root.key > key){
root.left = create(root.left, key);
}
else {
root.right = create(root.right,key);
}
return root;
}
}
OUTPUT
TASK 5
import java.util.Scanner;

public class Task5 {


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 BST is "+numofBST(a));

}
public static int numofBST(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 = numofBST(i-1);
int right = numofBST(n-i);
sum = sum + left*right;
}
return sum;
}

}
}
OUTPUT

You might also like