Files
unisa_strutture_dati_2013_2014/tree/LinkedTree.java

220 lines
6.1 KiB
Java

package tree;
import exceptions.*;
import position.NodePositionList;
import position.Position;
import position.PositionList;
import java.util.Iterator;
/**
* Created with xgiovio.macbookair.
* User: xgiovio
* Date: 07/04/14
* Time: 15:45
*/
public class LinkedTree <E> implements Tree<E> {
protected TreePosition<E> root = null;
protected int size = 0;
public int size() {
return size;
}
public boolean isEmpty() {
return (size == 0);
}
public Iterable<Position<E>> positions() {
PositionList<Position<E>> positions = new NodePositionList<Position<E>>();
if(size != 0)
preorderPositions(root(), positions);
return positions;
}
public E replace(Position<E> v, E o) throws InvalidPositionException {
TreePosition<E> vv = checkPosition(v);
E temp = v.element();
vv.setElement(o);
return temp;
}
public Position<E> root() throws EmptyTreeException {
if (root == null)
throw new EmptyTreeException("The tree is empty");
return root;
}
public Position<E> parent(Position<E> v) throws InvalidPositionException, BoundaryViolationException {
TreePosition<E> vv = checkPosition(v);
Position<E> parentPos = vv.getParent();
if (parentPos == null)
throw new BoundaryViolationException("No parent");
return parentPos;
}
public Iterable<Position<E>> children(Position<E> v) throws InvalidPositionException {
TreePosition<E> vv = checkPosition(v);
if (isExternal(v))
throw new InvalidPositionException("External nodes have no children");
return vv.getChildren();
}
public boolean isInternal(Position<E> v) throws InvalidPositionException {
return !isExternal(v);
}
public boolean isExternal(Position<E> v) throws InvalidPositionException {
TreePosition<E> vv = checkPosition(v);
return vv.getChildren().isEmpty();
}
public boolean isRoot(Position<E> v) throws InvalidPositionException {
checkPosition(v);
return (v == root());
}
public Iterator<E> iterator() {
Iterable<Position<E>> positions = positions();
PositionList<E> elements = new NodePositionList<E>();
for (Position<E> pos: positions)
elements.addLast(pos.element());
return elements.iterator();
}
////////////////////////// help methods
protected void preorderPositions(Position<E> v, PositionList<Position<E>> pos) throws InvalidPositionException {
TreePosition<E> a = checkPosition(v);
pos.addLast(a);
for (Position<E> w : a.getChildren())
preorderPositions(w, pos);
}
protected void postorderPositions(Position<E> v, PositionList<Position<E>> pos) throws InvalidPositionException {
TreePosition<E> a = checkPosition(v);
for (Position<E> w : a.getChildren())
postorderPositions(w, pos);
pos.addLast(a);
}
protected TreePosition<E> checkPosition(Position<E> v)
throws InvalidPositionException {
if (v == null || !(v instanceof TreePosition) || isEmpty() )
throw new InvalidPositionException("The position is invalid");
return (TreePosition<E>) v;
}
public Position<E> addRoot(E e) throws NonEmptyTreeException {
if(!isEmpty())
throw new NonEmptyTreeException("Tree already has a root");
size = 1;
root = createNode(e,null,null);
return root;
}
public void swapElements(Position<E> v, Position<E> w)
throws InvalidPositionException {
TreePosition<E> vv = checkPosition(v);
TreePosition<E> ww = checkPosition(w);
E temp = w.element();
ww.setElement(v.element());
vv.setElement(temp);
}
protected TreePosition<E> createNode(E element, TreePosition<E> parent, PositionList<Position<E>> children) {
return new TreeNode<E>(element,parent,children);
}
public E removeRoot() throws EmptyTreeException, UndeletableNodeException {
if (isEmpty())
throw new EmptyTreeException();
if (size() > 1)
throw new UndeletableNodeException();
E to_return = root().element();
root = null;
size = 0;
return to_return;
}
public E removeExternalChild(Position<E> v) throws UndeletableNodeException,InvalidPositionException{
TreePosition<E> a = checkPosition(v);
if (isExternal(a))
throw new InvalidPositionException();
PositionList<Position<E>> c = a.getChildren();
if (isInternal(c.first().element()))
throw new UndeletableNodeException();
E to_return = (c.remove(c.first())).element();
size--;
return to_return;
}
public Position <E> addChild (E e , Position<E> v) throws InvalidPositionException {
TreePosition<E> a = checkPosition(v);
a.getChildren().addLast(createNode(e,a,null));
size++;
return a.getChildren().last().element();
}
public E remove (Position<E> v) {
TreePosition<E> a = checkPosition(v);
if (isInternal(a))
throw new InvalidPositionException();
if (size() == 1)
return removeRoot();
PositionList<Position<E>> list = a.getParent().getChildren();
Iterable<Position<Position<E>>> it = list.positions();
for (Position<Position<E>> w : it){
if (w.element() == v){
size--;
return (list.remove(w)).element();
}
}
return null;
}
@Override
public String toString() {
String to_return = "";
to_return = to_return + "[";
if (size() == 0 )
return to_return+= "]";
NodePositionList<Position<E>> t = new NodePositionList<Position<E>>();
postorderPositions(root(),t);
for (Position<E> w :t){
to_return+=w.element() + ",";
}
to_return = to_return.substring(0, to_return.length()-1);
return to_return+= "]";
}
}