Мне нужно получить всех потомков узла дерева. Для этого я написал функцию, которая нуждается в вспомогательной функции. Когда я хочу получить результаты, я получаю следующие ошибки:
в java.base / java.util.ArrayList $ Itr.checkForComodification (неизвестный источник)
в java.base / java.util.ArrayList $ Itr.next (неизвестный источник)
У меня уже была эта ошибка, поэтому я создал разные списки для каждого метода: дочерние элементы, потомки, предки, элементы и позиции. Теперь у меня везде ошибки.
public List<Position<E>> descendants(Position<E> p) throws InvalidPositionException {
if(p == null || isEmpty())
throw new InvalidPositionException();
// remove all positions from list
descendantList.removeAll(descendantList);
return descendantsList(p);
}
public List<Position<E>> descendantsList(Position<E> p) {
if(isInternal(p)) {
// add child to list and check if the child has also children
for(Position<E> child : children(p)) {
descendantList.add(child);
// if child has also children
if(isInternal(child))
descendantsList(child);
}
}
return descendantList;
}
Функция детей выглядит так:
public List<Position<E>> children(Position<E> p) throws InvalidPositionException {
if(p == null || isEmpty())
throw new InvalidPositionException();
else {
BinaryTreeNode<E> node = (BinaryTreeNode<E>) p;
childrenList.removeAll(childrenList);
// add left child first
if(node.leftChild != null)
childrenList.add(node.leftChild);
if(node.rightChild != null)
childrenList.add(node.rightChild);
return childrenList;
}
}
Для этого я создал разные списки:
- ArrayList for children: childrenList
- ArrayList for descendants: descendantsList
Как я могу решить эту ошибку?
Редактировать: я заменил все removeAll на clear. Он работал для потомков этого метода, но в качестве примера у меня все еще есть та же ошибка для моего метода высоты:
public int height() throws EmptyTreeException {
if(isEmpty())
throw new EmptyTreeException();
else
return heightOf(root);
}
private int heightOf(Position<E> p) {
if(isExternal(p))
return 0;
else {
int h = 0;
for(Position<E> child : children(p)) {
h = Math.max(h,heightOf(child));
}
return h + 1;
}
}