dimanche 29 mars 2015

Binary Search Tree searching in java



I'm attempting to make a BST program that is capable of inserting a given number and then telling the user if the number is in the BST by saying either true or false. However, even if the number is inserted, it always registers as false. Would anyone be able to tell me where I am going wrong here? I can supply the rest of my class files if they are needed.



public class BinarySearchTree implements BST
{
private int n;
private Node r;
private Node l;

public void enter(int num)
{
Node node = new Node(num);

if (num < node.getData())
{
if (node.getL() != null)
{
insert(num);
}

else
{
node.setL(new Node(num));
}
}

else if (num > node.getData())
{
if (node.getR() != null)
{
insert(num);
}

else
{
node.setR(new Node(num));
}
}
}

public boolean search (int num)
{
if (num == this.n)
{
return true;
}

else if (num > this.n)
{
if (r == null)
{
return false;
}

else
{
return true;
}
}
else if (num < this.n)
{
if (l == null)
{
return false;
}
else
{
return true;
}
}
return false;
}
}



Aucun commentaire:

Enregistrer un commentaire