In data structure, Binary Search Tree is very important. Below are the instructions on how to create a Binary Search Tree in Java.
Remember the following: We used Book as our data and we load book information from file.
Step 1: Create a new class and named it with "BinaryTree".
Step 2: Insert the following codes below inside your new created java class.
Remember the following: We used Book as our data and we load book information from file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Step 1: declare root as new Book | |
Step 2: Create a method named addNode and include parameters | |
Step 3: inside the method, declare newBook as Book and initialized it | |
Step 4: create a if statement to identify if the root is null or empty | |
Step 5: initialized the root with newBook inside if block | |
Step 6: inside the else block, declare focusBook as new book and initilized it with root | |
Step 7: declare parent as new Book | |
Step 8: create a while loop with boolean expression parameters | |
Step 9: inside the while loop, initialized parent with focusbook | |
Step 10: create a new if statment to identify if the call Num is less than the focusBook.callNum | |
*/ | |
Book root; | |
public void addNode(int callNum, String bookName){ | |
Book newBook = new Book(callNum, bookName); | |
if(root == null){ | |
root = newBook; | |
}else{ | |
Book focusBook = root; | |
Book parent; | |
while (true){ | |
parent = focusBook; | |
if(callNum < focusBook.callNum){ | |
focusBook = focusBook.leftChild; | |
if(focusBook == null){ | |
parent.leftChild = newBook; | |
return; | |
} | |
}else{ | |
focusBook = focusBook.rightChild; | |
if(focusBook == null){ | |
parent.rightChild = newBook; | |
return; | |
} | |
} | |
} | |
} | |
} | |
/* | |
Step 1: create inOrderTraverseTree method with book parameters | |
Step 2: inside the method, create if statement to check if focusBook is null or empty. | |
Step 3: initialized inOrderTraverseTree with leftChild | |
Step 4: print the focusBook | |
Step 5: initialized inOrderTraverseTree with rightChilid | |
*/ | |
public void inOrderTraverseTree(Book focusBook){ | |
if(focusBook != null){ | |
inOrderTraverseTree(focusBook.leftChild); | |
System.out.println(focusBook); | |
inOrderTraverseTree(focusBook.rightChild); | |
} | |
} | |
/* | |
Step 1: create preOrderTraverseTree method with book parameters | |
Step 2: inside the method, create if statement to check if focusBook is null or empty. | |
Step 3: print the focusBook | |
Step 4: initialized preOrderTraverseTree with leftChild | |
Step 5: initialized preOrderTraverseTree with rightChilid | |
*/ | |
public void preOrderTraverseTree(Book fucosBook){ | |
if(fucosBook != null){ | |
System.out.println(fucosBook); | |
preOrderTraverseTree(fucosBook.leftChild); | |
preOrderTraverseTree(fucosBook.rightChild); | |
} | |
} | |
/* | |
Step 1: create postOrderTraverseTree method with book parameters | |
Step 2: inside the method, create if statement to check if focusBook is null or empty. | |
Step 3: initialized postOrderTraverseTree with leftChild | |
Step 4: initialized postOrderTraverseTree with rightChilid | |
Step 5: print the focusBook | |
*/ | |
public void postOrderTraverseTree(Book fucosBook){ | |
if(fucosBook != null){ | |
postOrderTraverseTree(fucosBook.leftChild); | |
postOrderTraverseTree(fucosBook.rightChild); | |
System.out.println(fucosBook); | |
} | |
} | |
/* | |
Step 1: create a main method used to run the BinaryTree | |
Step 2: declare theBook as new BinaryTree | |
Step 3: adding the node data | |
Step 4: Display the node by using post order, pre order, and in order | |
Step 5: Display the whatThis method | |
*/ | |
public static void main(String arg[]){ | |
BinaryTree theBook = new BinaryTree(); | |
theBook.addNode(50, "English 2"); | |
theBook.addNode(45, "Math 11"); | |
theBook.addNode(32, "Java Prog. Sixth Edition"); | |
theBook.addNode(20, "Physics 11"); | |
theBook.addNode(10, "History 2"); | |
//theBook.inOrderTraverseTree(theBook.root); | |
//theBook.preOrderTraverseTree(theBook.root); | |
theBook.postOrderTraverseTree(theBook.root); | |
try { | |
whatThis(); | |
} catch (Exception ex) { | |
Logger.getLogger(BinaryTree.class.getName()).log(Level.SEVERE, null, ex); | |
} | |
} | |
/* | |
Step 1: create a new method named whatThis | |
Step 2: inside the method, declare file as new File and initialized it with a file path | |
Step 3: declare sc as new Scanner and initialized using the file | |
Step 4: create a while loop to check if the scanner element | |
Step 5: inside the loop, create tks as new String tokenizer | |
Step 6: create a while loop to separate the string | |
*/ | |
public static void whatThis()throws Exception{ | |
File file = new File("D:\\MIX FILES\\Assorted Files\\DataStructure\\DataStructure2\\DataStucture\\src\\BinaryTree\\what_BinaryTree.txt"); | |
Scanner sc = new Scanner(file); | |
while (sc.hasNextLine()){ | |
StringTokenizer tks = new StringTokenizer(sc.nextLine(), ","); | |
while (tks.hasMoreTokens()){ | |
String problem = tks.nextToken(); | |
String implemen = tks.nextToken(); | |
String subject = tks.nextToken(); | |
String programmer = tks.nextToken(); | |
System.out.println("--------------------------------------"); | |
System.out.println("Problem Domain: " + problem); | |
System.out.println("Implementation: " +implemen); | |
System.out.println("Subject: " +subject); | |
System.out.println("Programmer: " +programmer); | |
} | |
} | |
} | |
} | |
class Book{ | |
int callNum; | |
String bookName; | |
Book leftChild; | |
Book rightChild; | |
Book(int callNum, String bookName){ | |
this.callNum = callNum; | |
this.bookName = bookName; | |
} | |
public String toString(){ | |
return bookName + " has a book id " + callNum; | |
} |
0 Comments