implementazione di linked tree. manca il tostring
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
package tree;
|
||||
|
||||
import exceptions.BoundaryViolationException;
|
||||
import exceptions.EmptyTreeException;
|
||||
import exceptions.InvalidPositionException;
|
||||
import exceptions.*;
|
||||
import position.NodePositionList;
|
||||
import position.Position;
|
||||
import position.PositionList;
|
||||
|
||||
import java.util.Iterator;
|
||||
|
||||
@@ -15,62 +15,178 @@ import java.util.Iterator;
|
||||
*/
|
||||
public class LinkedTree <E> implements Tree<E> {
|
||||
|
||||
protected TreePosition<E> root = null;
|
||||
protected int size = 0;
|
||||
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return 0;
|
||||
return size;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return false;
|
||||
return (size == 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
public Iterable<Position<E>> positions() {
|
||||
return null;
|
||||
PositionList<Position<E>> positions = new NodePositionList<Position<E>>();
|
||||
if(size != 0)
|
||||
preorderPositions(root(), positions);
|
||||
return positions;
|
||||
}
|
||||
|
||||
@Override
|
||||
public E replace(Position<E> v, E e) throws InvalidPositionException {
|
||||
return null;
|
||||
public E replace(Position<E> v, E o) throws InvalidPositionException {
|
||||
TreePosition<E> vv = checkPosition(v);
|
||||
E temp = v.element();
|
||||
vv.setElement(o);
|
||||
return temp;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Position<E> root() throws EmptyTreeException {
|
||||
return null;
|
||||
if (root == null)
|
||||
throw new EmptyTreeException("The tree is empty");
|
||||
return root;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
public Position<E> parent(Position<E> v) throws InvalidPositionException, BoundaryViolationException {
|
||||
return null;
|
||||
TreePosition<E> vv = checkPosition(v);
|
||||
Position<E> parentPos = vv.getParent();
|
||||
if (parentPos == null)
|
||||
throw new BoundaryViolationException("No parent");
|
||||
return parentPos;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterable<Position<E>> children(Position<E> v) throws InvalidPositionException {
|
||||
return null;
|
||||
TreePosition<E> vv = checkPosition(v);
|
||||
if (isExternal(v))
|
||||
throw new InvalidPositionException("External nodes have no children");
|
||||
return vv.getChildren();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isInternal(Position<E> v) throws InvalidPositionException {
|
||||
return false;
|
||||
return !isExternal(v);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isExternal(Position<E> v) throws InvalidPositionException {
|
||||
return false;
|
||||
TreePosition<E> vv = checkPosition(v);
|
||||
return vv.getChildren().isEmpty();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
|
||||
|
||||
public boolean isRoot(Position<E> v) throws InvalidPositionException {
|
||||
return false;
|
||||
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 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--;
|
||||
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 int depth ( Position<E> v) {
|
||||
TreePosition<E> a = checkPosition(v);
|
||||
if (a.getParent() == null)
|
||||
return 0;
|
||||
return (depth(a.getParent()) + 1);
|
||||
}
|
||||
|
||||
public int height ( Position<E> v) {
|
||||
TreePosition<E> a = checkPosition(v);
|
||||
if (isExternal(a))
|
||||
return 0;
|
||||
int max = 0;
|
||||
for ( Position<E> w : a.getChildren()){
|
||||
if (max == 0 || height(w) > max){
|
||||
max = height(w);
|
||||
}
|
||||
}
|
||||
return 1 + max;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<E> iterator() {
|
||||
return null;
|
||||
public String toString() {
|
||||
return super.toString();
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package tree;
|
||||
|
||||
import position.NodePositionList;
|
||||
import position.Position;
|
||||
import position.PositionList;
|
||||
|
||||
@@ -13,13 +14,15 @@ public class TreeNode<E> implements TreePosition<E> {
|
||||
|
||||
private E element = null;
|
||||
private TreePosition<E> parent = null;
|
||||
private PositionList<Position<E>> children= null;
|
||||
private PositionList<Position<E>> children= new NodePositionList<Position<E>>();
|
||||
|
||||
|
||||
public TreeNode(E in_element, TreePosition<E> in_parent, PositionList<Position<E>> in_children ){
|
||||
element = in_element;
|
||||
parent = in_parent;
|
||||
children = in_children;
|
||||
if (in_children != null){
|
||||
children = in_children;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -35,7 +38,9 @@ public class TreeNode<E> implements TreePosition<E> {
|
||||
|
||||
@Override
|
||||
public void setChildren(PositionList<Position<E>> c) {
|
||||
children = c;
|
||||
if (c != null){
|
||||
children = c;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user