package ru.agolovin;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;

/**
 * Tree.
 *
 * @param <E> E
 *            Created by istolbov on 09.05.2017.
 */
public class Tree<E extends Comparable<E>> implements SimpleTree<E> {

    private int nodesIndex;

    private List<Node<E>> nodes;

    private Node<E> first;

    Tree() {
        nodes = new ArrayList<>();
    }


    @Override
    public boolean add(E parent, E child) {
        boolean result = false;
        if (this.first == null) {
            this.first = new Node<>(parent);
            this.first.children.add(new Node<>(child));
            result = true;
        } else if (exist(this.first, parent, child)) {
            result = true;
        }
        return result;
    }


    private boolean exist(Node<E> node, E parent, E child) {
        boolean result = false;
        if (!node.children.isEmpty()) {
            for (Node<E> x : node.children) {
                exist(x, parent, child);
            }
            result = true;
        } else if (node.value.compareTo(parent) == 0) {
            node.children.add(new Node<>(child));
            result = true;

        }
        return result;
    }


    @Override
    public Iterator<E> iterator() throws NoSuchElementException {
//        nodesIndex = 0;
//        fillRoot(this.first);
        return new Iterator<E>() {

            @Override
            public boolean hasNext() {
                return nodesIndex < nodes.size();
            }

            @Override
            public E next() {

                if (nodesIndex < nodes.size()) {
                    if (!nodes.get(nodesIndex).children.isEmpty()) {
                        nodes.addAll(nodes.get(nodesIndex).children);
                    }
                    return nodes.get(nodesIndex++).value;
                } else {
                    throw new NoSuchElementException();
                }
            }
        };
    }

//    /**
//     * fillRoot.
//     *
//     * @param node node
//     */
//
//    private void fillRoot(Node<E> node) {
//        if (nodesIndex == 0 && node != null) {
//            nodes.clear();
//            nodes.add(node);
//        }
//    }

    /**
     * Node.
     *
     * @param <E> E
     */
    class Node<E> {
        /**
         * children.
         */
        List<Node<E>> children = new ArrayList<>();
        /**
         * value.
         */
        E value;

        /**
         * constructor.
         *
         * @param value value
         */
        Node(E value) {
            this.value = value;
        }
    }
}
@@ -146,81 +146,74 @@
 %0A * 
-Tree.%0A *%0A * @param %3CE%3E E%0A *            Created by istolbov on 09.05.2017.
+@author agolovin (agolovin@list.ru)%0A * @version $Id$%0A * @since 0.1
 %0A */
@@ -1322,18 +1322,16 @@
 ption %7B%0A
-//
         
@@ -1346,18 +1346,16 @@
 ex = 0;%0A
-//
         
@@ -1994,87 +1994,8 @@
  %7D%0A%0A
-//    /**%0A//     * fillRoot.%0A//     *%0A//     * @param node node%0A//     */%0A//%0A//
     
@@ -2028,26 +2028,24 @@
 %3CE%3E node) %7B%0A
-//
         if (
@@ -2079,18 +2079,16 @@
 null) %7B%0A
-//
         
@@ -2106,18 +2106,16 @@
 lear();%0A
-//
         
@@ -2135,18 +2135,16 @@
 (node);%0A
-//
         
@@ -2149,338 +2149,107 @@
   %7D%0A
-//
     %7D%0A%0A
-    /**%0A     * Node.%0A     *%0A     * @param %3CE%3E E%0A     */%0A    class Node%3CE%3E %7B%0A        /**%0A         * children.%0A         */%0A        List%3CNode%3CE%3E%3E children = new ArrayList%3C%3E();%0A        /**%0A         * value.%0A         */%0A        E value;%0A%0A        /**%0A         * constructor.%0A         *%0A         * @param value value%0A         */
+%0A    class Node%3CE%3E %7B%0A%0A        List%3CNode%3CE%3E%3E children = new ArrayList%3C%3E();%0A%0A        E value;%0A
 %0A   

add task1 in part6 chapter5 package2