Implementato Binary Tree. Bisogbna testarlo. da implementare il tostring

This commit is contained in:
2014-04-26 22:02:12 +02:00
parent 3fe3c635d8
commit cd1a246d4f
6 changed files with 413 additions and 3 deletions

35
binarytree/BTNode.java Normal file
View File

@@ -0,0 +1,35 @@
package binarytree;
public class BTNode<E> implements BTPosition<E> {
private E element = null;
private BTPosition<E> left = null;
private BTPosition<E> right = null;
private BTPosition<E> parent = null;
public BTNode() { }
public BTNode(E element, BTPosition<E> parent, BTPosition<E> left, BTPosition<E> right) {
setElement(element);
setParent(parent);
setLeft(left);
setRight(right);
}
public E element() { return element; }
public void setElement(E o) { element=o; }
public BTPosition<E> getLeft() { return left; }
public void setLeft(BTPosition<E> v) { left=v; }
public BTPosition<E> getRight() { return right; }
public void setRight(BTPosition<E> v) { right=v; }
public BTPosition<E> getParent() { return parent; }
public void setParent(BTPosition<E> v) { parent=v; }
}

View File

@@ -0,0 +1,14 @@
package binarytree;
import position.Position;
public interface BTPosition<E> extends Position<E> { // inherits element()
public void setElement(E o);
public BTPosition<E> getLeft();
public void setLeft(BTPosition<E> v);
public BTPosition<E> getRight();
public void setRight(BTPosition<E> v);
public BTPosition<E> getParent();
public void setParent(BTPosition<E> v);
}

View File

@@ -0,0 +1,20 @@
package binarytree;
import exceptions.BoundaryViolationException;
import exceptions.InvalidPositionException;
import position.Position;
import tree.Tree;
public interface BinaryTree<E> extends Tree<E> {
public Position<E> left(Position<E> v) throws InvalidPositionException, BoundaryViolationException;
public Position<E> right(Position<E> v) throws InvalidPositionException, BoundaryViolationException;
public boolean hasLeft(Position<E> v) throws InvalidPositionException;
public boolean hasRight(Position<E> v) throws InvalidPositionException;
}

View File

