June 09, 2012

DS: Binary Search Tree Program

import java.io.*;
import java.util.Scanner;

public class BinaryOne {
public static void main(String args[]) {
int choice, value;
bst b = new bst();
do {
System.out.println("Binary Search Tree Operations");
System.out.println("1.create()");
System.out.println("2.display()");
System.out.println("3.empty()");
System.out.println("Enter U'r choice");
Scanner s = new Scanner(System.in);
choice = s.nextInt();
switch (choice) {
case 1:
System.out.println("Enter item");
value = s.nextInt();
b.insert(value);
break;
case 2:
b.inorder(b.root);
break;
case 3:
if (b.equals(null))
System.out.println("BSTree is empty");
else
System.out.println("BSTree is not empty");
break;
}
} while (choice < 4);
}
}

class node {
node right;
node left;
int data;
}

class bst {
node root, loc, par;
int item;

public void find(int x) {
int item;
item = x;
node ptr, save;
if (root == null) {
loc = null;
par = null;
return;
}
if (item == root.data) {
loc = root;
par = null;
return;
}
if (item < root.data) {
ptr = root.left;
save = root;
} else {
ptr = root.right;
save = root;
}
while (ptr != null) {
if (item == ptr.data) {
loc = ptr;
par = save;
return;
}
if (item < ptr.data) {
save = ptr;
ptr = ptr.left;
} else {
save = ptr;
ptr = ptr.right;
}
}
loc = null;
par = save;
return;
}

public void insert(int item) {
node temp;
find(item);
if (loc != null) {
System.out.println(item + "is already exists");
return;
}
temp = new node();
temp.data = item;
temp.left = null;
temp.right = null;
loc = temp;
if (par == null)
root = temp;
else {
if (item < par.data)
par.left = temp;
else
par.right = temp;
}
}

public void inorder(node p) {
if (p != null) {
inorder(p.left);
System.out.println("  " + p.data);
inorder(p.right);
}
}
}



Output:
Binary Search Tree Operations
1.create()                                                                    
2.display()  
3.empty()                                                                 
Enter U'r choice                                                               
1                                                                              
Enter item                                                                      
22
Binary Search Tree Operations                                                                            
1.create()                                                                      
2.display()                                                                    
3.empty()

Enter U'r choice                                                               
1                                                                               
Enter item                                                                     
1                                                                              
Binary Search Tree Operations
1.create()                                                                      
2.display() 
3.empty()                                                                  
Enter U'r choice                                                               
1                                                                               
Enter item                                                                     
76                                                                             
Binary Search Tree Operations
1.create()                                                                      
2.display()                               
3.empty()                                    
Enter U'r choice            
2                                                                                
1                                                                               
22                                                                             
54                                                                             
76                                                                            
Binary Search Tree Operations
1.create()                                                                     
2.display()
3.empty()                                                                   
Enter U'r choice 
3                                                                              
BSTree is not empty  
Binary Search Tree Operations
1.create()                                                                     
2.display()
3.empty()                                                                    
Enter U'r choice 
4                                                             

No comments:

Post a Comment

I'm certainly not an expert, but I'll try my hardest to explain what I do know and research what I don't know.

My Favorite Site's List

#update below script more than 500 posts