Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package csc143.data_structures;
- import java.lang.*;
- /**
- *@author Vita Wiebe
- *@version LA9
- */
- public class MySimpleSet extends SimpleSet {
- // Field:
- // Only requires one, the reference to the overall root node.
- Node root;
- // Size tracks the number of elements on the BST including
- //the root (overall root)
- private int size;
- /**
- * A private class for constructing the nodes of our BST
- */
- class Node {
- // Fields
- // Our data, e
- E e;
- // Instances of the underlying node objects
- Node left, right;
- /**
- * A parameter-less constructor
- */
- public Node() {
- }
- /** Our node constructor
- * @param E e, the data stored in it
- * @param Node left
- * @param Node right
- */
- public Node(E e, Node left, Node right) {
- this.e = e;
- this.left = left;
- this.right = right;
- }
- }
- /**
- * constructor for making first node, the overall root of tree
- * Essentially, an empty tree.
- */
- public MySimpleSet() {
- root = new Node(null);
- }
- // private MySimpleSet(E e, Node left, Node right) {
- //
- //
- //
- // }
- /**
- * Add an element to the set.
- * Call overloaded private add method.
- * @param e The element to be added to the set
- * @return <tt>true</tt> if this operation updated the contents of the set
- */
- public boolean add(E e) {
- Node node = new Node(e);
- while(add) {
- // do stuff to add to tree if possible and sort it as it goes in
- root = addToSubtree(root, e);
- // update size
- size++;
- // return true and thus execute once more
- return true;
- }
- // Our base case, at which point it stops
- return false;
- }
- /**
- * Helper method for public add method (seen above)
- * Used to determined whether to add (root) to our BST,
- * checks for duplicates, >/<
- */
- private static Node addToSubtree(Node r, Comparable e) {
- r = root;
- if(e == null) {
- r = new Node(e, null, null);
- }
- int comp = e.compareTo(r.e);
- if(comp == 0) {
- // don't add: it is a duplicate item
- System.out.println("Duplicate: item not added.");
- // a value of -1 means "less than" and item should be sorted
- // into left column
- }else if(comp == -1) {
- System.out.println("Item added to left side.");
- // add r to left side of BST
- }else if(comp == 1) {
- System.out.println("Item added to right side.");
- }
- }
- /**
- * Remove all elements from this set
- */
- public void clear();
- /**
- * Checks for the existance of the specified value within the set.
- *
- * @param e The value sought
- * @return <tt>true</tt> if the value exists in the set.
- */
- public boolean contains(E e) {
- }
- public boolean contains(Node root, value) {
- root = this.root;
- }
- /**
- * Check for the existance of elements in the set
- *
- * @return <tt>true</tt> is there are no elements in the set
- */
- public boolean isEmpty() {
- }
- /**
- * Return the number of elements in the set
- * @return the number of elements in the set.
- */
- public int size();
- /**
- * Returns a String representation of the contents of the set.
- * @return the String representation of the set.
- */
- public String toString();
- public static void main(String[] args) {
- // Some test instances here
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement