// Class Tree that defines a tree of objects // Version of 2/11/98 import java.lang.*; import java.util.*; public class Tree extends Object implements Cloneable { public Object NodeContent; private Vector ChildrenNodes; public Tree() { super(); NodeContent = new Object(); ChildrenNodes = new Vector(); } public Tree(Object TheObject) { super(); NodeContent = TheObject; ChildrenNodes = new Vector(); } public synchronized Tree Clone() { Tree TheNewTree; if (NodeContent != null) { TheNewTree = new Tree(NodeContent); if (!(IsLeaf())) { for (int i = 0; i < BranchNumber(); i++) { TheNewTree.AddChildren(GetChild(i).Clone()); } } } else { TheNewTree = new Tree(); } return TheNewTree; } public final synchronized boolean IsLeaf() throws NullPointerException { boolean TheQuestion = true; if (NodeContent == null) { System.out.println("Can't know if IsLeaf() for an empty tree."); throw new NullPointerException(); } else { TheQuestion = ChildrenNodes.isEmpty(); } return TheQuestion; } public final synchronized boolean IsEmpty() { return (NodeContent == null); } public final synchronized void AddChildren(Tree TheTree) throws NullPointerException { if (NodeContent == null) { System.out.println("Can't AddChildren(Atree) for an empty tree."); throw new NullPointerException(); } else { ChildrenNodes.addElement(TheTree); } } public final synchronized int BranchNumber() throws NullPointerException { int TheNumber = 0; if (NodeContent == null) { System.out.println("No Branch for an empty tree."); throw new NullPointerException(); } else { TheNumber = ChildrenNodes.size(); } return TheNumber; } public final synchronized boolean Fruitful() throws NullPointerException { boolean TheFruitAlive = true; if (NodeContent == null) { System.out.println("Can't Fruitful() for an empty tree."); throw new NullPointerException(); } else { if (!(IsLeaf())) { int i = 0; while ((i < BranchNumber()) && TheFruitAlive) { TheFruitAlive = (TheFruitAlive && GetChild(i).GivesFruit(NodeContent.getClass())); i++; } } } return TheFruitAlive; } private final synchronized boolean GivesFruit(Class TheClass) throws NullPointerException { boolean TheFruitGiven = true; if (NodeContent == null) { System.out.println("Can't verify GivesFruit(AClass) for an empty tree."); throw new NullPointerException(); } else { if (IsLeaf()) { TheFruitGiven = (TheClass == NodeContent.getClass()); } else { int i = 0; while ((i < BranchNumber()) && TheFruitGiven) { TheFruitGiven = (TheFruitGiven && GetChild(i).GivesFruit(NodeContent.getClass())); i++; } } } return TheFruitGiven ; } public final synchronized void insertChildrenAt(Tree TheTree, int index) throws NullPointerException, NoSuchElementException { if (NodeContent == null) { System.out.println("Can't insertChildrenAt(Atree,AIndex) for an empty tree."); throw new NullPointerException(); } else { try { ChildrenNodes.insertElementAt(TheTree, index); } catch (ArrayIndexOutOfBoundsException TheException) { System.out.println("Invalid child index for this tree: " + TheException); throw new NoSuchElementException(); } } } public final synchronized Tree GetChild(int index) throws ArrayIndexOutOfBoundsException { Tree TheChildTree = new Tree(); try { TheChildTree = (Tree) ChildrenNodes.elementAt(index); } catch (ArrayIndexOutOfBoundsException TheException) { System.out.println("Invalid child index for this tree: " + TheException); throw new ArrayIndexOutOfBoundsException(); } return TheChildTree; } public final synchronized Tree GetFirstChild() throws NoSuchElementException { Tree TheChildTree = new Tree(); try { TheChildTree = (Tree) ChildrenNodes.firstElement(); } catch (NoSuchElementException TheException) { System.out.println("No child for this tree: " + TheException); throw new NoSuchElementException(); } return TheChildTree; } public final synchronized Tree GetLastChild() throws NoSuchElementException { Tree TheChildTree = new Tree(); try { TheChildTree = (Tree) ChildrenNodes.lastElement(); } catch (NoSuchElementException TheException) { System.out.println("No child for this tree: " + TheException); throw new NoSuchElementException(); } return TheChildTree; } public final synchronized String toString() { String TheString = new String(); if (NodeContent != null) { TheString = TheString.concat(NodeContent.toString()); if (!(IsLeaf())) { TheString = TheString.concat(" ("); for (int i = 0; i < (BranchNumber() - 1); i++) { TheString = TheString.concat(GetChild(i).toString() + ", "); } TheString = TheString.concat(GetChild(BranchNumber() - 1).toString()); TheString = TheString.concat(")"); } } return TheString; } }