Advertisement
Vita_Harvey

LA8_A

Feb 23rd, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.68 KB | None | 0 0
  1. package csc143.data_structures;
  2.  
  3. import java.lang.*;
  4.  
  5. /**
  6.  *@author Vita Wiebe
  7.  *@version LA9
  8.  */
  9. public class MySimpleSet extends SimpleSet {
  10.    
  11.    // Field:
  12.    // Only requires one, the reference to the overall root node.
  13.    Node root;
  14.    
  15.    // Size tracks the number of elements on the BST including
  16.    //the root (overall root)
  17.    private int size;
  18.    
  19.    /**
  20.     *  A private class for constructing the nodes of our BST
  21.     */
  22.    class Node {
  23.      
  24.       // Fields
  25.      
  26.       // Our data, e
  27.       E e;
  28.      
  29.       // Instances of the underlying node objects
  30.       Node left, right;
  31.      
  32.       /**
  33.        * A parameter-less constructor
  34.        */
  35.       public Node() {
  36.       }
  37.      
  38.       /** Our node constructor
  39.        * @param E e, the data stored in it
  40.        * @param Node left
  41.        * @param Node right
  42.        */
  43.       public Node(E e, Node left, Node right) {
  44.          this.e = e;
  45.          this.left = left;
  46.          this.right = right;
  47.       }  
  48.    }
  49.      
  50.    /**
  51.     * constructor for making first node, the overall root of tree
  52.     * Essentially, an empty tree.
  53.     */
  54.    public MySimpleSet() {
  55.       root = new Node(null);        
  56.    }
  57.    
  58.   //  private MySimpleSet(E e, Node left, Node right) {
  59. //          
  60. //        
  61. //    
  62. //    }
  63.    
  64.    /**
  65.    * Add an element to the set.
  66.    * Call overloaded private add method.
  67.    * @param e The element to be added to the set
  68.    * @return  <tt>true</tt> if this operation updated the contents of the set
  69.    */
  70.   public boolean add(E e) {
  71.       Node node = new Node(e);    
  72.       while(add) {
  73.          // do stuff to add to tree if possible and sort it as it goes in
  74.          root = addToSubtree(root, e);
  75.          // update size
  76.          size++;
  77.          // return true and thus execute once more
  78.          return true;        
  79.       }
  80.       // Our base case, at which point it stops
  81.       return false;
  82.   }
  83.  
  84.   /**
  85.    * Helper method for public add method (seen above)
  86.    * Used to determined whether to add (root) to our BST,
  87.    * checks for duplicates, >/<
  88.    */
  89.   private static Node addToSubtree(Node r, Comparable e) {
  90.      
  91.      r = root;
  92.      if(e == null) {    
  93.         r = new Node(e, null, null);
  94.      }
  95.      int comp = e.compareTo(r.e);
  96.      if(comp == 0) {
  97.          // don't add: it is a duplicate item
  98.          System.out.println("Duplicate: item not added.");
  99.      // a value of -1 means "less than" and item should be sorted
  100.      // into left column
  101.      }else if(comp == -1) {
  102.          System.out.println("Item added to left side.");
  103.          // add r to left side of BST
  104.      }else if(comp == 1) {
  105.          System.out.println("Item added to right side.");  
  106.      }  
  107.        
  108.   }
  109.    
  110.   /**
  111.    * Remove all elements from this set
  112.    */
  113.   public void clear();
  114.  
  115.   /**
  116.    * Checks for the existance of the specified value within the set.
  117.    *
  118.    * @param e The value sought
  119.    * @return  <tt>true</tt> if the value exists in the set.
  120.    */
  121.   public boolean contains(E e) {
  122.      
  123.   }
  124.  
  125.   public boolean contains(Node root, value) {
  126.       root = this.root;    
  127.  
  128.   }
  129.  
  130.   /**
  131.    * Check for the existance of elements in the set
  132.    *
  133.    * @return  <tt>true</tt> is there are no elements in the set
  134.    */
  135.   public boolean isEmpty() {
  136.   }
  137.  
  138.   /**
  139.    * Return the number of elements in the set
  140.    * @return  the number of elements in the set.
  141.    */
  142.   public int size();
  143.  
  144.   /**
  145.    * Returns a String representation of the contents of the set.
  146.    * @return  the String representation of the set.
  147.    */
  148.   public String toString();
  149.  
  150.  
  151.   public static void main(String[] args) {
  152.  
  153.   // Some test instances here
  154.  
  155.  
  156.   }
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement