package trampoline;

public class Node {
    private Node lhs;
    private Node rhs;

    private Integer value;

    public Node(Integer value)
    {
        this.value = value;
    }

    /**
     *
     * @return true iff there is a left hand side node
     */
    public boolean hasLhs () {
        return lhs != null;
    }

    /**
     *
     * @return true iff there is a right hand side node
     */
    public boolean hasRhs () {
        return rhs != null;
    }

    public void printTree () {
       innerPrintTree();
    }

    private Node innerPrintTree () {
        if (this.lhs != null) {
            return lhs;
        }

        if (this.rhs != null) {
            return rhs;
        }
        return null;
    }

    /**
     * find a node, returns null if not found
     * @param targetValue
     * @return
     */
    public Node find (Integer targetValue) {
        Node found = this;
        Node lastFound = found;
        while (found != null) {
            found = found.innerFind(targetValue);
            lastFound = found;
            if (lastFound == null) {
                break;
            }
            if (lastFound.getValue().equals(targetValue)) {
                break;
            }
        }

        return lastFound;
    }

    /**
     * returns Node with given value, if not found returns null
     * @param targetValue
     * @return
     */
    private Node innerFind (Integer targetValue) {
        if (this.getValue().equals(targetValue)) {
            return this;
        }

        if (this.getValue() > targetValue) {
            if (rhs == null) {
                return null;
            }
            return rhs;
        } else {
            if (lhs == null) {
                return null;
            }
            return lhs;
        }
    }

    /**
     * adds a node to the tree
     *
     * @param child
     */
    public void add (Node child) {
        Node tempParent;
        Node lastParent =  this;
        Node parent = innerAdd(child);
        while (parent != lastParent) {
            tempParent = parent;
            parent = tempParent.innerAdd(child);
            lastParent = tempParent;
        }
    }

    private Node innerAdd (Node n) {
        if (n.getValue() > getValue()) {
            if (lhs == null) {
                lhs = n;
            } else {
                return lhs;
            }
        } else {
            if (rhs == null) {
                rhs = n;
            } else {
                return rhs;
            }
        }
        return this;
    }

    public Integer getValue() {
        return value;
    }
}

@@ -78,17 +78,70 @@
 de rhs;%0A
+    private boolean isLhs;%0A    private boolean isRhs;
 %0A
-
     priv
@@ -197,16 +197,16 @@
 )%0A    %7B%0A
+
         
@@ -236,297 +236,8 @@
  %7D%0A%0A
-    /**%0A     *%0A     * @return true iff there is a left hand side node%0A     */%0A    public boolean hasLhs () %7B%0A        return lhs != null;%0A    %7D%0A%0A    /**%0A     *%0A     * @return true iff there is a right hand side node%0A     */%0A    public boolean hasRhs () %7B%0A        return rhs != null;%0A    %7D%0A%0A
     
@@ -2373,11 +2373,566 @@
 e;%0A    %7D
+%0A%0A    public boolean isLhs() %7B%0A        return isLhs;%0A    %7D%0A%0A    public void setLhs(boolean isLhs) %7B%0A        this.isLhs = isLhs;%0A    %7D%0A%0A    public boolean isRhs() %7B%0A        return isRhs;%0A    %7D%0A%0A    public void setRhs(boolean isRhs) %7B%0A        this.isRhs = isRhs;%0A    %7D%0A%0A    /**%0A     *%0A     * @return true iff there is a left hand side node%0A     */%0A    public boolean hasLhs () %7B%0A        return lhs != null;%0A    %7D%0A%0A    /**%0A     *%0A     * @return true iff there is a right hand side node%0A     */%0A    public boolean hasRhs () %7B%0A        return rhs != null;%0A    %7D
 %0A%7D%0A

is/has lhs/rhs