Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.87 KB | None | 0 0
  1. package assignment3;
  2.  
  3. public class BSTMap <K extends Comparable <K>, V> implements SimpleMap<K,V>{
  4.  
  5.     int compCount = 0;                      // Creating a int variable set to 0 to count the number of times compareTo is run in search.
  6.     int searchCount = 0;                    // Creating a int variable set to 0 to count the number of times search is run.
  7.     int size = 0;                           // Creating a int variable set to 0 to count the size of the tree.
  8.     private bstNode root;                   // Setting a private variable of type bstNode.
  9.     bstNode <K, V> currentNode = root;      // Creating an object of type bstNode set as the root.
  10.     bstNode <K, V> modNode = root;          // Creating an object of type bstNode set as the root.
  11.     bstNode <K, V> delNode = root;          // Creating an object of type bstNode set as the root.
  12.    
  13.    
  14.     // bstNode class.
  15.     private class bstNode <K extends Comparable <K>, V>{
  16.         K key;                                  // Creating key of generic type K
  17.         V value;                                // Creating value of generic type V
  18.         bstNode left, right;                    // Creating left and right of type bstNode
  19.        
  20.        
  21.         //Constructor
  22.         public bstNode(K key, V value) {
  23.             // Setting Key and value
  24.  
  25.             this.key = key;
  26.             this.value = value;
  27.         }
  28.     }
  29.     /*
  30.      *@returns boolean value
  31.      */
  32.     public boolean isEmpty() {
  33.         if (root == null) {                     // Checks to see if root is null.
  34.             return true;                        // Returns true if root is null.
  35.         }
  36.         return false;                           // Else returns false
  37.     }
  38.  
  39.  
  40.     /*
  41.      *@returns int value
  42.      */
  43.     public int size() { return size; }          // Returns the size.
  44.  
  45.     /*
  46.      *@param key, value
  47.      */
  48.     public void insert(K key, V value) throws KeyFoundException {   // Recursive insert function
  49.         if (root == null){
  50.             root.key = key;                     // Sets the roots key.
  51.             root.value = value;                 // Sets the roots value.
  52.             currentNode = root;                 // Setting currentNode to the root
  53.             size++;                             // Increments size of tree.
  54.             return;                             // Returns nothing.
  55.         }      
  56.                
  57.         if (currentNode == null) {        
  58.             currentNode.key = key;          // Sets currentNode's key to the parameters key.
  59.             currentNode.value = value;      // Sets currentNode's value to the parameters value.
  60.             size++;                         // Increments size.
  61.             currentNode = root;             // Reseting currentNode to root
  62.             return;                         // Returns nothing.
  63.         }
  64.         else if (key.compareTo(currentNode.key) == 0) {    
  65.             currentNode.value = value;                  // Sets currentNode's value to the parameters value.
  66.             currentNode = root;             // Reseting currentNode to root
  67.             return;                                     // Returns Nothing.
  68.         }
  69.         else if (key.compareTo(currentNode.key) < 0) {  // Comparing key to the currentNode's key to see if currentNode should go left.
  70.             currentNode = currentNode.left;             // sets currentNode to the left of currentNode.
  71.             insert(key, value);                         // Calls insert again same parameters.
  72.        
  73.         }
  74.         else if (key.compareTo(currentNode.key) > 0) {  // Comparing key to the currentNode's key to see if currentNode should go right.
  75.             currentNode = currentNode.right;            // Sets currentNode to the right of currentNode.
  76.             insert(key, value);                         // Calls insert again with the same parameters.
  77.         }  
  78.        
  79.     }
  80.  
  81.    
  82.     /*
  83.      *@param key
  84.      */
  85.     public void delete(K key) throws KeyNotFoundException {
  86.        
  87.         if(delNode == null) throw new KeyNotFoundException();           // Throws exception if the tree is null.
  88.        
  89.         else if (key.compareTo(delNode.key) == 0) {                     // Checks if the key is the same as the node's key
  90.            
  91.            
  92.             if(delNode.left == null && delNode.right == null){          // Checks if the node has no children.
  93.                 delNode = null;                                         // sets node to null
  94.                 size--;                                                 // decrements size
  95.                 bstNode <K, V> delNode = root;                          // resets node's location to the root.
  96.                 return;
  97.             }
  98.             else if(delNode.left != null && delNode.right == null) {    // Checks if node has only left child
  99.                 bstNode <K, V> replaceNode = delNode.left;              // Creates a instance of a node.
  100.                 while(replaceNode.right != null) {                      // Search's for the replacing node
  101.                     replaceNode = replaceNode.right;                    // Sets the replacing node.
  102.                 }
  103.                 delNode = replaceNode;                                  // Replaces the parent node
  104.                 replaceNode = null;                                     // Sets the leaf to null
  105.                 size--;                                                 // Decrements size
  106.                 bstNode <K, V> delNode = root;                          // Resets the position of the node to root
  107.                 return;
  108.             }
  109.             else if(delNode.left == null && delNode.right != null) {    // Checks if node has only right child
  110.                 bstNode <K, V> replaceNode = delNode.right;             // Creates instance of a node
  111.                 while(replaceNode.left != null) {                       // Search's for a replacing node
  112.                     replaceNode = replaceNode.left;                     // Sets the replacing node.
  113.                 }
  114.                 delNode = replaceNode;                                  // Replaces the parent node
  115.                 replaceNode = null;                                     // Sets leaf to null
  116.                 size--;                                                 // Decrements the size
  117.                 bstNode <K, V> delNode = root;                          // Resets the position of the node to root
  118.                 return;
  119.             }
  120.             else if (delNode.left != null && delNode.right != null) {   // Checks if node has two children
  121.                 bstNode <K, V> replaceNode = delNode.right;             // Creates an instance of a node
  122.                 while (replaceNode.left != null) {                      // Search's for a replacing node
  123.                     replaceNode = replaceNode.left;                     // Sets the replacing node.
  124.                 }
  125.                 if(replaceNode.right != null){                          // Checks the right subtree of the parent.
  126.                     bstNode <K, V> temp = replaceNode.right;            // Creates new instance of a node.
  127.                     delNode = replaceNode;                              // Overwrites the deleteNode
  128.                     replaceNode = temp;                                 // Overwrites the node that replaced deleteNode
  129.                     temp = null;                                        // Deletes a leaf
  130.                     size--;                                             // Decrements size
  131.                     bstNode <K, V> delNode = root;                      // Resets the location of delNode to the root.
  132.                     return;
  133.                 }
  134.                 delNode = null;                                         // deletes the root basically.
  135.                 size--;                                                 // Decrements size
  136.                 bstNode <K, V> delNode = root;                          // Resets the delNode to the root.
  137.                 return;
  138.                 }
  139.            
  140.         }else if (key.compareTo(delNode.key) < 0) {                     // Searching for the correct node that should be deleted.
  141.             delNode = delNode.left;                                     // Traverse left in the tree
  142.             delete(key);                                                // Recursively call delete
  143.         }else if(key.compareTo(delNode.key) > 0) {                      // Searching for the correct node that should be deleted.
  144.             delNode = delNode.right;                                    //Traverse right in the tree
  145.             delete(key);                                                // Recursively call delete
  146.         }
  147.        
  148.     }
  149.  
  150.    
  151.     /*
  152.      * @params key
  153.      * @returns V type
  154.      */
  155.     public V search(K key) throws KeyNotFoundException {
  156.         compCount = 0;                          // Resets compareTo counter to 0.
  157.         searchCount++;                          // Increments the search counter
  158.         bstNode <K, V> currentNode = root;      // Sets a currentNode to root
  159.         while (key.compareTo(currentNode.key) != 0) {                                       // Loops until exception is thrown or key is found
  160.            compCount++;                                                                     // Incrementing compareTo count
  161.             if (key.compareTo(currentNode.key) < 0) {                                       // Compares the key to the key of the current node to see which direction the tree should traverse.
  162.                 if (currentNode.left == null) throw new KeyNotFoundException();     // Throws KeyNotFoundException if the next location is null.
  163.                 currentNode = currentNode.left;                                             // traverses to the left
  164.                 compCount++;                                                                // Increments the compareTo counter
  165.                 continue;                                                                   // Continues to the next iteration of the loop
  166.             }
  167.             if (key.compareTo(currentNode.key) > 0) {                                       // Compares the key to the key of the current node to see which direction the tree should traverse.
  168.                 if (currentNode.right == null) throw new KeyNotFoundException();        // Throws KeyNotFoundException if the next location is null.
  169.                 currentNode = currentNode.right;                                            // Traverses to the right
  170.                 compCount++;                                                                // Increments the compare to counter
  171.                 continue;                                                                   // continues to the next iteration of the loop
  172.             }
  173.         }
  174.         return currentNode.value;               // Returns the value of the node that was being looked for.
  175.     }
  176.  
  177.  
  178.     /*
  179.      * @param key, value
  180.      */
  181.     public void modify(K key, V value) throws KeyNotFoundException {
  182.        
  183.         if(modNode == null) throw new KeyNotFoundException();       // Throws exception if tree is null.
  184.        
  185.         else if (key.compareTo(modNode.key) == 0) {     // Checking if the node we are modifying is found
  186.             modNode.value = value;                      // Modifying the value of the node
  187.             bstNode <K, V> modNode = root;              // Reseting the location of modNode
  188.             return;                                
  189.         }else if (key.compareTo(modNode.key) < 0) {     // Seeing if we should traverse left through the tree
  190.             modNode = modNode.left;                     // Traverse left
  191.             modify(key, value);                         // Recursively calling modify
  192.         }else if(key.compareTo(modNode.key) > 0) {      // Seeing if we should traverse right through the tree
  193.             modNode = modNode.right;                    // Traverse right
  194.             modify(key, value);                         // Recursively calling modify
  195.         }
  196.        
  197.     }
  198.    
  199.     /*
  200.      *@returns int value
  201.      */
  202.     public int getNumCompares() {
  203.         return compCount;               // Returns the compareTo count
  204.     }
  205.    
  206.     /*
  207.      *@returns int value
  208.      */
  209.     public int getNumSearch() {
  210.         return searchCount;
  211.     }
  212. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement