Professional Documents
Culture Documents
21K-3828 Lab9
21K-3828 Lab9
21K-3828 Lab9
class node{
node right;
node left;
double data;
node(double data){
this.data=data;
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;
if (node == null)
return;
print(node.left);
print(node.right);
if (root == null)
return root;
else {
if (root.left == null)
return root.right;
return root.left;
root.data = minValue(root.right);
return root;
}
double minValue(node root)
minv = root.left.data;
root = root.left;
return minv;
if (n==null){
return false;
return true;
return false;
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");
}
}
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);
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 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;
}
}
}