|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
public interface Forest<E>
The Forest
interface is used to store any type of
arborescence. A forest contains many nodes. Some of them have one
parent. They are internal nodes if they have at least one child and
are leaves if they do not have any child. The nodes without parents
are the roots. A forest (unlike a tree) can have many roots.
The nodes of the trees can be handled using Position
s.
The following example presents a program for displaying the content of a given forest.
public static void displayForest(Forestf){ LinkedSequence > s = new LinkedSequence >(); LinkedSequence depthList = new LinkedSequence (); int depth=0; for(Position root : f.roots()){ depth=0; s.add(root); depthList.add(depth); while(!s.isEmpty()){ Position tmpNode = s.delete(s.last()); depth = depthList.delete(depthList.last()); System.out.println(createXSpaces(depth)+tmpNode.element()); for(Position child : f.children(tmpNode)){ s.add(child); depthList.add(depth+1); } } } }
Method Summary | |
---|---|
List<Position<E>> |
children(Position<?> parent)
Returns the list of the children of a given node. |
void |
cut(Position<?> child)
Removes the relation between the given node ( child ) and its parent. |
void |
link(Position<?> parent,
Position<?> child)
Creates a link between parent and
child . |
void |
linkAfter(Position<?> sibling,
Position<?> node)
Inserts node in the list of children of the parent
of sibling . |
void |
linkBefore(Position<?> sibling,
Position<?> node)
Inserts node in the list of children of the parent
of sibling . |
Position<E> |
parent(Position<?> child)
Returns the parent of the given node. |
List<Position<E>> |
roots()
Returns the list of the roots contained in the forest. |
Methods inherited from interface ch.bfh.algo.Container |
---|
delete, element, encloses, insert, positionIterator, replace, swap |
Methods inherited from interface java.util.Collection |
---|
add, addAll, clear, contains, containsAll, equals, hashCode, isEmpty, iterator, remove, removeAll, retainAll, size, toArray, toArray |
Method Detail |
---|
List<Position<E>> roots()
Position<E> parent(Position<?> child) throws InvalidAccessorException
child
and return its parent
child
- the node we are looking at
child
.
InvalidAccessorException
- if the position is not a
valid node of this forest.List<Position<E>> children(Position<?> parent) throws InvalidAccessorException
parent
node and return the list of its children.
parent
- the node we are looking at
parent
.
InvalidAccessorException
- if the position is not a
valid node of this forest.void link(Position<?> parent, Position<?> child) throws InvalidAccessorException
parent
and
child
. child
is inserted in the list
of children of parent
and parent
becomes its parent.
parent
- the node that will be the parentchild
- the node that will be the child
InvalidAccessorException
- if the positions are not
valid nodes of this forest.void linkAfter(Position<?> sibling, Position<?> node) throws BoundaryViolationException, InvalidAccessorException
node
in the list of children of the parent
of sibling
. node
is inserted just
after sibling
in the list. The parent of
sibling
becomes the new parent of
node
.
sibling
- the future sibling of node
node
- the node that will be inserted
InvalidAccessorException
- if the positions are not
valid nodes of this forest.
BoundaryViolationException
void linkBefore(Position<?> sibling, Position<?> node) throws BoundaryViolationException, InvalidAccessorException
node
in the list of children of the parent
of sibling
. node
is inserted just
before sibling
in the list. The parent of
sibling
becomes the new parent of
node
.
sibling
- the future sibling of node
node
- the node that will be inserted
InvalidAccessorException
- if the positions are not
valid nodes of this forest.
BoundaryViolationException
void cut(Position<?> child) throws InvalidAccessorException
child
) and its parent. child
becomes
a new root of the forest.
child
- the node to be transformed into a root.
InvalidAccessorException
- if the position is not
valid node of this forest.
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |