package ru.job4j.tree;

import java.util.ArrayList;
import java.util.List;

/**
 * Class BinTree.
 *
 * @author Alexander Ivanov
 * @since 10.04.2017
 * @version 1.0
 * @param <E> type of elements.
 */
public class BinTree<E extends Comparable> {

    /**
     * Node-root.
     */
    private Node<E> root;

    /**
     * Put key to tree.
     * Put only unique keys.
     * @param key for adding to tree.
     */
    public void put(E key) {
        Node<E> nextNode = root;
        Node<E> prevNode = null;
        while (nextNode != null) {
            int comp = key.compareTo(nextNode.key);
            if (comp < 0) {
                prevNode = nextNode;
                nextNode = nextNode.left;
            } else if (comp > 0) {
                prevNode = nextNode;
                nextNode = nextNode.right;
            } else {
                return;
            }
        }
        Node<E> newNode = new Node<>(key);
        if (prevNode == null) {
            root = newNode;
        } else {
            if (key.compareTo(prevNode.key) < 0) {
                prevNode.left = newNode;
            } else {
                prevNode.right = newNode;
            }
        }
    }

    /**
     * Get all keys of tree.
     * @return list of keys.
     */
    public List<E> getAllKeys() {
        List<E> list = new ArrayList<E>();
        if (root != null) {
            list.add(root.key);
            getRecursiveList(root, list);
        }
        return list;
    }

    /**
     * Addition recursive method for getting all keys.
     * @param node parent.
     * @param list for adding.
     * @return list of keys.
     */
    private List<E> getRecursiveList(Node<E> node, List<E> list) {
        Node<E> rootNode = node;
        if (rootNode.left != null) {
            list.add(rootNode.left.key);
            getRecursiveList(rootNode.left, list);
        }
        if (rootNode.right != null) {
            list.add(rootNode.right.key);
            getRecursiveList(rootNode.right, list);
        }
        return list;
    }

    /**
     * Check tree for containing key.
     * @param key for check.
     * @return true if tree contains key.
     */
    public boolean contains(E key) {
        Node<E> node = root;
        while (node != null) {
            int comp = key.compareTo(node.key);
            if (comp == 0) {
                return true;
            } else {
                if (comp > 0) {
                    node = node.right;
                } else {
                    node = node.left;
                }
            }
        }
        return false;
    }

    /**
     * Node for building tree.
     * @param <E> type of key.
     */
    private class Node<E> {
        /**
         * Key for storage.
         */
        private E key;

        /**
         * Reference for left node.
         */
        private Node<E> left;

        /**
         * Reference for right node.
         */
        private Node<E> right;

        /**
         * Constructor for Node.
         * @param key for storage.
         */
        Node(E key) {
            this.key = key;
        }
    }

    /**
     * PSVM.
     * @param args of string.
     */
    public static void main(String[] args) {
        BinTree<Integer> tree = new BinTree<>();
        tree.put(8);
        tree.put(4);
        tree.put(7);
        tree.put(7);
        tree.put(25);
        System.out.println(tree.contains(25));
        List<Integer> list = tree.getAllKeys();
        for (Integer i : list) {
            System.out.println(i);
        }
    }
}

@@ -3127,446 +3127,7 @@
    %7D
-%0A%0A    /**%0A     * PSVM.%0A     * @param args of string.%0A     */%0A    public static void main(String%5B%5D args) %7B%0A        BinTree%3CInteger%3E tree = new BinTree%3C%3E();%0A        tree.put(8);%0A        tree.put(4);%0A        tree.put(7);%0A        tree.put(7);%0A        tree.put(25);%0A        System.out.println(tree.contains(25));%0A        List%3CInteger%3E list = tree.getAllKeys();%0A        for (Integer i : list) %7B%0A            System.out.println(i);%0A        %7D%0A    %7D
 %0A%7D%0A

Add pictures.