0
public class CustomTree extends AbstractList <String>implements Cloneable, Serializable {
    Entry<String> root;
    public CustomTree() {
        this.root = new Entry<>("0");
    }

    @Override
    public boolean add(String s) {

  }   

  static class Entry<T> implements Serializable{
        String elementName;
        boolean availableToAddLeftChildren, availableToAddRightChildren;

        Entry<T> parent, leftChild, rightChild;

        public Entry(String elementName) {
            this.elementName = elementName;
            this.availableToAddLeftChildren = true;
            this.availableToAddRightChildren = true;
        }

        public boolean isAvailableToAddChildren(){
            return availableToAddLeftChildren||availableToAddRightChildren;
        }
    }
}

1 ответ 1

0

на самом деле это не так сложно. делаем упрощенный двусвязный список. нам придется добавить метод get и foreach, чтобы иметь доступ к тому, что положим в наш самодельный двусвязный список. выглядит это примерно так:

import java.util.AbstractList;
import java.util.function.Consumer;

public class CustomTree <E> extends AbstractList<E> {

    private int size = 0;

    private Entry<E> first;

    private Entry<E> last;

    @Override
    public boolean add(E e) {
        Entry entry;
        //first element in collection        
        if (first == null) {
            entry = new Entry(null, e, null);
            first = entry;
        } 
        else {
            entry = new Entry(last, e, null);
            last.next = entry;
        }
        last = entry;
        size++;
        return true;
    }

    @Override
    public void forEach(Consumer action) {        
        Entry<E> entry = first;
        do{
            action.accept(entry.item);
            entry = entry.next;
        }while (entry!=null);
    }

    @Override
    public E get(int index) {
        if (index<0 || index>=size) throw new RuntimeException("Out of range");
        Entry<E> entry = first;
        for (int i = 0; i < index; i++) entry = entry.next;
        return entry.item;
    }

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

    private static class Entry<E> {
        E item;
        Entry<E> next;
        Entry<E> prev;

        Entry(Entry<E> prev, E element, Entry<E> next) {
            this.item = element;
            this.next = next;
            this.prev = prev;
        }
    }

}

ну и метод для того, чтобы продемонстрировать работу:

public static void main(String[] args) {
    CustomTree<Integer> customTree = new CustomTree<>();
    customTree.add(1);
    customTree.add(2);
    customTree.add(3);
    customTree.add(4);

    customTree.forEach(System.out::println);

    System.out.println("**********************************************");

    System.out.println(customTree.get(0));
    System.out.println(customTree.get(1));
    System.out.println(customTree.get(2));
    System.out.println(customTree.get(3));
}

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.