heap - inizio
This commit is contained in:
151
tree/binarytree/heap/ArrayListCompleteBinaryTree.java
Normal file
151
tree/binarytree/heap/ArrayListCompleteBinaryTree.java
Normal file
@@ -0,0 +1,151 @@
|
|||||||
|
package tree.binarytree.heap;
|
||||||
|
|
||||||
|
import arraylist.IndexList;
|
||||||
|
import exceptions.BoundaryViolationException;
|
||||||
|
import exceptions.EmptyTreeException;
|
||||||
|
import exceptions.InvalidPositionException;
|
||||||
|
import position.Position;
|
||||||
|
|
||||||
|
import java.util.Iterator;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created with xgiovio.macbookair.
|
||||||
|
* User: xgiovio
|
||||||
|
* Date: 28/04/14
|
||||||
|
* Time: 15:15
|
||||||
|
*/
|
||||||
|
public class ArrayListCompleteBinaryTree<E> implements CompleteBinaryTree<E> {
|
||||||
|
|
||||||
|
IndexList<BTPos<E>> T;
|
||||||
|
|
||||||
|
public E remove() throws EmptyTreeException {
|
||||||
|
if(isEmpty()) throw new EmptyTreeException("L’albero è vuoto");
|
||||||
|
return T.remove(size()).element();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Position<E> add(E e) {
|
||||||
|
int i = size() + 1;
|
||||||
|
BTPos<E> p = new BTPos<E>(e,i);
|
||||||
|
T.add(i, p);
|
||||||
|
return p;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int size() {
|
||||||
|
return T.size() -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isEmpty() {
|
||||||
|
return (size() == 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Position<E> left(Position<E> v) throws InvalidPositionException, BoundaryViolationException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Position<E> right(Position<E> v) throws InvalidPositionException, BoundaryViolationException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean hasLeft(Position<E> v) throws InvalidPositionException {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean hasRight(Position<E> v) throws InvalidPositionException {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Iterable<Position<E>> positions() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public E replace(Position<E> v, E e) throws InvalidPositionException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Position<E> root() throws EmptyTreeException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Position<E> parent(Position<E> v) throws InvalidPositionException, BoundaryViolationException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Iterable<Position<E>> children(Position<E> v) throws InvalidPositionException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isInternal(Position<E> v) throws InvalidPositionException {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isExternal(Position<E> v) throws InvalidPositionException {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isRoot(Position<E> v) throws InvalidPositionException {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Iterator<E> iterator() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// check position
|
||||||
|
protected BTPos<E>checkPosition(Position<E> v) throws InvalidPositionException
|
||||||
|
{
|
||||||
|
if (v == null || !(v instanceof BTPos))
|
||||||
|
throw new InvalidPositionException("La posizione non è valida");
|
||||||
|
return (BTPos<E>) v;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// internal class
|
||||||
|
protected static class BTPos<E> implements Position<E> {
|
||||||
|
E element; int index;
|
||||||
|
|
||||||
|
public BTPos(E elt, int i) {
|
||||||
|
element = elt;
|
||||||
|
index = i;
|
||||||
|
}
|
||||||
|
|
||||||
|
public E element() { return element; }
|
||||||
|
|
||||||
|
public int index() { return index; }
|
||||||
|
|
||||||
|
public E setElement(E elt) {
|
||||||
|
E temp = element;
|
||||||
|
element = elt;
|
||||||
|
return temp; }
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
18
tree/binarytree/heap/CompleteBinaryTree.java
Normal file
18
tree/binarytree/heap/CompleteBinaryTree.java
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
package tree.binarytree.heap;
|
||||||
|
|
||||||
|
import position.Position;
|
||||||
|
import tree.binarytree.BinaryTree;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created with xgiovio.macbookair.
|
||||||
|
* User: xgiovio
|
||||||
|
* Date: 28/04/14
|
||||||
|
* Time: 15:12
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
public interface CompleteBinaryTree <E> extends BinaryTree<E>
|
||||||
|
{
|
||||||
|
public Position<E> add(E elem);
|
||||||
|
public E remove();
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user