package ru.apetrov.MyTreeSet;

import java.util.List;

/**
 * Created by Andrey on 30.04.2017.
 */
public class MyTreeSet<E> implements SimpleTree<E> {

    private Leaf<E> root;

    private int size = 0;

    @Override
    public int getSize() {
        return size;
    }

    @Override
    public boolean addChild(E e) {
        boolean result = false;

        Leaf<E> oldRoot = null;
        Leaf<E> currRoot = this.root;

        while (currRoot != null) {
            oldRoot = currRoot;
            if (e.hashCode() < currRoot.item.hashCode()) {
                currRoot = currRoot.left;
            } else {
                currRoot = currRoot.right;
            }
        }

        if (oldRoot == null) {
            this.root = new Leaf<>(e, null);
            this.size++;
            result = true;
        } else if (e.hashCode() < oldRoot.item.hashCode()) {
            oldRoot.left = new Leaf<>(e, oldRoot.item);
            this.size++;
            result = true;
        } else {
            oldRoot.right = new Leaf<>(e, oldRoot.item);
            this.size++;
            result = true;
        }

        return result;
    }

    @Override
    public List<E> getChildren(E e) {
        return null;
    }

    @Override
    public String toString() {
        return "MyTreeSet{" +
                "root=" + root +
                '}';
    }

    private class Leaf<E> {

        private E item;
        private Leaf<E> left;
        private Leaf<E> right;
        private E parent;

        public Leaf(E item, E parent) {
            this.item = item;
            this.parent = parent;
        }

        @Override
        public String toString() {
            return "Leaf{" +
                    "item=" + item +
                    ", left=" + left +
                    ", right=" + right +
                    ", parent=" + parent +
                    '}';
        }
    }
}

@@ -367,19 +367,20 @@
 Leaf%3CE%3E 
-old
+curr
 Root = n
@@ -396,28 +396,27 @@
     Leaf%3CE%3E 
-curr
+old
 Root = this.
@@ -437,20 +437,19 @@
  while (
-curr
+old
 Root != 
@@ -472,22 +472,22 @@
     
-old
+curr
 Root = 
-curr
+old
 Root
@@ -519,20 +519,19 @@
 ode() %3C 
-curr
+old
 Root.ite
@@ -558,39 +558,37 @@
             
-curr
+old
 Root = 
-curr
+old
 Root.left;%0A 
@@ -627,23 +627,21 @@
     
-curr
+old
 Root = 
-curr
+old
 Root
@@ -685,19 +685,20 @@
     if (
-old
+curr
 Root == 
@@ -828,35 +828,36 @@
 (e.hashCode() %3C 
-old
+curr
 Root.item.hashCo
@@ -872,27 +872,28 @@
             
-old
+curr
 Root.left = 
@@ -898,35 +898,36 @@
 = new Leaf%3C%3E(e, 
-old
+curr
 Root.item);%0A    
@@ -999,27 +999,28 @@
             
-old
+curr
 Root.right =
@@ -1034,19 +1034,20 @@
 af%3C%3E(e, 
-old
+curr
 Root.ite

add MyTreeSet, method addChild().