<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">/**
 * A binary search tree on Strings.
 * 
 */
public class BST {

   TreeCell&lt;String&gt; root; // The root of the BST

   /**
    * Constructor.
    */
   public BST() {
      root = null;
   }

   /**
    * Insert. Nothing happens if the string is already in the BST.
    */
   public void insert(String string) {
      root = insert(string, root);
   }

   private static TreeCell&lt;String&gt; insert(String string, TreeCell&lt;String&gt; node) {
      if (node == null) return new TreeCell&lt;String&gt;(string);
      int compare = string.compareTo(node.datum);
      if (compare &lt; 0) node.left = insert(string, node.left);
      else if (compare &gt; 0) node.right = insert(string, node.right);
      return node;
   }

   /**
    * Show the contents of the BST in alphabetical order.
    */
   public void show() {
      show(root);
      System.out.println();
   }

   private static void show(TreeCell&lt;String&gt; node) {
      if (node == null) return;
      show(node.left);
      System.out.print(node.datum + " ");
      show(node.right);
   }

   /**
    * toString. Prints a "sideways" version of the tree. The root is on the
    * left; left and right children correspond to down and up, respectively.
    */
   public String toString() {
      return toString("", root);
   }

   private static String toString(String prefix, TreeCell&lt;String&gt; node) {
      if (node == null) return "";
      String string = prefix + node.datum.toString();
      if (node.right != null)
         string = toString("    " + prefix, node.right) + "\n" + string;
      if (node.left != null)
         string = string + "\n" + toString("    " + prefix, node.left);
      return string;
   }
}

/**
 * TreeCell
 */
class TreeCell&lt;T&gt; {

   T datum;
   TreeCell&lt;T&gt; left, right;

   /**
    * Constructor
    */
   public TreeCell(T datum) {
      this.datum = datum;
      left = null;
      right = null;
   }
}
</pre></body></html>