0

I'm trying to create this Tree in Java but am stuck and can't figure out how to continue. Here is the code I have so far:

public class BTNode<E>
{
private E data;
private BTNode<E> left;
private BTNode<E> right;

public BTNode(E newData, BTNode<E> newLeft, BTNode<E> newRight)
{
    setData(newData);
    setLeft(newLeft);
    setRight(newRight);
}

public E getData()
{
    return data;
}

public BTNode<E> getLeft()
{
    return left;
}

public BTNode<E> getRight()
{
    return right;
}

public void inorderPrint()
{
    if(left != null)
        left.inorderPrint();

    System.out.println(data);

    if(right != null)
        right.inorderPrint();
}

public void setData(E newData)
{
    data = newData;
}

public void setLeft(BTNode<E> newLeft)
{
    left = newLeft;
}

public void setRight(BTNode<E> newRight)
{
    right = newRight;
}

}

public class Tree<E extends Comparable<E>>
{
private BTNode<E> root;
private int manyNodes;

public Tree()
{

}

public void add(E element)
{
    BTNode<E> newLeft = null;
    BTNode<E> newRight = null;

    if(root == null)
        root = new BTNode<E>(element, newLeft, newRight);

    else
    {
        BTNode<E> cursor = new BTNode<E>(element, newLeft, newRight);
        cursor = root;
        boolean done = false;

        while(done = false)
        {
            if (element.compareTo(cursor.getData()) <= 0)
            {
                if(cursor.getLeft() == null)
                {
                    cursor.setLeft(element); //create a new node from left
                    done = true;
                }

                else
                    cursor = cursor.getLeft();
            }

            else
            {
                if(cursor.getRight() ==null)
                {
                    cursor.setRight(element); //create a new node from right
                    done = true;
                }
                else
                    cursor = cursor.getRight();
            }
        }
    }

}

public int size()
{
    return manyNodes;
}

public BTNode<E> getRoot()
{
    return root;
}

}

The problem is creating the left and right nodes, it's not letting me because the types are different. Also i'm not sure what to put in the Tree constructor.

1 Answer 1

1

The methods

public void setLeft(BTNode<E> newLeft) {
    left = newLeft;
}
public void setRight(BTNode<E> newRight) {
    right = newRight;
}

are expecting BTNode objects but you are calling them like

cursor.setLeft(element); 

where element is of type E and E is, as far as the compiler is concerned, a type that extends Comparable<E>. You have a type mismatch. Wrap your element in a BTNode object and pass that.

Sign up to request clarification or add additional context in comments.

4 Comments

You mean like this: cursor.setLeft((BTNode<E>)element);
@user No, that is called casting. I mean create a new BTNode object and give it the element as its data.
BTNode<E> left = new BTNode<E>(element, null, null); ? I'm still not following. I'm trying to create a new node for the left, not the data. sorry
@user2985986 There are many things wrong with your add method. First, the comparison operator is == not =. Second, you immediately re-assign the cursor variable that previously store the node you want to add.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.