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);
    }

    public boolean contains(List<Node<E>> list, Node<E> children) {
        for (int i = 0; i < list.size(); i++) {
            Node<E> node = list.get(i);
            if (node.value == children.value) {
                return true;
            }
        }
        return false;
    }

    @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);

            if (contains(list, children)) {
                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<>();
        Node next = new Node(null);

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

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

    @Override
    public Iterator<E> iterator() {
        return new treeIterator();
    }
}
@@ -131,16 +131,60 @@
 E%3E root;
+%0A    Queue%3CNode%3E queue = new LinkedList%3C%3E();
 %0A%0A    pu
@@ -258,493 +258,133 @@
     
-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    public boolean add(E parent, E child) %7B%0A        if (findBy(parent).isPresent()) %7B%0A%0A            Node%3CE%3E element = findBy(parent).get();%0A            List%3CNode%3CE%3E%3E list = element.leaves();
+@Override%0A    public boolean add(E parent, E child) %7B%0A        if (findBy(parent).isPresent() && !findBy(child).isPresent()) %7B
 %0A   
@@ -404,127 +404,39 @@
 %3CE%3E 
-childr
+elem
 en
+t
  = 
-new Node%3C%3E(child);%0A%0A            if (contains(list, children)) %7B%0A                return false;%0A            %7D%0A
+findBy(parent).get();
 %0A   
@@ -501,17 +501,16 @@
 n true;%0A
-%0A
         
@@ -1079,25 +1079,29 @@
     
-public class tree
+@Override%0A    public 
 Iter
@@ -1113,38 +1113,28 @@
 E%3E i
-mplements I
 terator
+()
  %7B%0A
-%0A
         
 Queu
@@ -1133,51 +1133,66 @@
     
-Q
+q
 ueue
-%3CNode%3E data = new LinkedList%3C%3E
+.offer(root);%0A        return new treeIterator
 ();%0A    
     
@@ -1191,39 +1191,65 @@
     
+%7D%0A%0A
     
-Node next = new Node(null);
+public class treeIterator%3CE%3E implements Iterator %7B
 %0A%0A  
@@ -1322,20 +1322,23 @@
 urn 
-next != null
+queue.isEmpty()
 ;%0A  
@@ -1411,136 +1411,66 @@
     
-data.offer(root);%0A            while (!data.isEmpty()) %7B%0A                next = data.poll();%0A                break;%0A            %7D
+Node child = queue.poll();%0A            queue.offer(child);
 %0A   
@@ -1485,20 +1485,27 @@
  return 
-next
+child.value
 ;%0A      
@@ -1512,103 +1512,14 @@
   %7D%0A
+%0A
     %7D%0A%0A
-    @Override%0A    public Iterator%3CE%3E iterator() %7B%0A        return new treeIterator();%0A    %7D
 %0A%7D

task #1711