Java Reference
In-Depth Information
element type E ). Here is a client program which constructs a SearchTree<String>
that puts words into alphabetical order and a SearchTree<Integer> that puts num-
bers into numerical order.
1 // This program uses the SearchTree class to construct a binary
2 // search tree of strings and a binary search tree of integers
3 // and print out each.
4
5 import java.util.*;
6
7 public class SearchTreeClient {
8 public static void main(String[] args) {
9 Scanner console = new Scanner(System.in);
10 SearchTree<String> names = new SearchTree<String>();
11 System.out.print("Name (blank to quit)? ");
12 String name = console.nextLine();
13 while (name.length() > 0) {
14 names.add(name);
15 System.out.print("Name (blank to quit)? ");
16 name = console.nextLine();
17 }
18 System.out.println();
19 System.out.println("Alphabetized list:");
20 names.print();
21 System.out.println();
22
23 SearchTree<Integer> numbers = new SearchTree<Integer>();
24 System.out.print("Next int (0 to quit)? ");
25 int number = console.nextInt();
26 while (number != 0) {
27 numbers.add(number);
28 System.out.print("Next int (0 to quit)? ");
29 number = console.nextInt();
30 }
31 System.out.println();
32 System.out.println("Sorted list:");
33 numbers.print();
34 }
35 }
Here is a sample log of execution:
Name (blank to quit)? Leonard
Name (blank to quit)? Sheldon
Search WWH ::




Custom Search