Subversion Repositories programming

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
22 irasnyd 1
// Written by Ira Snyder
2
// 11-03-2004
3
import java.io.*;
4
import java.util.*;
5
 
26 irasnyd 6
//the class UnorderedTree from pg 338+ in the book
22 irasnyd 7
class UnorderedTree {
26 irasnyd 8
	private Object root;  //holds the node's data
9
	private Set subtrees; //holds all of the subtrees
10
	private int size;     //holds the size of the tree
22 irasnyd 11
 
12
	public UnorderedTree( ) { } //constructs an empty tree
13
 
14
	public UnorderedTree( Object root ) { //constructs a singleton
15
		this.root = root;
16
		subtrees = new HashSet(); //empty set
17
		size = 1;
18
	}
19
 
20
	//constructor to create any tree that is not a singleton
21
	//and is not an empty tree
22
	public UnorderedTree( Object root, Set trees ) {
26 irasnyd 23
		this(root); //calls "UnorderedTree( Object )" constructor
24
 
25
        //loops through all of the subtrees, and adds them this tree's
26
        //set of subtrees
22 irasnyd 27
		for( Iterator it=trees.iterator(); it.hasNext(); ) {
28
			Object object = it.next();
29
			if( object instanceof UnorderedTree ) {
30
				UnorderedTree tree = (UnorderedTree)object;
31
				subtrees.add(tree);
32
				size += tree.size;
33
			}
34
		}
35
	}
36
 
26 irasnyd 37
    //returns the size of the tree
22 irasnyd 38
	public int size( ) { return size; }
23 irasnyd 39
 
26 irasnyd 40
    //prints the tree in preorder to the specified PrintStream object
23 irasnyd 41
	public void printPreOrder( PrintStream ps ) {
26 irasnyd 42
        printPreOrderHelper( ps, this ); //print the whole tree in preorder
23 irasnyd 43
		ps.println(); //end the line
44
	}
45
 
26 irasnyd 46
    //a helper function to the printPreOrder() function
47
    //it is what actually uses recursion
48
    //
49
    //The algorithm behind this is:
50
    //  1. Print the current node
51
    //  2. Visit all the subtrees of the current node in PreOrder
52
    //
53
	private static void printPreOrderHelper( PrintStream ps, UnorderedTree tree ) {
23 irasnyd 54
		ps.print( tree.root + " " ); //print the node
55
		for( Iterator it=tree.subtrees.iterator(); it.hasNext(); ) {
56
			//recurse subtrees in preorder
57
			printPreOrderHelper( ps, (UnorderedTree)it.next() );
58
		}
59
	}
60
 
26 irasnyd 61
    //prints the tree in postorder to the specified PrintStream object
23 irasnyd 62
	public void printPostOrder( PrintStream ps ) {
63
		printPostOrderHelper( ps, this ); //print the whole tree in postorder
64
		ps.println(); //end the line
65
	}
26 irasnyd 66
 
67
    //a helper function for the printPostOrder() function
68
    //it is what actually uses recursion to print the tree
69
    //
70
    //The algorithm behind this is:
71
    //  1. Visit all the subtrees of the current node in PostOrder
72
    //  2. Print the current node
73
    //
23 irasnyd 74
	public static void printPostOrderHelper( PrintStream ps, UnorderedTree tree ) {
75
		for( Iterator it=tree.subtrees.iterator(); it.hasNext(); ) {
76
			//recurse subtrees in postorder
77
			printPostOrderHelper( ps, (UnorderedTree)it.next() );
78
		}
79
		ps.print( tree.root + " "); //print the node
80
	}
81
 
26 irasnyd 82
    //prints the tree in LevelOrder to the specified PrintStream object
83
    //
84
    //This is the algorithm (from pg 342-343 in the book):
85
    //  1. Initialize a Queue
86
    //  2. Add the root to the queue
87
    //  3. Repeat steps 4-6 until the queue is empty
88
    //  4. Remove the first node x from the queue
89
    //  5. Visit x
90
    //  6. Add all the children of x to the queue
91
    //
24 irasnyd 92
	public void printLevelOrder( PrintStream ps ) {
93
		Queue q = new Queue();
23 irasnyd 94
 
24 irasnyd 95
		q.enqueue(this); //add the root
96
 
97
		while( !q.isEmpty() ) {
98
 			//remove the first item in the queue
99
			UnorderedTree temp = (UnorderedTree)q.dequeue();
100
 
101
			ps.print(temp.root + " "); //visit the node
102
 
103
			//add all the children of temp to the queue
104
			for( Iterator it=temp.subtrees.iterator(); it.hasNext(); ) {
105
				q.enqueue( it.next() );
106
			}
107
		}
108
		ps.println(); //end the current line
109
	}
110
 
22 irasnyd 111
} //end class UnorderedTree
112