package ru.job4j.simpleTree;
import java.util.*;

public class Tree<E extends Comparable<E>> implements SimpleTree<E> {

    Node<E> root;

    public Tree(E value) {
        this.root = new Node<>(value);
    }

    @Override
    public boolean add(E parent, E child) {
        if (findBy(parent).isPresent()) {

            Node<E> element = findBy(parent).get();
            List<Node<E>> list = element.leaves();
            Node<E> children = new Node<>(child);

            for (int i = 0; i < list.size(); i++) {
                Node<E> node = list.get(i);
                if (node.value == children.value) {
                    return false;
                }
            }
            element.add(new Node<>(child));
            return true;

        } else {
           return false;
        }
    }

    @Override
    public Optional<Node<E>> findBy(E value) {
        Optional<Node<E>> rsl = Optional.empty();
        Queue<Node<E>> data = new LinkedList<>();
        data.offer(this.root);
        while (!data.isEmpty()) {
            Node<E> el = data.poll();
            if (el.eqValue(value)) {
                rsl = Optional.of(el);
                break;
            }
            for (Node<E> child : el.leaves()) {
                data.offer(child);
            }
        }
        return rsl;
    }

    public class treeIterator<E> implements Iterator {

        Queue<Node> data = new LinkedList<>();
        List next = root.leaves();

        @Override
        public boolean hasNext() {
            return next != null;
        }

        @Override
        public Object next() {
            data.offer(root);
            Node node = new Node(null);
            while (!data.isEmpty()) {
                node = data.poll();
                break;
            }
            return node;
        }
    }

    @Override
    public Iterator<E> iterator() {
        return new treeIterator();
    }
}
@@ -198,32 +198,318 @@
 (value);%0A    %7D%0A%0A
+    public boolean contains(List%3CNode%3CE%3E%3E list, Node%3CE%3E children) %7B%0A        for (int i = 0; i %3C list.size(); i++) %7B%0A            Node%3CE%3E node = list.get(i);%0A            if (node.value == children.value) %7B%0A                return true;%0A            %7D%0A        %7D%0A        return false;%0A    %7D%0A%0A
     @Override%0A  
@@ -498,32 +498,32 @@
 %0A%0A    @Override%0A
-
     public boole
@@ -763,148 +763,40 @@
     
-for (int i = 0; i %3C list.size(); i++) %7B%0A                Node%3CE%3E node = list.get(i);%0A                if (node.value == children.value) %7B%0A    
+if (contains(list, children)) %7B%0A
     
@@ -825,26 +825,8 @@
 se;%0A
-                %7D%0A
     
@@ -827,32 +827,33 @@
 ;%0A            %7D%0A
+%0A
             elem
@@ -1591,20 +1591,20 @@
     
-List
+Node
  next = 
 root
@@ -1603,20 +1603,21 @@
 t = 
-root.leaves(
+new Node(null
 );%0A%0A
@@ -1796,48 +1796,8 @@
 t);%0A
-            Node node = new Node(null);%0A
     
@@ -1847,19 +1847,19 @@
        n
-ode
+ext
  = data.
@@ -1923,19 +1923,19 @@
 return n
-ode
+ext
 ;%0A      

task #1711