dsa

Data Structures & Algorithms - Spring 2018
Log | Files | Refs | README

BinaryTreeBasis.java (1029B)


      1 package lab11;
      2 
      3 public abstract class BinaryTreeBasis<T> {
      4     protected TreeNode<T> root;
      5 
      6     public BinaryTreeBasis() {
      7         root = null;
      8     }  // end default constructor
      9 
     10     public BinaryTreeBasis(T rootItem) {
     11         root = new TreeNode<T>(rootItem, null, null);
     12     }  // end constructor
     13 
     14     public boolean isEmpty() {
     15         // Returns true if the tree is empty, else returns false.
     16         return root == null;
     17     }  // end isEmpty
     18 
     19     public void makeEmpty() {
     20         // Removes all nodes from the tree.
     21         root = null;
     22     }  // end makeEmpty
     23 
     24     public T getRootItem() throws TreeException {
     25         // Returns the item in the tree's root.
     26         if (root == null) {
     27             throw new TreeException("TreeException: Empty tree");
     28         }
     29         else {
     30             return root.getItem();
     31         }  // end if
     32     }  // end getRootItem
     33 
     34     public abstract void setRootItem(T newItem);
     35     // Throws UnsupportedOperationException if operation
     36     // is not supported.
     37 
     38 }  // end BinaryTreeBasis
     39