-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestTree.java
More file actions
110 lines (91 loc) · 4.31 KB
/
TestTree.java
File metadata and controls
110 lines (91 loc) · 4.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
public class TestTree extends TwoThreeTree{
public static void main(String[] args) throws IOException{
TwoThreeTree testTree = new TwoThreeTree( ); // Test binary search tree
int inputKey = 0; // User input key
char cmd; // Input command
//-----------------------------------------------------------------
// Need to read 'tokens' (namely priority values) input from the keyboard.
// In Java this takes instantiation of several classes.
//
// Initialize reader - To read a character at a time
InputStreamReader reader = new InputStreamReader(System.in);
// Initialize the tokenizer -
// To read tokens (words and numbers separated by whitespace)
StreamTokenizer tokens = new StreamTokenizer(reader);
// Note: use the nextToken( ) method to step through a stream of tokens.
// Use tokenizer's nval to obtain the number read.
// Since nval is of type double, cast it to an int.
System.out.println( );
System.out.println("Commands:");
System.out.println(" + key : Insert element");
System.out.println(" ? key : Search element");
System.out.println(" - key : Remove element");
System.out.println(" S key : Successor of the element");
System.out.println(" T : Inorder Traversal");
System.out.println(" M : The minimum element");
System.out.println(" C : Clear the tree");
System.out.println(" Q : Quit the test program");
System.out.println( );
do
{
testTree.output( ); // Output tree
System.out.println( );
System.out.print("Command: "); // Read command
cmd = (char)reader.read( );
while (Character.isWhitespace(cmd)) // ignore whitespace
cmd = (char)reader.read();
if ( cmd == '+' || cmd == 's' || cmd == 'S' ||
cmd == '-' || cmd == '?' )
{
tokens.nextToken( );
inputKey = (int)tokens.nval;
}
switch ( cmd )
{
case '+' : // insert
System.out.println("Insert : key = " + inputKey);
testTree.insert(inputKey);
break;
case '-' : // remove
System.out.println("Trying to Remove: " + inputKey);
testTree.delete(inputKey);
break;
case '?' :
System.out.println("Search for key : " + inputKey);
System.out.println("Search Result: " + testTree.search(inputKey));
break;
case 'S' : case 's' :
TreeNode temp = testTree.successor2(inputKey);
if (temp != null ){
if (testTree.getSpecial())
System.out.println("The successor of* " + inputKey +
" : " + temp.getKey2());
else
System.out.println("The successor of# " + inputKey +
" : " + temp.getKey1());
testTree.setSpecial(false);
}
break;
case 'C' : case 'c' :
System.out.println("Clear the tree");
testTree.setRoot(null);
break;
case 'M' : case 'm' :
System.out.println("The minimum key = " + testTree.minimum());
break;
case 'T' : case 't':
System.out.println("Traverse the tree ");
testTree.sort();
System.out.println( );
break;
case 'Q' : case 'q' : // Quit test program
break;
default : // Invalid command
System.out.println("Inactive or invalid command");
}
} while ( ( cmd != 'Q' ) && ( cmd != 'q' ) );
}
}