0

For some reason the add(value) function doesn't want to work. I should be able to use Node and TreeNode to create a child. This isn't a balanced tree. I tried both Node and NodeTree and to make a variable with the node and adding it in with no success

public abstract class TreeNode implements Comparable<TreeNode>{
protected int value;
protected TreeNode left;
protected TreeNode right;

public abstract int getValue();
public abstract int getSize();
public abstract TreeNode getLeft();
public abstract TreeNode getRight();

public void add(int value){
    if (value >= this.value){
        if (this.right == null){
            this.right = new Node(value); //trying to put a node in the "right" 
        }else{
            right.add(value);
        }
    }else if(value < this.value){
        if (this.left == null){
            this.left = new Node(value); //trying to do the same thing here
        }else{
            left.add(value);
        }
    }
    }

    public String toString() {
        return (left.toString() + ", " +Integer.toString(this.value) + ", " + right.toString());
    }

public int CompareTo(TreeNode obj){
    if(this.value > obj.value){
        return 1;
    }else if(this.value < value){
        return -1;
    }else{
        return 0;
    }
}

//public void remove(int value) throws NotFoundException{

//}
}
  • What values are you inputting? What would you expect to happen and what is actually happening? Where is the value assigned, in the contstuctor? – Steve May 01 '13 at 22:26
  • 1
    You say you've tried both `Node` and `NodeTree`. Have you tried `TreeNode`? – Lone nebula May 01 '13 at 22:28

2 Answers2

0

You have numerous things wrong with that code. First you aren't override the compareTo method. You need to change "CompareTo" to "compareTo".

Second, I can't tell if you are trying to make a TreeNode or a Node. Does Node extend TreeNode?

Third, you have specified TreeNode as an abstract class, but you are using it as if it was a normal class, even making the children as class Node().

Fourth and Fifth. These are minor, but your add function has "if (value >= this.value){} else if (value < this.value), which can be change to just an else. You also are sometimes using this.variable and sometimes just variable. You should really look up what these mean. For example in your "CompareTo" method, there is an error where you say "}else if(this.value < value){". That is checking the same variable against itself.

Fix those things and things will run better. It's hard to tell what the issue is when you didnt post the Node class, and there are so many minor bugs everywhere.

greedybuddha
  • 7,488
  • 3
  • 36
  • 50
0

A starting place is provided here: http://cs.uni.edu/~holmesm/docs/Session40.pdf

Basically, your code should be in a new class (for example Node) Your add method is close. In your NullNode class set the toString() method to return the empty string (return "";) and then in the add method change this.right == null to right.toString().equals (""). In the compareTo(...) method, read what was suggested above and change obj.value to obj.getValue().

Jens Erat
  • 37,523
  • 16
  • 80
  • 96
Aryt
  • 1