package ru.job4j;

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

/**
 * Task 5.6.1.
 * Create my Tree
 *
 * Created by Anton Vasilyuk on 24.06.2017.
 * @version 1.0
 */
public class Tree<E extends Comparable<E>> implements SimpleTree<E> {

    /**.
     * @size is amount element in tree
     */
    private int maxSize = 0;

    /**.
     * @cursorIter is cursor in the iterator
     */
    private int cursorIter = 0;

    /**.
     * @elements is container for iterator
     */
    private List<E> elements;

    /**.
     * @firstNode is first node
     */
    private Node<E> firstNode;

    /**.
     * Constructor for this class
     */
    public Tree() {
        elements = new ArrayList<E>() {
        };
    }

    /**.
     * Class Node is container for elements and link to other elements
     * @param <E>
     */
    class Node<E> {
        List<Node<E>> childen;
        E value;
        Node<E> parentNode;
        Node(E value, Node<E> node) {
            this.value = value;
            this.parentNode = node;
            childen = new ArrayList<>();
        }
    }

    /**.
     *
     * @param parent parent.
     * @param child child.
     * @return
     */
    @Override
    public boolean add(E parent, E child) {
        if (firstNode != null && parent == null) {throw new NullPointerException("Parent is null!");}
        if (child == null) {throw new NullPointerException("Child is null");}
        if (firstNode == null) {
            Node<E> node = new Node<>(child, null);
            firstNode = node;
            return true;
        } else {
            Node<E> node = searchParent(firstNode, parent);
            if (node != null) {
                node.childen.add(new Node<E>(child, node));
                return true;
            } else {return false;}
        }
    }

    /**.
     * Method for check on binary the tree
     * @return boolean result
     */
    public boolean isBinary() {
        if (maxSize <= 2) {return true;}
        else {return false;}
    }

    /**.
     * Recursive method for search parent element
     * @param node is node for checking
     * @param parent is parent
     * @return node for add element
     */
    private Node<E> searchParent(Node<E> node, E parent) {
        Node<E> result = null;
        if(node.value.equals(parent)) {
            result = node;
        } else if (node.childen.size() > 0) {
            for (Node<E> tempNode : node.childen) {
                result = searchParent(tempNode, parent);
            }
        } else {result = null;}
        return result;
    }

    /**.
     * Recursive method for fill List
     * @param node
     */
    private void fillList(Node<E> node) {
        if (node != null) {
            if(node.childen.size() > maxSize) {maxSize = node.childen.size();}
            if (!elements.contains(node.value)) {
                elements.add(node.value);
            }
            for (Node<E> tempNode : node.childen) {
                if (tempNode.value != null) {
                    fillList(tempNode);
                }
            }
        } else {throw new NullPointerException("Node is null!");}
    }

    /**.
     * Realisation Iterator
     * @return elements
     */
    @Override
    public Iterator<E> iterator() {
        Iterator<E> iterator = new Iterator<E>() {

            @Override
            public boolean hasNext() {
                if (cursorIter < elements.size()) {return true;}
                else {return false;}
            }

            @Override
            public E next() {
                fillList(firstNode);
                if (hasNext()) {
                    return elements.get(cursorIter++);
                } else {
                    throw new NoSuchElementException("No more elements!");
                }
            }
        };
        return iterator;
    }
}

@@ -311,94 +311,8 @@
  %7B%0A%0A
-    /**.%0A     * @size is amount element in tree%0A     */%0A    private int maxSize = 0;%0A%0A
     
@@ -353,32 +353,32 @@
 in the iterator%0A
+
      */%0A    priv
@@ -574,16 +574,53 @@
 tNode;%0A%0A
+    private boolean isBinary = true;%0A
     /**.
@@ -1967,20 +1967,25 @@
 if (
-maxSize %3C= 2
+firstNode != null
 ) %7Br
@@ -1994,41 +1994,103 @@
 urn 
-true;%7D%0A        else %7Breturn false
+checkIsBinary(firstNode);%7D%0A        else %7Bthrow new NoSuchElementException(%22This Tree is empty%22)
 ;%7D%0A 
@@ -2649,32 +2649,336 @@
  result;%0A    %7D%0A%0A
+    private boolean checkIsBinary(Node%3CE%3E node) %7B%0A        if(node.childen.size() %3E 2) %7BisBinary = false;%7D%0A        if(node.childen.size() %3E 0) %7B%0A            for (Node%3CE%3E tempNode : node.childen) %7B%0A                isBinary = checkIsBinary(tempNode);%0A            %7D%0A        %7D%0A        return isBinary;%0A    %7D%0A%0A
     /**.%0A     * 
@@ -3069,32 +3069,32 @@
 Node%3CE%3E node) %7B%0A
+
         if (node
@@ -3109,87 +3109,8 @@
 ) %7B%0A
-            if(node.childen.size() %3E maxSize) %7BmaxSize = node.childen.size();%7D%0A
     

add file for chapter_003_5.6.2