ch.bfh.algo.example
Class TreeExample

java.lang.Object
  extended by ch.bfh.algo.example.TreeExample

public class TreeExample
extends Object

This file contains some examples of the use of the Forest and the BinaryForest interfaces.

Version:
1.0
Author:
Emmanuel Benoist

Constructor Summary
TreeExample()
           
 
Method Summary
static void branchAllRoots(Forest<String> f, String root)
          This method creates a new node containing the value given as the root.
static void displayBinaryForest(BinaryForest<String> f)
          The displayBinaryForest method implements a recursive in-order traversal.
static void displayForest(Forest<String> f)
          This method displays all the nodes of a forests f This function contains a loop on all the roots of the forest.
static void ex1()
          The ex1 method executes all tests concerning the Forest.
static void ex2()
          The ex2 method contains the tests about the BinaryForest.
static BinaryForest<String> initBinaryForest()
          The initBinaryForest method is used to create a new binary forest.
static Forest<String> initForest()
          This method creates and initialize a Forest.
static void main(String[] args)
           
static void transferChildren(Forest<String> f, Position<String> p1, Position<String> p2)
          This method transfers all the children from the node p1 to the node p2.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

TreeExample

public TreeExample()
Method Detail

displayForest

public static void displayForest(Forest<String> f)
This method displays all the nodes of a forests f This function contains a loop on all the roots of the forest. Once a root is visited, all its subtree is displayed (preorder-traversal).

Parameters:
f - Forest the forest to be displayed.

branchAllRoots

public static void branchAllRoots(Forest<String> f,
                                  String root)
This method creates a new node containing the value given as the root. Then all the root nodes of the forest f are branched as children of this new node. The result is a forest containing only one single tree whose root is the new node.

Parameters:
f - a Forest
root - a String value that will label the new node to be created.

transferChildren

public static void transferChildren(Forest<String> f,
                                    Position<String> p1,
                                    Position<String> p2)
This method transfers all the children from the node p1 to the node p2. This means we transfer all the subtrees whose roots are children of p1 under p2.

Parameters:
f - Forest The forest in which the nodes are. All node need to be in the same forest.
p1 - The Position (i.e. node) that has children
p2 - the second node, that will receive the children of p1.

initForest

public static Forest<String> initForest()
This method creates and initialize a Forest. The resulting forest contains already some nodes that form some trees. This method creates a new DefaultForest, but manipulates and returns only a Forest.

Returns:
a new Forest

displayBinaryForest

public static void displayBinaryForest(BinaryForest<String> f)
The displayBinaryForest method implements a recursive in-order traversal. The aim of this method is to display the binary trees in a way that is familiar to us (with the root in the middle).

Parameters:
f - the BinaryForest that has to be displayed.

initBinaryForest

public static BinaryForest<String> initBinaryForest()
The initBinaryForest method is used to create a new binary forest. That forest contains some nodes and they are grouped into different binary trees.

Returns:
a newly created BinaryForest.

ex1

public static void ex1()
The ex1 method executes all tests concerning the Forest.


ex2

public static void ex2()
The ex2 method contains the tests about the BinaryForest.


main

public static void main(String[] args)