@@ -0,0 +1,315 @@
package binarytree;
import exceptions.BoundaryViolationException;
import exceptions.EmptyTreeException;
import exceptions.InvalidPositionException;
import exceptions.NonEmptyTreeException;
import position.NodePositionList;
import position.Position;
import position.PositionList;
import java.util.Iterator;
public class LinkedBinaryTree<E> implements BinaryTree<E> {
protected BTPosition<E> root;
protected int size;
public LinkedBinaryTree() {
root = null;
size = 0;
}
public int size() {
return size;
}
public boolean isEmpty() {
return (size == 0);
}
public boolean isInternal(Position<E> v) throws InvalidPositionException {
checkPosition(v);
return (hasLeft(v) || hasRight(v));
}
public boolean isExternal(Position<E> v) throws InvalidPositionException {
return !isInternal(v);
}
public boolean isRoot(Position<E> v) throws InvalidPositionException {
checkPosition(v);
return (v == root());
}
public boolean hasLeft(Position<E> v) throws InvalidPositionException {
BTPosition<E> vv = checkPosition(v);
return (vv.getLeft() != null);
}
public boolean hasRight(Position<E> v) throws InvalidPositionException {
BTPosition<E> vv = checkPosition(v);
return (vv.getRight() != null);
}
public Position<E> root() throws EmptyTreeException {
if (root == null)
throw new EmptyTreeException("The tree is empty");
return root;
}
public Position<E> left(Position<E> v) throws InvalidPositionException, BoundaryViolationException {
BTPosition<E> vv = checkPosition(v);
Position<E> leftPos = vv.getLeft();
if (leftPos == null)
throw new BoundaryViolationException("No left child");
return leftPos;
}
public Position<E> right(Position<E> v)
throws InvalidPositionException, BoundaryViolationException {
BTPosition<E> vv = checkPosition(v);
Position<E> rightPos = vv.getRight();
if (rightPos == null)
throw new BoundaryViolationException("No right child");
return rightPos;
}
public Position<E> parent(Position<E> v) throws InvalidPositionException, BoundaryViolationException {
BTPosition<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 {
PositionList<Position<E>> children = new NodePositionList<Position<E>>();
if (hasLeft(v))
children.addLast(left(v));
if (hasRight(v))
children.addLast(right(v));
return children;
}
public Iterable<Position<E>> positions() {
PositionList<Position<E>> positions = new NodePositionList<Position<E>>();
if(size != 0)
preorderPositions(root(), positions);
return positions;
}
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();
}
public E replace(Position<E> v, E o) throws InvalidPositionException {
BTPosition<E> vv = checkPosition(v);
E temp = v.element();
vv.setElement(o);
return temp;
}
public Position<E> sibling(Position<E> v) throws InvalidPositionException, BoundaryViolationException {
BTPosition<E> vv = checkPosition(v);
BTPosition<E> parentPos = vv.getParent();
if (parentPos != null) {
BTPosition<E> sibPos;
BTPosition<E> leftPos = parentPos.getLeft();
if (leftPos == vv)
sibPos = parentPos.getRight();
else
sibPos = parentPos.getLeft();
if (sibPos != null)
return sibPos;
}
throw new BoundaryViolationException("No sibling");
}
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,null);
return root;
}
public Position<E> insertLeft(Position<E> v, E e) throws InvalidPositionException {
BTPosition<E> vv = checkPosition(v);
Position<E> leftPos = vv.getLeft();
if (leftPos != null)
throw new InvalidPositionException("Node already has a left child");
BTPosition<E> ww = createNode(e, vv, null, null);
vv.setLeft(ww);
size++;
return ww;
}
public Position<E> insertRight(Position<E> v, E e) throws InvalidPositionException {
BTPosition<E> vv = checkPosition(v);
Position<E> rightPos = vv.getRight();
if (rightPos != null)
throw new InvalidPositionException("Node already has a right child");
BTPosition<E> w = createNode(e, vv, null, null);
vv.setRight(w);
size++;
return w;
}
public E remove(Position<E> v) throws InvalidPositionException {
BTPosition<E> vv = checkPosition(v);
BTPosition<E> leftPos = vv.getLeft();
BTPosition<E> rightPos = vv.getRight();
if (leftPos != null && rightPos != null)
throw new InvalidPositionException("Cannot remove node with two children");
BTPosition<E> ww;
if (leftPos != null)
ww = leftPos;
else if (rightPos != null)
ww = rightPos;
else
ww = null;
if (vv == root) {
if (ww != null)
ww.setParent(null);
root = ww;
}
else {
BTPosition<E> uu = vv.getParent();
if (vv == uu.getLeft())
uu.setLeft(ww);
else
uu.setRight(ww);
if(ww != null)
ww.setParent(uu);
}
size--;
return v.element();
}
/** Attaches two trees to be subtrees of an external node. */
public void attach(Position<E> v, BinaryTree<E> T1, BinaryTree<E> T2) throws InvalidPositionException {
BTPosition<E> vv = checkPosition(v);
if (isInternal(v))
throw new InvalidPositionException("Cannot attach from internal node");
int newSize = size + T1.size() + T2.size();
if (!T1.isEmpty()) {
BTPosition<E> r1 = checkPosition(T1.root());
vv.setLeft(r1);
r1.setParent(vv);
}
if (!T2.isEmpty()) {
BTPosition<E> r2 = checkPosition(T2.root());
vv.setRight(r2);
r2.setParent(vv);
}
size = newSize;
}
public void swapElements(Position<E> v, Position<E> w) throws InvalidPositionException {
BTPosition<E> vv = checkPosition(v);
BTPosition<E> ww = checkPosition(w);
E temp = w.element();
ww.setElement(v.element());
vv.setElement(temp);
}
public void expandExternal(Position<E> v, E l, E r) throws InvalidPositionException {
checkPosition(v);
if (!isExternal(v))
throw new InvalidPositionException("Node is not external");
insertLeft(v, l);
insertRight(v, r);
}
public void removeAboveExternal(Position<E> v) throws InvalidPositionException {
checkPosition(v);
if (!isExternal(v))
throw new InvalidPositionException("Node is not external");
if (isRoot(v))
remove(v);
else {
Position<E> u = parent(v);
remove(v);
remove(u);
}
}
public void attachLeaves (PositionList<E> L) {
if (!isEmpty())
attachLeaves_r(root(),L);
}
protected void attachLeaves_r(Position<E> v, PositionList<E> L) throws InvalidPositionException {
BTPosition<E>vv = checkPosition(v);
if (hasLeft(v))
attachLeaves_r(left(v), L);
if (hasRight(v))
attachLeaves_r(right(v), L);
if (isExternal(v)){
BTPosition<E> a = createNode( L.remove(L.first()) ,vv,null,null);
BTPosition<E> b = createNode( L.remove(L.first()) ,vv,null,null);
vv.setLeft(a);
vv.setRight(b);
size+=2;
}
}
// Other methods
protected BTPosition<E> checkPosition(Position<E> v) throws InvalidPositionException {
if (v == null || !(v instanceof BTPosition) || isEmpty() )
throw new InvalidPositionException("The position is invalid");
return (BTPosition<E>) v;
}
protected BTPosition<E> createNode(E element, BTPosition<E> parent, BTPosition<E> left, BTPosition<E> right) {
return new BTNode<E>(element,parent,left,right); }
protected void preorderPositions(Position<E> v, PositionList<Position<E>> pos) throws InvalidPositionException {
pos.addLast(v);
if (hasLeft(v))
preorderPositions(left(v), pos);
if (hasRight(v))
preorderPositions(right(v), pos);
}
protected void inorderPositions(Position<E> v, PositionList<Position<E>> pos) throws InvalidPositionException {
if (hasLeft(v))
inorderPositions(left(v), pos);
pos.addLast(v);
if (hasRight(v))
inorderPositions(right(v), pos);
}
@Override
public String toString() {
return super.toString();
}
}

View File

@@ -30,13 +30,14 @@ public class LinkedTreeTest {
System.out.println(a.isEmpty());
System.out.println(a);
a.addChild(100,a.children(a.root()).iterator().next());
System.out.println(a.height());
a.remove(a.children(a.root()).iterator().next());
System.out.println(a.size());
System.out.println(a.isEmpty());
System.out.println(a);
System.out.println(a.height());

View File

@@ -173,6 +173,31 @@ public class LinkedTree <E> implements Tree<E> {
}
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;
}
public int depth ( Position<E> v) throws InvalidPositionException{
TreePosition<E> a = checkPosition(v);
if (a.getParent() == null)