java ile yazılmış bir bst uygulamasıdır //AnaClass.java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class AnaClass {//this is the class where all the codes are executed
public static void main(String[] args) {
Bstree tree = new Bstree();//we make a tree here
int choice=0,flag=0,result;/*choice for getting the users choice.
Flag is used for first iteration When the other iteration appears flag will
not be used again */
for (;choice !=3 {
if(flag==0){
choice=menu();/*if this is the first time (opening of the program)
send the menu information to the user */
flag=1;
}
switch(choice){//this switch decides which application will be used
case 1:result=addnode(tree);if (result==1){/*if the adding operation is successful
increase the tree counter*/
System.out.println("Added...\n");
tree.count=tree.count+1;
}
else System.out.println("Not Added...Please control your number\n");break;//so there must be a mistake
case 2:if(tree.count==0)//if the tree is empty then there is nothing to show
System.out.println("Tree is empty...\n");
else
printlist(tree.root);break;
}//end of switch
choice=menu();
}//end of for
System.out.println("Program terminated...\n");
}
public static int menu() {//this function is created for showing the options to the user
int i = 0;
for (;i!=1&&i!=2&&i!=3 {
BufferedReader cin = new BufferedReader(new InputStreamReader(
System.in));
System.out.println("\t 1- Add a node\n\t 2- Print the tree \n\t 3- Exit");
String choice;
try {
choice = cin.readLine();//we take the choice in string format
i = Integer.valueOf(choice);/*and turns it to the integer type
if we somehow do a mistake -entering a char instead of number- it will warn us*/
} //end of try
catch (IOException e) {
}
catch (NumberFormatException e) {
System.out.println("You have to enter a integer number between 1-3\n ");/*This is how the user
informed about the mistake*/
}
}//end of for
return i;//we return the number which the user choose
}//end of menu
static int addnode(Bstree tree){//this function is about adding the node to the tree
int data=-1;
String secim;//this variable is for getting the number from the user
System.out.println("Please enter a positive integer\n");
BufferedReader cin=new BufferedReader(new InputStreamReader(
System.in));
try{
secim=cin.readLine();
data=Integer.valueOf(secim);
}
catch (IOException e) {
}
catch (NumberFormatException e) {
System.out.println("You have to enter a positive integer number\n ");
}
if(data<0){
System.out.println("You have to enter a positive integer number\n ");
return 0;
}
Bnode node=new Bnode();//these 3 nodes will be used in adding operation
Bnode walk=new Bnode();
Bnode parent=new Bnode();
node.number=data;//data is the number which user wants to enter
node.left=null;
node.right=null;
if(tree.root==null){//if the root is empty
tree.root=node;//add the new node and make it the first -root-node of the tree
}
else{//if we are adding somewhere else
walk=tree.root;
while(walk!=null){//go to the concerned node
parent=walk;
if(node.number<parent.number)
walk=walk.left;
else
walk=walk.right;
}//end of while
if(node.number<parent.number)//and decide where to add-to the right or to the left-.
parent.left=node;
else if(node.number>parent.number)
parent.right=node;
else{//if the data is the same as the parent then we can not add it
System.out.println("You already have this number in your tree\n ");
return 0;
}
}//end of else
return 1;
}//end of adding
static void printlist(Bnode root){
if(root!=null){
printlist(root.left);//go to the leftmost
System.out.println(" "+root.number);//this model prints the tree by inorder method.
printlist(root.right);
}
}//end of printlist
}//end of anaclass
//Bnode.java
public class Bnode { //this is the node of tree
Bnode left;
Bnode right;
int number; //for keeping the number of the data
}
//Bstree.java
public class Bstree { //this is the class for binary search tree
int count;
Bnode root; //this is the root
Bstree(){
root=null;
count=0;
}
}
Binary Search Tree with java
Binary Search Tree with java
Konuyu Okuyanlar: 1 Ziyaretçi