crsandu icon

Untitled

crsandu | PRO | 02/05/19 02:24:42 PM UTC | 0 ⭐ | 327 👁️ | Never ⏰ | []
Java |

1.16 KB

|

None

|

0 👍

/

0 👎

 
public class TreeSet <Type extends Comparable>{
    private TreeSet rightChild = null; // initialily this is null
    private TreeSet leftChild = null; // initiali this is null
    
    private Type value = null; // no value assigned yet
    
    public TreeSet(Type valoare) {
        this.value = valoare;
    }
    
    public TreeSet getRight() {
        return this.rightChild;
    }
    
    public TreeSet getLeft() {
        return this.leftChild;
    }
    
    public Type getValue() {
        return this.value;
    }
    
    public void addNewNode(TreeSet value) {
        if(this.leftChild == null && this.rightChild == null) {
            // am ajuns la frunza, adaugam valoarea
            // like Potolea (<3)
            
            if(value.getValue().compareTo(this.getValue()) == -1) {
                // daca e mai mica
                this.leftChild = value;
            }
            else if(value.getValue().compareTo(this.getValue()) == 1) {
                // daca e mai mare
                this.rightChild = value;
            }
        }
        else {
            if(value.getValue().compareTo(this.getValue()) == -1) {
                // daca e mai mica
                this.getLeft().addNewNode(value);
            }
            else if(value.getValue().compareTo(this.getValue()) == 1) {
                // daca e mai mare
                this.getRight().addNewNode(value);
            }
        }
    }
}

Comments