Question

So I want to go into my tree (which is assumed to have no duplicates and is branched correctly) and find the element that is given in the parameters. I have found that my method gives me a BinaryNode that resembles what I want (the root) in its fields, but is not actually the root. I have not overridden the equals method. Using the equals method, the test returns false when the returned object and the root are compared. I want to know why my variable, elementNode, does not reference (and hence change) the root to be null when it is set null.

Binary nodes are implemented with generics. This method is being called with the root as its starting point. Any help will be appreciated, thank you.

/**
 * Returns the node that the sought element resides within
 * 
 * @param element: The element being hunted
 * @param start: The start of the search (usually the root)
 * @return The node that the element resides in
 */
private BinaryNode<T> findElementNode(T element, BinaryNode<T> start) {


    if (start.getElement().equals(element)) {
        return start;
    }

    // the element is not in the collection
    if (start.getLeftChild() == null && start.getRightChild() == null) {
        return null;
    }

    int comparison = element.compareTo(start.getElement());

    if (comparison < 0) {
        return findElementNode(element, start.getLeftChild());
    } else {
        return findElementNode(element, start.getRightChild());
    }
}

EDIT FOR EXAMPLE: (this is for a removal method)

    if (!hasLeft && !hasRight) {

        System.out.println(elementNode + "," + root);
        elementNode = null;         
        System.out.println(elementNode + "," + root);

        return true;
    }

This outputs:

BinaryNode@68b0019f,BinaryNode@68b0019f and null,BinaryNode@68b0019f

EDIT FOR ANSWER: The reason that the returned element does not set the node it points to as null is because in Java, we can only point at, not edit, memory locations. So in order to make my node null, I will locate the parent and just set its left/right child to null.

Was it helpful?

Solution

Try with something like this.

private boolean findElementNode(T element, BinaryNode<T> start) {

    if (start == null) {
       return false;
    } else {
      if (element.getElement().equals(start.getElement())) {
         return true;
      } else {
         int comparison = element.compareTo(start.getElement());

         if (comparison < 0) {
            return findElementNode(element, start.getLeftChild());
         } else {
            return findElementNode(element, start.getRightChild());
         }          
     }
}

EDIT: this is actually what you want. Tell me if this works.

private BinaryNode<T> findElementNode(T element, BinaryNode<T> start) {
    if(start != null){
        if(start.getElement().equals(element)){
           return start;
        } else {
            BinaryNode<T> start = findElementNode(element, start.getLeftChild());
            if(start == null) {
                start = findElementNode(element, start.getRightChild());
            }
            return start;
         }
    } else {
        return null;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top