bst

I have a list containing 52431 elements, and another list with the same elements. When I create my BST, the tree can be built. But when I try to search for the number 4, I get a problem. The issue is that when the root becomes 2, it only gets a left leaf (2 -> left). But when I look at the path 5 -> 2 -> 4 on the right side, the 4 does exist, but not when 2 becomes the root.
public T searchElement(T elementToFind)
{
return searchingE(root, elementToFind);
}

private T searchingE(TreeNode<T> current, T elementToFind)
{
else if(current.right != null)
{
String mainRight = current.right.element.toString();
String boring = " ";
}
}

if(current.element.compareTo(elementToFind) == 0)
{
return current.element;
}
else
{
if(current.element.compareTo(elementToFind) > 0)
{
return searchingE(current.left, elementToFind);
}
else
{
return searchingE(current.right, elementToFind);
}
}
}
public T searchElement(T elementToFind)
{
return searchingE(root, elementToFind);
}

private T searchingE(TreeNode<T> current, T elementToFind)
{
else if(current.right != null)
{
String mainRight = current.right.element.toString();
String boring = " ";
}
}

if(current.element.compareTo(elementToFind) == 0)
{
return current.element;
}
else
{
if(current.element.compareTo(elementToFind) > 0)
{
return searchingE(current.left, elementToFind);
}
else
{
return searchingE(current.right, elementToFind);
}
}
}
4 Replies
JavaBot
JavaBot5d ago
This post has been reserved for your question.
Hey @mueoc! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
JavaBot
JavaBot5d ago
Please format your code to make it more readable. For java, it should look like this:
​`​`​`​java
public void foo() {

}
​`​`​`​
​`​`​`​java
public void foo() {

}
​`​`​`​
mueoc
mueocOP5d ago
the adding part
java
public void addElement(T newElement)
{
TreeNode<T> current = root;

if(current == null)
{
root = new TreeNode<>(newElement, null, null);
size++;
}
else
{
if(current.element.compareTo(newElement) < 0)
{
if(current.right == null)
{
root.right = new TreeNode<>(newElement, null, null);
size++;
}
else
addingE(root, newElement);
}
else
{
if(current.left == null)
{
root.left = new TreeNode<>(newElement, null, null);
size++;
}
else
addingE(root, newElement);
}
}
}

private void addingE(TreeNode<T> current, T newElement)
{

if(current == null)
{
root = new TreeNode<>(newElement, null, null);
size++;
}

if(current.element.compareTo(newElement) > 0)
{
if(current.left == null)
{
current.left = new TreeNode<>(newElement, null, null);
size++;
}
else
{
addingE(current.left, newElement);
}
}
else
{
if(current.right == null)
{
current.right = new TreeNode<>(newElement, null, null);
size++;
}
else
{
addingE(current.right, newElement);
}
}
}
java
public void addElement(T newElement)
{
TreeNode<T> current = root;

if(current == null)
{
root = new TreeNode<>(newElement, null, null);
size++;
}
else
{
if(current.element.compareTo(newElement) < 0)
{
if(current.right == null)
{
root.right = new TreeNode<>(newElement, null, null);
size++;
}
else
addingE(root, newElement);
}
else
{
if(current.left == null)
{
root.left = new TreeNode<>(newElement, null, null);
size++;
}
else
addingE(root, newElement);
}
}
}

private void addingE(TreeNode<T> current, T newElement)
{

if(current == null)
{
root = new TreeNode<>(newElement, null, null);
size++;
}

if(current.element.compareTo(newElement) > 0)
{
if(current.left == null)
{
current.left = new TreeNode<>(newElement, null, null);
size++;
}
else
{
addingE(current.left, newElement);
}
}
else
{
if(current.right == null)
{
current.right = new TreeNode<>(newElement, null, null);
size++;
}
else
{
addingE(current.right, newElement);
}
}
}
!solved
JavaBot
JavaBot5d ago
Post Closed
This post has been closed by <@423161233760452630>.

Did you find this page helpful?