fixed linked tree

This commit is contained in:
2014-04-24 02:07:23 +02:00
parent 3607c918db
commit 3fe3c635d8
2 changed files with 84 additions and 9 deletions

View File

@@ -0,0 +1,46 @@
package com.xgiovio;
import tree.LinkedTree;
/**
* Created with MONSTER.
* User: xgiovio
* Date: 23/04/2014
* Time: 21:46
*/
public class LinkedTreeTest {
public static void main(String[] args) {
LinkedTree<Integer> a = new LinkedTree<Integer>();
System.out.println(a.size());
System.out.println(a.isEmpty());
System.out.println(a);
a.addRoot(10);
System.out.println(a.size());
System.out.println(a.isEmpty());
System.out.println(a);
System.out.println(a.height());
a.addChild(15,a.root());
a.addChild(30,a.root());
System.out.println(a.size());
System.out.println(a.isEmpty());
System.out.println(a);
a.addChild(100,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

@@ -73,9 +73,6 @@ public class LinkedTree <E> implements Tree<E> {
return vv.getChildren().isEmpty();
}
public boolean isRoot(Position<E> v) throws InvalidPositionException {
checkPosition(v);
return (v == root());
@@ -100,6 +97,17 @@ public class LinkedTree <E> implements Tree<E> {
preorderPositions(w, pos);
}
protected void postorderPositions(Position<E> v, PositionList<Position<E>> pos) throws InvalidPositionException {
TreePosition<E> a = checkPosition(v);
for (Position<E> w : a.getChildren())
postorderPositions(w, pos);
pos.addLast(a);
}
protected TreePosition<E> checkPosition(Position<E> v)
throws InvalidPositionException {
if (v == null || !(v instanceof TreePosition) || isEmpty() )
@@ -138,7 +146,7 @@ public class LinkedTree <E> implements Tree<E> {
throw new UndeletableNodeException();
E to_return = root().element();
root = null;
size--;
size = 0;
return to_return;
}
@@ -165,21 +173,25 @@ public class LinkedTree <E> implements Tree<E> {
}
public int depth ( Position<E> v) {
public int depth ( Position<E> v) throws InvalidPositionException{
TreePosition<E> a = checkPosition(v);
if (a.getParent() == null)
return 0;
return (depth(a.getParent()) + 1);
}
public int height ( Position<E> v) {
public int height () throws EmptyTreeException{
return height_f(root());
}
public int height_f ( 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);
if (max == 0 || height_f(w) > max){
max = height_f(w);
}
}
return 1 + max;
@@ -187,6 +199,23 @@ public class LinkedTree <E> implements Tree<E> {
@Override
public String toString() {
return super.toString();
String to_return = "";
to_return = to_return + "[";
if (size() == 0 )
return to_return+= "]";
NodePositionList<Position<E>> t = new NodePositionList<Position<E>>();
postorderPositions(root(),t);
for (Position<E> w :t){
to_return+=w.element() + ",";
}
to_return = to_return.substring(0, to_return.length()-1);
return to_return+= "]";
}
}