
class DefsTree extends BinTree {
  //This is a tree of bindings.  This is organized as a binary SEARCH tree
  //with the alphabetic order of the keys used for ordering purposes.  You
  //have to signal that a value is being redefined if that happens.  All yu
  //have to do is print a message to the screen saying what variable is
  //being redefined.  If a search failed to find what you are looking for
  //you have to print a message to the screen.  There is no delete.

  public DefsTree(Binding b){super(b);}

  public void insert(Binding b){
      DefsTree rightTree, leftTree;
      Binding r = (Binding) this.root();//cast Object to Binding
      //use Unicode for enforcing order:
      int rootUnicode = Character.getNumericValue(r.GetKey());
      int insertedUnicode = Character.getNumericValue(b.GetKey());
      if (insertedUnicode > rootUnicode) {
      //If we're inserting a "higher" letter than the root, put it on the right:
          if (this.right() == null) this.SetRight(new DefsTree(b));
          else {
              rightTree = (DefsTree) this.right();
              rightTree.insert(b);
          }
      }
      else if (insertedUnicode < rootUnicode){
      //If we're inserting a "lower" letter than the root, put it on the left:
          if (this.left() == null) this.SetLeft(new DefsTree(b));
          else {
              leftTree = (DefsTree) this.left();
              leftTree.insert(b);
          }
      }
      else {
      //insertedUnicode = rootUnicode, replacing variables
          System.out.println("Redefining " + r.GetKey());
          r.SetVal(b.GetVal());
      }
  }// end method insert

  public int lookup(char name){
      DefsTree rightTree, leftTree;
      Binding r = (Binding) this.root();//cast Object to Binding
      //Use Unicode to search:
      int rootUnicode = Character.getNumericValue(r.GetKey());
      int searchUnicode = Character.getNumericValue(name);
      if (searchUnicode > rootUnicode) {
      //If the number we're looking for is "higher" than the root, go to the right: 
          if (this.right() == null) System.out.println("Error--no such variable.");
          else {
              rightTree = (DefsTree) this.right();
              return rightTree.lookup(name);
          }
      }
      else if (searchUnicode < rootUnicode){
      //If the number we're looking for is "lower" than the root, go to the left:
          if (this.left() == null) System.out.println("Error--no such variable.");
          else {
              leftTree = (DefsTree) this.left();
              return leftTree.lookup(name);
          }
      }
      else {
      //searchUnicode = rootUnicode, we found it
          return r.GetVal();
      }
      return 0;
  }//end method lookup
  
}// end DefsTree
