import java.util.HashSet;
import java.util.Iterator;

public class GCTest4 {
	static int m1;

	void aMethod(Node paramNode) {
		Node m2 = new Node("n1");
		Node tmpNode;

		tmpNode = m2.createChild("n11");
		tmpNode.createChild("n111");
		tmpNode = m2.createChild("n12");
		tmpNode.createChild("n121");
		tmpNode.createChild("n122");
		tmpNode.createChild("n123");

		Node m3 = new Node("n2");
		tmpNode = m3.createChild("n21");
		tmpNode.appendChild( m3 );

		System.out.println("output just for testing reasons...");
		System.out.println("name of parameter Node: "+paramNode.getName() );
		System.out.println("name of Node referenced by m2: "+m2.getName() );
		System.out.println("child's names:");
		m2.getChildsNames();

		System.out.println("name of Node referenced by m3: "+m3.getName() );
		System.out.println("is n21 child of n2? "+m3.isChild("n21") );
		System.out.println("is n2 child of n21? "+tmpNode.isChild("n2") );

		tmpNode = new Node("n31");

		((tmpNode.createChild("n32")).createChild("n33")).appendChild(tmpNode);

		tmpNode = null;
		System.gc();
	} //aMethod()

	public static void main(String[] args) {
		(new GCTest4()).aMethod(new Node("n4"));
	} //end main()
} //class GCTest4

class Node {
	HashSet childs = new HashSet();
	String name;

	public Node(String name) {
		this.name = name;
		System.out.println("created Node object named "+this.name);
	} //Node(String)

	public void appendChild(Node child) {
		childs.add(child);
	} //appendChild(Node)

	public Node createChild(String name) {
		Node newChild = new Node( name );
		childs.add(newChild);
		return newChild;
	} //createChild(String)

	public String getName() {
		return name;
	} //getName()

	public boolean isChild(String name) {
		Iterator childIt = childs.iterator();
		while (childIt.hasNext()) {
			if ( ((Node) childIt.next()).getName() == name)
				return true;
		} //while
		return false;
	} //isChild(String)

	public void getChildsNames() {
		Node child;
		Iterator childIt = childs.iterator();
		while (childIt.hasNext()) {
			child = (Node) childIt.next(); //explicit (down) type cast needed since HashMap contains only Object instances
			System.out.println( child.getName() );
			child.getChildsNames();
		} //while
	} //getChildsNames()
	protected void finalize() {
		System.out.println("Node object named "+name+" freed!");
	} //finalize()
} //class Node