Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.82 KB | None | 0 0
  1.  
  2. public class BSTMap<K extends Comparable<K>, V> implements SimpleMap<K, V> {
  3.     private int size;
  4.     Node<K, V> left;
  5.     Node<K, V> right;      
  6.     Node<K, V> root;
  7.     Node<K, V> current;
  8.     Node<K, V> previous;
  9.  
  10.     class Node <K extends Comparable <K>, V>{
  11.         protected Node<K, V> left;
  12.         protected Node<K, V> right;
  13.         protected Node<K, V> root;
  14.         protected Node<K, V> current;
  15.         protected Node<K, V> previous;
  16.  
  17.         public Node() {
  18.             root = null;
  19.             current = null;
  20.             previous = null;
  21.             left = null;
  22.             right = null;
  23.  
  24.         }
  25.         public Node(K key, V value) {
  26.             this.key = key;
  27.             this.value = value;
  28.         }
  29.     }
  30.     public BSTMMap() {
  31.         root = null;
  32.         right = null;
  33.         current = null;
  34.         previous = null;
  35.         left = null;
  36.     }
  37.     /**
  38.      * isEmpty()
  39.      * @return boolean value if the bst is empty or not. checks the node
  40.      */
  41.     @Override
  42.     public boolean isEmpty() {
  43.          if (root == null){
  44.              return true;
  45.          }
  46.         return false;
  47.     }
  48.  
  49.     /**
  50.      * size()
  51.      * @return returns the size of the tree as an int
  52.      */
  53.     @Override
  54.     public int size() {
  55.         return size;
  56.     }
  57.  
  58.     /**
  59.      * if root is null, first instance of insert will become the root. otherwise, it will iterate through the tree and place
  60.      * the keys appropriately depending on if it is greater or less. when current has reached null, it has reached the bottom of the tree
  61.      * and will increment the size and create a new node at that location
  62.      */
  63.     @Override
  64.     public void insert(K key, V value) throws KeyFoundException {
  65.         int compare;
  66.         Node<K,V> current = root;
  67.         compare = key.compareTo(current.key);
  68.  
  69.        
  70.         while (current != null && current.key != key) {
  71.             if (compare < 0 ) {
  72.                 current = current.left;
  73.                 continue;
  74.             } else if (compare > 0 ) {
  75.                 current = current.right;
  76.                 continue;
  77.             }
  78.         }
  79.  
  80.        
  81.         if (current == null) {
  82.             current.key = key;
  83.             current.value = value;
  84.         }
  85.         if (current.key == key) {
  86.             current.value = value;
  87.         }
  88.     }
  89.  
  90.     /**
  91.      * Deletes a key from the bst, throws keynotfound if key is not found
  92.      * Goes through multiple cases to make sure the tree is correctly balanced
  93.      */
  94.     @Override
  95.     public void delete(K key) throws KeyNotFoundException {
  96.         if (root == null) {
  97.             throw new KeyNotFoundException();
  98.         }
  99.         Node<K,V> current = root;
  100.         int compare;
  101.         compare = key.compareTo(current.key);
  102.         while (current.key != key) {
  103.             if (compare < 0) {
  104.                 current = current.left;
  105.                 continue;
  106.             } else if (compare > 0) {
  107.                 current = current.right;
  108.                 continue;
  109.             } else if (compare == 0) {
  110.                 previous = current;
  111.                 if (current.left == null & current.right == null) {
  112.                     current = null;
  113.                 }else if (current.left != null && current.right != null) {
  114.                     current = current.right;
  115.                     while(current.left != null) {
  116.                         current = current.left;
  117.                     }
  118.                     previous.key = current.key;
  119.                     previous.value = current.value;
  120.                     current = null;
  121.                
  122.                 }else if (current.left == null) {
  123.                    
  124.                     current = current.right;
  125.                     while(current.left != null){
  126.                         current = current.left;
  127.                     }
  128.                     previous.key = current.key;
  129.                     previous.value = current.value;
  130.                     current = null;
  131.                 } else if (current.right == null) {
  132.                     current = current.left;
  133.                     while(current.right != null) {
  134.                         current = current.right;
  135.                     }
  136.                     previous.key = current.key;
  137.                     previous.value = current.value;
  138.                     current = null;
  139.                 }
  140.             } else {
  141.                 current = root;
  142.             }
  143.         }
  144.  
  145.         }
  146.  
  147.     /**
  148.      * iteratively searches through the BST for a key, if value is null then there is no key to be found.
  149.      * if it is not null, then it returns all instances of the key found
  150.      */
  151.     @Override
  152.     public V search(K key) throws KeyNotFoundException {
  153.         int compare;
  154.         Node<K,V> current = root;
  155.  
  156.         //compare = key.compareTo(current.key);
  157.         while (current != null && current.key != key) {
  158.             if (key.compareTo(current.key) < 0) {
  159.                 if (current.left == null) throw new KeyNotFoundException();
  160.                 current = current.left;
  161.                 continue;
  162.             }
  163.             else if (key.compareTo(current.key) > 0) {
  164.                 if (current.right == null) throw new KeyNotFoundException();
  165.                 current = current.right;
  166.                 continue;
  167.             }
  168.         }
  169.         if (key.compareTo(current.key) == 0) {
  170.             return current.value;
  171.         } else {
  172.             throw new KeyNotFoundException();
  173.         }
  174.     }
  175.  
  176.     /**
  177.      * searches for the key given and modifies it's value to the given value.
  178.      * keynotfound if it is null
  179.      */
  180.     @Override
  181.     public void modify(K key, V value) throws KeyNotFoundException {
  182.         int compare;
  183.         Node<K,V> current = root;
  184.         compare = key.compareTo(current.key);
  185.         while (current != null && current.key != key) {
  186.             if (current == null) {
  187.                 throw new KeyNotFoundException();
  188.             } else if (compare == 0) {
  189.                 current.value = value;
  190.             } else if (compare < 0 ) {
  191.                 current = current.left;
  192.                 continue;
  193.             } else if (compare > 0 ) {
  194.                 current = current.right;
  195.                 continue;
  196.             }
  197.            
  198.         }
  199.     }
  200.  
  201. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement