CSE143 Generic Binary Search Tree handout #27
Log of execution of testing program
-----------------------------------
Name (blank to quit)? Goofy
Name (blank to quit)? Daffy Duck
Name (blank to quit)? Superman
Name (blank to quit)? Wonder Woman
Name (blank to quit)? Cinderella
Name (blank to quit)? Batman
Name (blank to quit)? Robin
Name (blank to quit)? Batgirl
Name (blank to quit)? Cat Woman
Name (blank to quit)? Bugs Bunny
Name (blank to quit)? Road Runner
Name (blank to quit)? Wiley Coyote
Name (blank to quit)? Rocky
Name (blank to quit)? Bullwinkle
Name (blank to quit)?
Alphabetized list:
Batgirl
Batman
Bugs Bunny
Bullwinkle
Cat Woman
Cinderella
Daffy Duck
Goofy
Road Runner
Robin
Rocky
Superman
Wiley Coyote
Wonder Woman
Next int (0 to quit)? 12
Next int (0 to quit)? 8
Next int (0 to quit)? 7
Next int (0 to quit)? 15
Next int (0 to quit)? 3
Next int (0 to quit)? 48
Next int (0 to quit)? 8
Next int (0 to quit)? 6
Next int (0 to quit)? 92
Next int (0 to quit)? -8
Next int (0 to quit)? 23
Next int (0 to quit)? 105
Next int (0 to quit)? 0
Sorted list:
-8
3
6
7
8
8
12
15
23
48
92
105
Client program SearchTreeClient.java
------------------------------------
// This program uses the SearchTree class to construct a binary search tree of
// strings and a binary search tree of integers, printing out each.
import java.util.*;
public class SearchTreeClient {
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
SearchTree names = new SearchTree();
System.out.print("Name (blank to quit)? ");
String name = console.nextLine();
while (name.length() > 0) {
names.add(name);
System.out.print("Name (blank to quit)? ");
name = console.nextLine();
}
System.out.println();
System.out.println("Alphabetized list:");
names.print();
System.out.println();
SearchTree numbers = new SearchTree();
System.out.print("Next int (0 to quit)? ");
int number = console.nextInt();
while (number != 0) {
numbers.add(number);
System.out.print("Next int (0 to quit)? ");
number = console.nextInt();
}
System.out.println();
System.out.println("Sorted list:");
numbers.print();
}
}
SearchTreeNode class
--------------------
// Class SearchTreeNode is used to build binary search trees.
public class SearchTreeNode {
public E data; // data stored in this node
public SearchTreeNode left; // reference to left subtree
public SearchTreeNode right; // reference to right subtree
// post: constructs a SearchTreeNode as a leaf with given data
public SearchTreeNode(E data) {
this(data, null, null);
}
// post: constructs a SearchTreeNode with the given data and links
public SearchTreeNode(E data, SearchTreeNode left,
SearchTreeNode right) {
this.data = data;
this.left = left;
this.right = right;
}
}
SearchTree Class
----------------
// Class SearchTree stores and prints a binary search tree of objects of
// type E. E must implement the Comparable interface.
public class SearchTree> {
private SearchTreeNode overallRoot; // root of overall tree
// post: constructs an empty search tree
public SearchTree() {
overallRoot = null;
}
// post: value added to tree so as to preserve binary search tree
public void add(E value) {
overallRoot = add(overallRoot, value);
}
// post: value added to tree so as to preserve binary search tree
private SearchTreeNode add(SearchTreeNode root, E value) {
if (root == null)
root = new SearchTreeNode(value);
else if (root.data.compareTo(value) >= 0)
root.left = add(root.left, value);
else
root.right = add(root.right, value);
return root;
}
// post: returns true if tree contains value, returns false otherwise
public boolean contains(E value) {
return contains(overallRoot, value);
}
// post: returns true if given tree contains value, returns false otherwise
private boolean contains(SearchTreeNode root, E value) {
if (root == null)
return false;
else {
int compare = value.compareTo(root.data);
if (compare == 0)
return true;
else if (compare < 0)
return contains(root.left, value);
else
return contains(root.right, value);
}
}
// post: prints the data of the tree, one per line
public void print() {
printInorder(overallRoot);
}
// post: prints the data of the tree using an inorder traversal
private void printInorder(SearchTreeNode root) {
if (root != null) {
printInorder(root.left);
System.out.println(root.data);
printInorder(root.right);
}
}
}