package ru.pravvich.tree.simpleTree;

import java.util.*;

public class SimpleTreeSet<E> implements Tree<E> {
    private Leaf<E> root;
    private List<E> list;
    private int size = 0;

    public SimpleTreeSet() {
        list = new LinkedList<>();
    }

    @Override
    public boolean add(E e) {
        if (size == 0) {
            return initRootLeaf(e);
        }

        Leaf<E> newNode = new Leaf<>(e);
        Leaf<E> lastNode = findLastLeaf(root, newNode);

        if (lastNode == null) {
            return false;
        }

        // храню элементы для итератора и get
        list.add(newNode.element);

        if (lastNode.compareTo(newNode) > 0) {
            lastNode.right = newNode;
            size++;
            return true;
        } else {
            lastNode.left = newNode;
            size++;
            return true;
        }
    }

    // ищу последний лист дерева
    private Leaf<E> findLastLeaf(
            final Leaf<E> oldLeaf,
            final Leaf<E> newLeaf
    ) {

        Leaf<E> lastLeaf = oldLeaf;
        int compare = oldLeaf.compareTo(newLeaf);

        if (compare > 0 && oldLeaf.right != null) {
            lastLeaf = findLastLeaf(oldLeaf.right, newLeaf);
            return lastLeaf;
        }

        if (compare < 0 && oldLeaf.left != null) {
            lastLeaf = findLastLeaf(oldLeaf.left, newLeaf);
            return lastLeaf;
        }

        if (compare == 0)
            return null;

        return lastLeaf;
    }


    private boolean initRootLeaf(final E e) {
        root = new Leaf<>(e);
        list.add(e);
        size++;
        return true;
    }

    @Override
    public List<E> get() {
        return list;
    }

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

    @Override
    public Iterator<E> iterator() {
        return list.iterator();
    }

    private class Leaf<E> implements Comparable<E> {
        private Leaf<E> right;
        private Leaf<E> left;
        private E element;

        private Leaf(E element) {
            this.element = element;
        }

        @Override
        public int compareTo(Object obj) {
            if (obj instanceof SimpleTreeSet.Leaf) {
                Leaf<E> node = (Leaf<E>) obj;
                return this.hashCode() - node.hashCode();
            }
            throw new ClassCastException();
        }

        @Override
        public int hashCode() {
            int hash = 31;
            hash = hash * 17 + element.hashCode();
            return hash;
        }
    }
}

@@ -528,32 +528,48 @@
 lse;%0A        %7D%0A%0A
+        size++;%0A
         // %D1%85%D1%80%D0%B0%D0%BD%D1%8E
@@ -598,16 +598,16 @@
 %D0%B0 %D0%B8 get%0A
-
         
@@ -719,36 +719,16 @@
 ewNode;%0A
-            size++;%0A
         
@@ -802,28 +802,8 @@
 de;%0A
-            size++;%0A
     
@@ -1460,17 +1460,16 @@
 %0A    %7D%0A%0A
-%0A
     priv
@@ -1654,20 +1654,868 @@
     
-return list;
+quickSort(list, 0, list.size() - 1);%0A        return new LinkedList%3C%3E(list); // %D1%87%D1%82%D0%BE%D0%B1%D1%8B %D1%81%D0%BD%D0%B0%D1%80%D1%83%D0%B6%D0%B8 %D0%BD%D0%B5 %D0%BF%D0%BE%D0%BB%D0%BE%D0%BC%D0%B0%D1%82%D1%8C %D0%B1%D1%8B%D0%BB%D0%BE%0A    %7D%0A%0A    private void quickSort(List%3CE%3E list, int left, int right) %7B%0A        if (right %3E left) %7B%0A            int i = left, j = right;%0A            E tmp;%0A%0A            int v = list.get(right).hashCode(); //pivot%0A%0A            do %7B%0A                while (list.get(i).hashCode() %3C v)%0A                    i++;%0A                while (list.get(j).hashCode() %3E v)%0A                    j--;%0A%0A                if (i %3C= j) %7B%0A                    tmp = list.get(i);%0A                    list.set(i, list.get(j));%0A                    list.set(j, tmp);%0A                    i++;%0A                    j--;%0A                %7D%0A            %7D while (i %3C= j);%0A%0A            if (left %3C j) quickSort(list, left, j);%0A            if (i %3C right) quickSort(list, i, right);%0A        %7D
 %0A   
@@ -2961,65 +2961,8 @@
 ) %7B%0A
-            if (obj instanceof SimpleTreeSet.Leaf) %7B%0A    
     
@@ -3003,36 +3003,32 @@
 bj;%0A            
-   
- 
 return this.hash
@@ -3057,66 +3057,8 @@
 ();%0A
-            %7D%0A            throw new ClassCastException();%0A
     

update SimpleTreeSet