Hi ,
The following code is meant for creating a binary search tree. But however because of the language semantics and the programmers oversight there are logical errors in the code. Try to find the errors if you can.. It takes good understanding of Java to find out the logical errors. All the very best ...
class MyBinarySearchTreeNode {
Integer value;
MyBinarySearchTreeNode left;
MyBinarySearchTreeNode right;
public MyBinarySearchTreeNode() {
// TODO Auto-generated constructor stub
value=null;
left=null;
right=null;
}
public MyBinarySearchTreeNode(Integer value) {
// TODO Auto-generated constructor stub
this.value=value;
left=null;
right=null;
}
}
******************************Another class in the same file **************************
public class MyBinarySearchTree{
MyBinarySearchTreeNode rootNode;
public MyBinarySearchTree(){
rootNode=null;
}
public void insertNode(MyBinarySearchTreeNode node){
/* check if root node is null
*
* if null then its the first node to be inserted .
*
* So make it the root the node.
*
* */
if(rootNode==null){
rootNode=node;
rootNode.left=null;
rootNode.right=null;
MyBinarySearchTreeNode rootNode;
public MyBinarySearchTree(){
rootNode=null;
}
public void insertNode(MyBinarySearchTreeNode node){
/* check if root node is null
*
* if null then its the first node to be inserted .
*
* So make it the root the node.
*
* */
if(rootNode==null){
rootNode=node;
rootNode.left=null;
rootNode.right=null;
return;
}
/*
* if root node is not null then we need to traverse the
*
* tree to find the appropriate position.
*
*/
else {
MyBinarySearchTreeNode currNode;
MyBinarySearchTreeNode nextNode;
currNode=rootNode;
nextNode=null;
while(nextNode==null){
if(node.value<currNode.value){
if(currNode.left!=null){
currNode=currNode.left;
}
else{
nextNode=currNode.left;
}
}
else if(node.value>currNode.value){
if(currNode.right!=null){
currNode=currNode.right;
}
else{
nextNode=currNode.right;
}
}
}
nextNode=node;
}
}
}
/*
* if root node is not null then we need to traverse the
*
* tree to find the appropriate position.
*
*/
else {
MyBinarySearchTreeNode currNode;
MyBinarySearchTreeNode nextNode;
currNode=rootNode;
nextNode=null;
while(nextNode==null){
if(node.value<currNode.value){
if(currNode.left!=null){
currNode=currNode.left;
}
else{
nextNode=currNode.left;
}
}
else if(node.value>currNode.value){
if(currNode.right!=null){
currNode=currNode.right;
}
else{
nextNode=currNode.right;
}
}
}
nextNode=node;
}
}
public static void main(String args[]){
MyBinarySearchTreeNode node=new MyBinarySearchTreeNode(11);
MyBinarySearchTree btree=new MyBinarySearchTree();
btree.insertNode(node);
for(int i=0;i<10;i++){
System.out.println("\n Enter value for next node");
DataInputStream din=new DataInputStream(System.in);
int val=0;
try{
val=Integer.parseInt(din.readLine());
}
catch(Exception e){
System.out.println("Exception"+e.getMessage());
}
node.value=val;
node.left=null;
node.right=null;
btree.insertNode(node);
}
}
}
MyBinarySearchTreeNode node=new MyBinarySearchTreeNode(11);
MyBinarySearchTree btree=new MyBinarySearchTree();
btree.insertNode(node);
for(int i=0;i<10;i++){
System.out.println("\n Enter value for next node");
DataInputStream din=new DataInputStream(System.in);
int val=0;
try{
val=Integer.parseInt(din.readLine());
}
catch(Exception e){
System.out.println("Exception"+e.getMessage());
}
node.value=val;
node.left=null;
node.right=null;
btree.insertNode(node);
}
}
}
For clarity below is the description.
MYBinarySearchTreeNode is a class used for representing Nodes.
MyBinarySearchTree class is used for implementing the logic.
Furhter mail method is also written in Tree class.
Finally if you have identified the logical error(s) post it here in the comments section.