I am trying to implement a tree using an array and I have all my code but I am receiving the following error, any ideas how to fix this? Also if you can find any other way to implement this without all the casting I'm doing that would be great.
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.RangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at assign2Trees.ArrayTreeNode.<init>(ArrayTreeNode.java:17)
at assign2Trees.ArrayTree.<init>(ArrayTree.java:21)
at assign2Trees.ArrayTree.main(ArrayTree.java:110)
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
public class ArrayTree<E> implements Tree<E>{
protected ArrayTreeNode<E>[] tree;
protected E element;
public static final int CAPACITY = 1024;
protected List<ArrayTreeNode<E>> children;
protected ArrayTreeNode<E> parent;
protected ArrayTreeNode<E> root;
public ArrayTree(E e)
{
tree = new ArrayTreeNode[CAPACITY];
for(int i = 0; i<CAPACITY; i++)
tree[i] = new ArrayTreeNode<E>();
root = tree[0];
root.setChildren(this.children);
root.setElement(e);
root.setParent(null);
}
public ArrayTree(E e, int size)
{
tree = new ArrayTreeNode[size];
for(int i = 0; i<size; i++)
tree[i] = new ArrayTreeNode<E>();
root = tree[0];
root.setElement(e);
root.setChildren(this.children);
root.setParent(null);
}
public void delete(E node) throws RootNonDeletableException
{
tree[Arrays.asList(tree).indexOf(node)] = null;
parent(tree[Arrays.asList(tree).indexOf(node)]).addChild(tree[Arrays.asList(tree).indexOf(node)]);
}
public boolean isEmpty()
{
if (tree[0] == null)
return true;
else
return false;
}
public Iterator<E> iterator()
{
return new Iterator<E>()
{
private int index = 0;
public boolean hasNext()
{
return index < ArrayTree.this.size();
}
public E next() throws NoSuchElementException
{
if (!hasNext())
{
return null;
}
return (E) ArrayTree.this.tree[index++];
}
public void remove()
{
}
};
}
public E replace(E oldE, E newE)
{
tree[Arrays.asList(tree).indexOf(oldE)] = (ArrayTreeNode<E>) newE;
return newE;
}
public E root() throws EmptyTreeException
{
if (isEmpty())
throw new EmptyTreeException();
else
return (E) tree[0];
}
public int size()
{
int count = 0;
for (int i = 0; i < tree.length; i++)
count = count+1;
return count;
}
public Iterable<E> children(E node)
{
return (Iterable<E>) children;
}
public ArrayTreeNode<E> parent(ArrayTreeNode<E> node)
{
return parent;
}
public E parent(E node)
{
return (E) parent;
}
public static void main(String[] args)
{
ArrayTree<String> tree = new ArrayTree<String>("Eric");
System.out.println(tree.size());
tree.isEmpty();
}
}
import java.util.ArrayList;
import java.util.List;
public class ArrayTreeNode<E> {
protected E element;
protected List<ArrayTreeNode<E>> children;
protected ArrayTreeNode<E> parent;
public ArrayTreeNode()
{
children = new ArrayList<ArrayTreeNode<E>>();
element = null;
parent = children.get(0);
}
public ArrayTreeNode(E e)
{
children = new ArrayList<ArrayTreeNode<E>>();
parent = children.get(0);
element = e;
this.setElement(e);
}
public void setElement(E e)
{
this.element = e;
}
public void setChildren(List<ArrayTreeNode<E>> kids)
{
this.children = kids;
}
public void addChild(ArrayTreeNode<E> child)
{
children.add(child);
}
public void deleteChildren()
{
this.children = new ArrayList<ArrayTreeNode<E>>();
}
public List<ArrayTreeNode<E>> getChildren()
{
return this.children;
}
public void setParent(ArrayTreeNode<E> e)
{
parent = e;
}
}