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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
/*
* esodata - data structures and other things, of varying utility
* Copyright 2022, Ben Culkin
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package bjc.funcdata.bst;
import java.io.*;
import java.util.*;
/**
* Example showing how to use the binary search tree.
*
* @author ben
*
*/
public class BinarySearchTreeExample {
private static void display(final BinarySearchTree<Character> tree, final Scanner input, PrintStream output) {
output.print("What order would you like the tree to be printed in (m for options): ");
char command;
while (true) {
command = input.nextLine().charAt(0);
TreeLinearizationMethod method = null;
switch (command) {
case 'm':
System.out.println("Possible tree printing methods: ");
System.out.println("\tp: Preorder printing (print parent first, then left & right).");
System.out.println("\ti: Inorder printing (print left first, then parent & right).");
System.out.println("\to: Postorder printing (print left first, then right & parent).");
break;
case 'p':
method = TreeLinearizationMethod.PREORDER;
break;
case 'i':
method = TreeLinearizationMethod.INORDER;
break;
case 'o':
method = TreeLinearizationMethod.POSTORDER;
break;
default:
System.out.println("ERROR: Unknown command.");
}
if (method != null) {
tree.traverse(method, element -> {
System.out.println("Node: " + element);
return true;
});
return;
}
System.out.print("What order would you like the tree to be printed in (m for options): ");
}
}
/**
* Main method of class
*
* @param args Unused CLI args
*/
public static void main(final String[] args) {
try (Scanner input = new Scanner(System.in)) {
runExample(input, System.out);
}
}
private static void runExample(final Scanner input, OutputStream outpt) {
System.out.println("Binary Tree Constructor/Searcher");
final BinarySearchTree<Character> tree = new BinarySearchTree<>((o1, o2) -> o1 - o2);
PrintStream output = new PrintStream(outpt);
char command = ' ';
while (command != 'e') {
output.print("Enter a command (m for help): ");
command = input.nextLine().charAt(0);
switch (command) {
case 'm':
output.println("Valid commands: ");
output.println("\tm: Display this help message.");
output.println("\te: Exit this program.");
output.println("\ta: Add a node to the binary tree.");
output.println("\td: Display the binary tree.");
output.println("\tr: Remove a node from the binary tree.");
output.println("\tf: Check if a given node is in the binary tree.");
output.println("\tt: Trim all deleted nodes from the tree.");
output.println("\tb: Balance the tree (also trims dead nodes)");
break;
case 'a':
output.print("Enter the letter to add to the binary tree: ");
command = input.nextLine().charAt(0);
tree.addNode(command);
break;
case 'r':
output.print("Enter the letter to add to the binary tree: ");
command = input.nextLine().charAt(0);
tree.deleteNode(command);
break;
case 'd':
display(tree, input, output);
break;
case 'f':
output.print("Enter the letter to add to the binary tree: ");
command = input.nextLine().charAt(0);
final boolean inTree = tree.isInTree(command);
if (inTree) {
output.printf("Node %s was found\n", command);
} else {
output.printf("Node %s was not found\n", command);
}
break;
case 't':
tree.trim();
break;
case 'b':
tree.balance();
break;
case 'e':
break;
default:
output.println("ERROR: Unrecognized command.");
}
}
}
}
|