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 {
|
27 |
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 |
|
27 |
irasnyd |
12 |
public UnorderedTree( ) { } //constructs an empty tree
|
22 |
irasnyd |
13 |
|
27 |
irasnyd |
14 |
public UnorderedTree( Object root ) { //constructs a singleton
|
|
|
15 |
this.root = root;
|
|
|
16 |
subtrees = new HashSet(); //empty set
|
|
|
17 |
size = 1;
|
|
|
18 |
}
|
22 |
irasnyd |
19 |
|
27 |
irasnyd |
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 ) {
|
|
|
23 |
this(root); //calls "UnorderedTree( Object )" constructor
|
26 |
irasnyd |
24 |
|
|
|
25 |
//loops through all of the subtrees, and adds them this tree's
|
|
|
26 |
//set of subtrees
|
27 |
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 |
}
|
22 |
irasnyd |
36 |
|
26 |
irasnyd |
37 |
//returns the size of the tree
|
27 |
irasnyd |
38 |
public int size( ) { return size; }
|
23 |
irasnyd |
39 |
|
26 |
irasnyd |
40 |
//prints the tree in preorder to the specified PrintStream object
|
27 |
irasnyd |
41 |
public void printPreOrder( PrintStream ps ) {
|
26 |
irasnyd |
42 |
printPreOrderHelper( ps, this ); //print the whole tree in preorder
|
27 |
irasnyd |
43 |
ps.println(); //end the line
|
|
|
44 |
}
|
23 |
irasnyd |
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 |
//
|
27 |
irasnyd |
53 |
private static void printPreOrderHelper( PrintStream ps, UnorderedTree tree ) {
|
|
|
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 |
}
|
23 |
irasnyd |
60 |
|
26 |
irasnyd |
61 |
//prints the tree in postorder to the specified PrintStream object
|
27 |
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 |
//
|
27 |
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 |
}
|
23 |
irasnyd |
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 |
//
|
27 |
irasnyd |
92 |
public void printLevelOrder( PrintStream ps ) {
|
|
|
93 |
Queue q = new Queue();
|
23 |
irasnyd |
94 |
|
27 |
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();
|
24 |
irasnyd |
100 |
|
27 |
irasnyd |
101 |
ps.print(temp.root + " "); //visit the node
|
24 |
irasnyd |
102 |
|
27 |
irasnyd |
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 |
}
|
24 |
irasnyd |
110 |
|
22 |
irasnyd |
111 |
} //end class UnorderedTree
|
|
|
112 |
|