Advertisement
Guest User

Untitled

a guest
Mar 6th, 2015
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.70 KB | None | 0 0
  1. package assignment8;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Collection;
  5. import java.util.NoSuchElementException;
  6.  
  7. public class BinarySearchTree<Type extends Comparable<? super Type>> implements SortedSet<Type> {
  8.  
  9.     node root;
  10.     int size;
  11.    
  12.     private class node {
  13.         Type data;
  14.         node left;
  15.         node right;
  16.  
  17.         public node(Type data, node left, node right) {
  18.             this.data = data;
  19.             this.left = left;
  20.             this.right = right;
  21.         }
  22.     }
  23.  
  24.     public BinarySearchTree() {
  25.         this.root = null;
  26.     }
  27.    
  28.     @Override
  29.     public boolean add(Type item) {
  30.         return addHelper(item, root);
  31.     }
  32.    
  33.     /**
  34.      * helper method for the add method
  35.      *
  36.      * @param item
  37.      * @param n
  38.      * @return boolean
  39.      */
  40.     public boolean addHelper(Type item, node n) {
  41.         if(n.data == item) {
  42.             return true;
  43.         }
  44.         else {
  45.             // item goes down the right branch
  46.             if(item.compareTo(n.data) >= 0) {
  47.                 if(n.right.equals(null)) {
  48.                     addNode(item, n, true);
  49.                     return true;
  50.                 }
  51.                 addHelper(item, n.right);  
  52.                 return true;
  53.             }
  54.            
  55.             // item goes down the left branch
  56.             if(item.compareTo(n.data) < 0) {
  57.                 if(n.left.equals(null)) {
  58.                     addNode(item, n, false);
  59.                     return true;
  60.                 }
  61.                 addHelper(item, n.left);   
  62.                 return true;
  63.             }
  64.         }
  65.         return false;
  66.     }
  67.  
  68.     /**
  69.      * helper method for adding a node to the left or right of the node n, if i is 0 adds to the left and i is 1 adds to the right
  70.      *
  71.      * @param item
  72.      * @param n
  73.      * @param i
  74.      */
  75.     public void addNode(Type item, node n, boolean right) {
  76.         // adds to the right
  77.         if(right == true) {
  78.             node rightNode = new node(item, null, null);
  79.             n.right = rightNode;
  80.         }
  81.         // adds to the left
  82.         else {
  83.             node leftNode = new node(item, null, null);
  84.             n.left = leftNode;
  85.         }
  86.     }
  87.    
  88.     @Override
  89.     public boolean addAll(Collection<? extends Type> items) {
  90.         boolean bool = false;
  91.         for(Type item: items) {
  92.             if(add(item)) {
  93.                 bool = true;
  94.             }
  95.         }
  96.         return bool;
  97.     }
  98.  
  99.     @Override
  100.     public void clear() {
  101.         // TODO Auto-generated method stub
  102.        
  103.     }
  104.  
  105.     @Override
  106.     public boolean contains(Type item) {
  107.         return recursiveContains(item, root);
  108.     }
  109.    
  110.     /**
  111.      * helper method for the contains that recursively goes through every node to see if it contains an item
  112.      *
  113.      * @param item
  114.      * @param n
  115.      * @return boolean
  116.      */
  117.     public boolean recursiveContains(Type item, node n) {
  118.         if(n.data == item) {
  119.             return true;
  120.         }
  121.         else {
  122.             recursiveContains(item, n.left);
  123.             recursiveContains(item, n.right);
  124.         }
  125.         return false;
  126.     }
  127.  
  128.     @Override
  129.     public boolean containsAll(Collection<? extends Type> items) {
  130.         for(Type item : items) {
  131.             if(contains(item) == false) {
  132.                 return false;
  133.             }
  134.             return true;
  135.         }
  136.         return true;
  137.     }
  138.  
  139.     @Override
  140.     public Type first() throws NoSuchElementException {
  141.         // TODO Auto-generated method stub
  142.         return null;
  143.     }
  144.  
  145.     @Override
  146.     public boolean isEmpty() {
  147.         if (size == 0) {
  148.             return true;
  149.         }
  150.         else {
  151.             return false;
  152.         }
  153.     }
  154.  
  155.     @Override
  156.     public Type last() throws NoSuchElementException {
  157.         // TODO Auto-generated method stub
  158.         return null;
  159.     }
  160.  
  161.     @Override
  162.     public boolean remove(Type item) {
  163.         // TODO Auto-generated method stub
  164.         return false;
  165.     }
  166.  
  167.     @Override
  168.     public boolean removeAll(Collection<? extends Type> items) {
  169.         // TODO Auto-generated method stub
  170.         return false;
  171.     }
  172.  
  173.     @Override
  174.     public int size() {
  175.         return sizeHelper(root);
  176.     }
  177.    
  178.     /**
  179.      * helper method for size which returns the size of the subtree including the node
  180.      *
  181.      * @param n
  182.      * @return int
  183.      */
  184.     public int sizeHelper(node n) {
  185.         return 1 + sizeHelper(n.left) + sizeHelper(n.right);
  186.     }
  187.  
  188.     @Override
  189.     public ArrayList<Type> toArrayList() {
  190.         // TODO Auto-generated method stub
  191.         return null;
  192.     }
  193.  
  194. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement