Heap Completed
This commit is contained in:
31
com/xgiovio/ArrayListCompleteBinaryTreeTest.java
Normal file
31
com/xgiovio/ArrayListCompleteBinaryTreeTest.java
Normal file
@@ -0,0 +1,31 @@
|
||||
package com.xgiovio;
|
||||
|
||||
import tree.binarytree.heap.ArrayListCompleteBinaryTree;
|
||||
|
||||
/**
|
||||
* Created with MONSTER.
|
||||
* User: xgiovio
|
||||
* Date: 05/05/2014
|
||||
* Time: 02:34
|
||||
*/
|
||||
public class ArrayListCompleteBinaryTreeTest {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
ArrayListCompleteBinaryTree<Integer> a = new ArrayListCompleteBinaryTree<Integer>();
|
||||
|
||||
System.out.println(a.isEmpty());
|
||||
System.out.println(a.size());
|
||||
a.add(15);
|
||||
a.add(30);
|
||||
a.add(25);
|
||||
|
||||
System.out.println(a);
|
||||
System.out.println(a.isEmpty());
|
||||
System.out.println(a.size());
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -28,7 +28,7 @@ public class MyEntry<K,V> implements Entry<K,V> {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return ("" + key + " - " + value);
|
||||
return ("(" + key + " - " + value + ")");
|
||||
}
|
||||
|
||||
private K key;
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
package tree.binarytree.heap;
|
||||
|
||||
import arraylist.ArrayIndexList;
|
||||
import arraylist.IndexList;
|
||||
import exceptions.BoundaryViolationException;
|
||||
import exceptions.EmptyTreeException;
|
||||
import exceptions.InvalidPositionException;
|
||||
import position.NodePositionList;
|
||||
import position.Position;
|
||||
import position.PositionList;
|
||||
|
||||
import java.util.Iterator;
|
||||
|
||||
@@ -18,6 +21,13 @@ public class ArrayListCompleteBinaryTree<E> implements CompleteBinaryTree<E> {
|
||||
|
||||
IndexList<BTPos<E>> T;
|
||||
|
||||
|
||||
public ArrayListCompleteBinaryTree() {
|
||||
T = new ArrayIndexList<BTPos<E>>();
|
||||
T.add(0, null); // the location at rank 0 is deliberately empty
|
||||
}
|
||||
|
||||
|
||||
public E remove() throws EmptyTreeException {
|
||||
if(isEmpty()) throw new EmptyTreeException("L’albero è vuoto");
|
||||
return T.remove(size()).element();
|
||||
@@ -31,98 +41,112 @@ public class ArrayListCompleteBinaryTree<E> implements CompleteBinaryTree<E> {
|
||||
}
|
||||
|
||||
|
||||
@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;
|
||||
public Position<E> left(Position<E> v) throws InvalidPositionException, BoundaryViolationException {
|
||||
if (!hasLeft(v))
|
||||
throw new BoundaryViolationException("No left child");
|
||||
BTPos<E> vv = checkPosition(v);
|
||||
return T.get(2*vv.index());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Position<E> right(Position<E> v) throws InvalidPositionException, BoundaryViolationException {
|
||||
return null;
|
||||
public Position<E> right(Position<E> v) throws InvalidPositionException {
|
||||
if (!hasRight(v))
|
||||
throw new BoundaryViolationException("No right child");
|
||||
BTPos<E> vv = checkPosition(v);
|
||||
return T.get(2*vv.index() + 1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasLeft(Position<E> v) throws InvalidPositionException {
|
||||
return false;
|
||||
BTPos<E> vv = checkPosition(v);
|
||||
return 2*vv.index() <= size();
|
||||
}
|
||||
|
||||
@Override
|
||||
/** Returns whether v has a right child. */
|
||||
public boolean hasRight(Position<E> v) throws InvalidPositionException {
|
||||
return false;
|
||||
BTPos<E> vv = checkPosition(v);
|
||||
return 2*vv.index() + 1 <= size();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
public Iterable<Position<E>> positions() {
|
||||
return null;
|
||||
IndexList<Position<E>> P = new ArrayIndexList<Position<E>>();
|
||||
Iterator<BTPos<E>> iter = T.iterator();
|
||||
iter.next(); // skip the first position
|
||||
while (iter.hasNext())
|
||||
P.add(P.size(), iter.next());
|
||||
return P;
|
||||
}
|
||||
|
||||
@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;
|
||||
IndexList<E> list = new ArrayIndexList<E>();
|
||||
Iterator<BTPos<E>> iter = T.iterator();
|
||||
iter.next(); // skip the first element
|
||||
while (iter.hasNext())
|
||||
list.add(list.size(), iter.next().element());
|
||||
return list.iterator();
|
||||
}
|
||||
|
||||
|
||||
public E replace(Position<E> v, E o) throws InvalidPositionException {
|
||||
BTPos<E> vv = checkPosition(v);
|
||||
return vv.setElement(o);
|
||||
}
|
||||
|
||||
public Position<E> root() throws EmptyTreeException {
|
||||
if (isEmpty())
|
||||
throw new EmptyTreeException("Tree is empty");
|
||||
return T.get(1);
|
||||
}
|
||||
|
||||
|
||||
public Position<E> parent(Position<E> v) throws InvalidPositionException, BoundaryViolationException {
|
||||
if (isRoot(v))
|
||||
throw new BoundaryViolationException("No parent");
|
||||
BTPos<E> vv = checkPosition(v);
|
||||
return T.get(vv.index()/2);
|
||||
}
|
||||
|
||||
|
||||
public Iterable<Position<E>> children(Position<E> v) throws InvalidPositionException {
|
||||
checkPosition(v);
|
||||
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 boolean isInternal(Position<E> v) throws InvalidPositionException {
|
||||
checkPosition(v);
|
||||
return hasLeft(v); // if v has a right child it will have a left child
|
||||
}
|
||||
|
||||
public boolean isExternal(Position<E> v) throws InvalidPositionException {
|
||||
return !isInternal(v);
|
||||
}
|
||||
|
||||
public boolean isRoot(Position<E> v) throws InvalidPositionException {
|
||||
BTPos<E> vv = checkPosition(v);
|
||||
return vv.index() == 1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
// check position
|
||||
protected BTPos<E>checkPosition(Position<E> v) throws InvalidPositionException
|
||||
{
|
||||
if (v == null || !(v instanceof BTPos))
|
||||
if (v == null || !(v instanceof BTPos) || isEmpty())
|
||||
throw new InvalidPositionException("La posizione non è valida");
|
||||
return (BTPos<E>) v;
|
||||
}
|
||||
@@ -148,4 +172,30 @@ public class ArrayListCompleteBinaryTree<E> implements CompleteBinaryTree<E> {
|
||||
}
|
||||
|
||||
|
||||
|
||||
public String toString() {
|
||||
|
||||
Iterator<E> it = iterator();
|
||||
|
||||
String to_return = "";
|
||||
to_return = to_return + "[";
|
||||
|
||||
for (;it.hasNext();){
|
||||
E t = it.next();
|
||||
|
||||
if (it.hasNext()) {
|
||||
|
||||
to_return+=(t.toString() + " , ");
|
||||
} else {
|
||||
|
||||
to_return+=(t.toString());
|
||||
}
|
||||
}
|
||||
|
||||
return to_return+= "]";
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user