package trampoline;

import java.util.ArrayList;

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

    private Integer value;

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

    private class SideAwareNode extends Node {
        final public String side;
        public SideAwareNode(Integer value, String side) {
            super(value);
            this.side = side;
        }
        public boolean isLhs () {
            return this.side.equals("lhs");
        }
        public boolean isRhs () {
            return !isLhs();
        }
    }

    public void printTree () {
        SideAwareNode next = innerPrintTree();
        ArrayList<Integer> printList = new ArrayList<Integer>();
        while (next != null) {
            if (next.isLhs()) {
                if (next.getValue() != null) {
                    System.out.println(next.getValue());
                }
            } else {
                if (next.getValue() != null) {
                    System.out.println(next.getValue());
                }
            }
            next = next.innerPrintTree();
            /*
            while (next.isLhs()) {
                if (next.getValue() != null) {
                    printList.add(next.getValue());
                }
                next = next.innerPrintTree();
                if (next == null) {
                    break;
                }
            }
            if (next == null) {
                break;
            }
            while (next.isRhs()) {
                if (next.getValue() != null) {
                    printList.add(next.getValue());
                }
                next = next.innerPrintTree();
                if (next == null) {
                    break;
                }
            }
            */
        }

        for (Integer val: printList) {
            System.out.println(val);
        }
    }

    public SideAwareNode innerPrintTree () {
        if (this.lhs != null) {
            return new SideAwareNode(lhs.getValue(), "lhs");
        }

        if (this.rhs != null) {
            return new SideAwareNode(rhs.getValue(), "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;
    }

    public void setValue(Integer value) {
        this.value = value;
    }
}

@@ -18,37 +18,8 @@
 e;%0A%0A
-import java.util.ArrayList;%0A%0A
 publ
@@ -183,218 +183,8 @@
  %7D%0A%0A
-    private class SideAwareNode extends Node %7B%0A        final public String side;%0A        public SideAwareNode(Integer value, String side) %7B%0A            super(value);%0A            this.side = side;%0A        %7D%0A    
     
@@ -198,17 +198,18 @@
 boolean 
-i
+ha
 sLhs () 
@@ -202,36 +202,32 @@
 ean hasLhs () %7B%0A
-    
         return t
@@ -229,1431 +229,125 @@
 urn 
-this.side.equals(%22lhs%22);%0A        %7D%0A        public boolean isRhs () %7B%0A            return !isLhs();%0A        %7D%0A    %7D%0A%0A    public void printTree () %7B%0A        SideAwareNode next = innerPrintTree();%0A        ArrayList%3CInteger%3E printList = new ArrayList%3CInteger%3E();%0A        while (next != null) %7B%0A            if (next.isLhs()) %7B%0A                if (next.getValue() != null) %7B%0A                    System.out.println(next.getValue());%0A                %7D%0A            %7D else %7B%0A                if (next.getValue() != null) %7B%0A                    System.out.println(next.getValue());%0A                %7D%0A            %7D%0A            next = next.innerPrintTree();%0A            /*%0A            while (next.isLhs()) %7B%0A                if (next.getValue() != null) %7B%0A                    printList.add(next.getValue());%0A                %7D%0A                next = next.innerPrintTree();%0A                if (next == null) %7B%0A                    break;%0A                %7D%0A            %7D%0A            if (next == null) %7B%0A                break;%0A            %7D%0A            while (next.isRhs()) %7B%0A                if (next.getValue() != null) %7B%0A                    printList.add(next.getValue());%0A                %7D%0A                next = next.innerPrintTree();%0A                if (next == null) %7B%0A                    break;%0A                %7D%0A            %7D%0A            */%0A        %7D%0A%0A        for (Integer val: printList) %7B%0A            System.out.println(val);%0A        %7D
+lhs != null;%0A    %7D%0A%0A    public boolean hasRhs () %7B%0A        return rhs != null;%0A    %7D%0A%0A    public void printTree () %7B%0A
 %0A   
@@ -361,25 +361,16 @@
  public 
-SideAware
 Node inn
@@ -441,48 +441,11 @@
 urn 
-new SideAwareNode(lhs.getValue(), %22lhs%22)
+lhs
 ;%0A  
@@ -508,48 +508,11 @@
 urn 
-new SideAwareNode(rhs.getValue(), %22rhs%22)
+rhs
 ;%0A  

remove another false start