implementazione di linked tree. manca il tostring
This commit is contained in:
18
exceptions/NonEmptyTreeException.java
Normal file
18
exceptions/NonEmptyTreeException.java
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
package exceptions;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Signals that the boundaries of a data structure have been illegally
|
||||||
|
* traversed (e.g. past the end of a list).
|
||||||
|
* @author Roberto Tamassia
|
||||||
|
*/
|
||||||
|
//Copyright (c) 2003 Brown University, Providence, RI
|
||||||
|
//Additional modifications and methods by xgiovio
|
||||||
|
|
||||||
|
public class NonEmptyTreeException extends RuntimeException {
|
||||||
|
public NonEmptyTreeException(String message) {
|
||||||
|
super (message);
|
||||||
|
}
|
||||||
|
public NonEmptyTreeException() {
|
||||||
|
super ("Tree already has a root");
|
||||||
|
}
|
||||||
|
}
|
||||||
17
exceptions/UndeletableNodeException.java
Normal file
17
exceptions/UndeletableNodeException.java
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
package exceptions;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created with MONSTER.
|
||||||
|
* User: xgiovio
|
||||||
|
* Date: 05/03/14
|
||||||
|
* Time: 0.09
|
||||||
|
*/
|
||||||
|
public class UndeletableNodeException extends RuntimeException {
|
||||||
|
|
||||||
|
public UndeletableNodeException(){
|
||||||
|
super("Undeletable Node");
|
||||||
|
}
|
||||||
|
public UndeletableNodeException(String msg){
|
||||||
|
super(msg);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,9 +1,9 @@
|
|||||||
package tree;
|
package tree;
|
||||||
|
|
||||||
import exceptions.BoundaryViolationException;
|
import exceptions.*;
|
||||||
import exceptions.EmptyTreeException;
|
import position.NodePositionList;
|
||||||
import exceptions.InvalidPositionException;
|
|
||||||
import position.Position;
|
import position.Position;
|
||||||
|
import position.PositionList;
|
||||||
|
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
|
|
||||||
@@ -15,62 +15,178 @@ import java.util.Iterator;
|
|||||||
*/
|
*/
|
||||||
public class LinkedTree <E> implements Tree<E> {
|
public class LinkedTree <E> implements Tree<E> {
|
||||||
|
|
||||||
|
protected TreePosition<E> root = null;
|
||||||
|
protected int size = 0;
|
||||||
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int size() {
|
public int size() {
|
||||||
return 0;
|
return size;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean isEmpty() {
|
public boolean isEmpty() {
|
||||||
return false;
|
return (size == 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public Iterable<Position<E>> positions() {
|
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 o) throws InvalidPositionException {
|
||||||
public E replace(Position<E> v, E e) throws InvalidPositionException {
|
TreePosition<E> vv = checkPosition(v);
|
||||||
return null;
|
E temp = v.element();
|
||||||
|
vv.setElement(o);
|
||||||
|
return temp;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public Position<E> root() throws EmptyTreeException {
|
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 {
|
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 {
|
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 {
|
public boolean isInternal(Position<E> v) throws InvalidPositionException {
|
||||||
return false;
|
return !isExternal(v);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean isExternal(Position<E> v) throws InvalidPositionException {
|
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 {
|
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
|
@Override
|
||||||
public Iterator<E> iterator() {
|
public String toString() {
|
||||||
return null;
|
return super.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package tree;
|
package tree;
|
||||||
|
|
||||||
|
import position.NodePositionList;
|
||||||
import position.Position;
|
import position.Position;
|
||||||
import position.PositionList;
|
import position.PositionList;
|
||||||
|
|
||||||
@@ -13,14 +14,16 @@ public class TreeNode<E> implements TreePosition<E> {
|
|||||||
|
|
||||||
private E element = null;
|
private E element = null;
|
||||||
private TreePosition<E> parent = 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 ){
|
public TreeNode(E in_element, TreePosition<E> in_parent, PositionList<Position<E>> in_children ){
|
||||||
element = in_element;
|
element = in_element;
|
||||||
parent = in_parent;
|
parent = in_parent;
|
||||||
|
if (in_children != null){
|
||||||
children = in_children;
|
children = in_children;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -35,8 +38,10 @@ public class TreeNode<E> implements TreePosition<E> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setChildren(PositionList<Position<E>> c) {
|
public void setChildren(PositionList<Position<E>> c) {
|
||||||
|
if (c != null){
|
||||||
children = c;
|
children = c;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TreePosition<E> getParent() {
|
public TreePosition<E> getParent() {
|
||||||
|
|||||||
Reference in New Issue
Block a user