package trampoline;

import java.util.EmptyStackException;
import java.util.Stack;

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 () {
        Stack<Integer> stack = new Stack<Integer>();
        Node cur = this;

        while (cur != null) {
            while (cur.hasLhs()) {
                stack.push(cur.getValue());
                cur = cur.innerPrintTree();
            }
            try {
                while (true) {
                    Integer val = stack.pop();
                    System.out.println("Val:" + val);
                }
            } catch (EmptyStackException e) {}
            while (cur.hasRhs()) {
                stack.push(cur.getValue());
                cur = cur.innerPrintTree();
            }
            try {
                while (true) {
                    Integer val = stack.pop();
                    System.out.println("Val:" + val);
                }
            } catch (EmptyStackException e) {}

            cur = cur.innerPrintTree();
        }

    }

    public 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;
    }
}

@@ -18,71 +18,8 @@
 e;%0A%0A
-import java.util.EmptyStackException;%0Aimport java.util.Stack;%0A%0A
 publ
@@ -503,873 +503,8 @@
 ) %7B%0A
-        Stack%3CInteger%3E stack = new Stack%3CInteger%3E();%0A        Node cur = this;%0A%0A        while (cur != null) %7B%0A            while (cur.hasLhs()) %7B%0A                stack.push(cur.getValue());%0A                cur = cur.innerPrintTree();%0A            %7D%0A            try %7B%0A                while (true) %7B%0A                    Integer val = stack.pop();%0A                    System.out.println(%22Val:%22 + val);%0A                %7D%0A            %7D catch (EmptyStackException e) %7B%7D%0A            while (cur.hasRhs()) %7B%0A                stack.push(cur.getValue());%0A                cur = cur.innerPrintTree();%0A            %7D%0A            try %7B%0A                while (true) %7B%0A                    Integer val = stack.pop();%0A                    System.out.println(%22Val:%22 + val);%0A                %7D%0A            %7D catch (EmptyStackException e) %7B%7D%0A%0A            cur = cur.innerPrintTree();%0A        %7D%0A%0A
     

remove dead code