1

So I have managed to make a BST in Java with using an array but a friend told me he has done it with out using an array. I know roughly how he has done it here is an example(I have left out constructors and getters and setters):

class TreeNode implements Comparable<TreeNode>
{
    private int value;
    private TreeNode leftChild;
    private TreeNode rightChild;
    private TreeNode parent;

As you can see you can just link the left and the right nodes but this got me thing to how the add method would work. So far I have just initialized a new object like this:

public void add(Comparable c)
{
    TreeNode node = new TreeNode(c);
}

But this leaves wondering how can I link to the previous object if I can't access it like it was in an array? and every time I go to link a object I end up adding a new one. I'm kinda confused on how to implement this method, how would you do it?

7
  • But you're adding it to an existing node, right? So left, right, and parent have values. Commented Nov 17, 2015 at 17:57
  • Why array is needed in the first place? Commented Nov 17, 2015 at 17:57
  • When you are adding to a TreeNode - where are you adding - left child, right child or parent? In the 'Comparable c' parameter passed in the add() - the corresponding variable should point to 'c'. Commented Nov 17, 2015 at 17:58
  • Read this tutorial algorithms.tutorialhorizon.com/… Commented Nov 17, 2015 at 18:06
  • Well I first have to add the root node and then after that I can add to existing nodes. Commented Nov 17, 2015 at 18:14

1 Answer 1

1

I'm pretty sure what you have to do is something akin to this:

class TreeNode<T extends Comparable<T>> {
    private T value;
    private TreeNode<T> left;
    private TreeNode<T> right;
    private TreeNode<T> parent;

    public TreeNode(TreeNode parent, T value) {
        this.parent = parent;
        this.value = value;
    }

    public TreeNode<T> add(T t) {
        if(t.compareTo(value) < 0) {
            if(left == null) {
                left = new TreeNode(this, t);
                return left;
            } else {
                return left.add(t);
            }
        } else {
            if(right == null) {
                right = new TreeNode(this, t);
                return right;
            } else {
                return right.add(t);
            }
        }
    }

    public boolean find(T t) {
        if(t.equals(value)) {
            return true;
        } else {
            if(t.compareTo(value) < 0) {
                if(left == null) {
                    return false;
                } else {
                    return left.find(t);
                }
            } else {
                if(right == null) {
                    return false;
                } else {
                    return right.find(t);
                }
            }
        }
    }
}

class Tree<T extends Comparable<T>> {
    private TreeNode<T> topElement;

    public TreeNode<T> add(T t) {
        if(topElement == null) {
            topElement = new TreeNode(null, t);
            return topElement;
        } else {
            return topElement.add(t);
        }
    }

    public boolean find(T t) {
        if(topElement == null) {
            return false;
        } else {
            return topElement.find(t);
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Maybe you should use t.compareTo(value) == 0 instead of t.equals(value) here.

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.