Iz E-študij, proste zakladnice študentskega znanja
import java.util.*;
public class TestBinaryTree
{
public static void main(String[] args) //sestavljamo od spodaj navzgor
{
BinaryTree e = new BinaryTree("E"); //--.
BinaryTree g = new BinaryTree("G"); // listi
BinaryTree h = new BinaryTree("H"); // /
BinaryTree i = new BinaryTree("I"); //-
BinaryTree d = new BinaryTree("D",null,g); // koren "D" (ima samo desno poddrevo g)
BinaryTree f = new BinaryTree("F",h,i); // koren "F" (ima levo p. h in desno.p. i)
BinaryTree b = new BinaryTree("B",d,e); //
BinaryTree c = new BinaryTree("C",f,null); // koren...
BinaryTree tree = new BinaryTree("A",b,c);
System.out.println("a(celotno drevo) = " + tree);
System.out.println("a.contains(\"H\") = " + tree.contains("H"));
System.out.println("b = " + b);
System.out.println("b.contains(\"H\") = " + b.contains("H"));
System.out.println("c = " + c);
System.out.println("c.contains(\"H\") = " + c.contains("H"));
System.out.println();
BinaryTree.Iterator it;
System.out.print("LevelOrder (nivojski obhod): ");
for (it = tree.new LevelOrder(); it.hasNext(); )
System.out.print(it.next() + " ");
System.out.print("\nPreOrder (premi obhod): ");
for (it = tree.new PreOrder(); it.hasNext(); )
System.out.print(it.next() + " ");
System.out.print("\nInOrder (vmesni obhod): ");
for (it = tree.new InOrder(); it.hasNext(); )
System.out.print(it.next() + " ");
System.out.print("\nPostOrder (obratni obhod): ");
for (it = tree.new PostOrder(); it.hasNext(); )
System.out.print(it.next() + " ");
System.out.println();
System.out.println();
}
}