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