Advertisement
Aldin_SXR

BinarySearchTree.java

Jun 22nd, 2020
330
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.43 KB | None | 0 0
  1. package ds.bst;
  2.  
  3. public class BinarySearchTree<Key extends Comparable<Key>, Value> {
  4.    
  5.     private Node<Key, Value> root;
  6.    
  7.     /* Retrieve a value associated with a given key */
  8.     public Value get(Key key) {
  9.         Node<Key, Value> x = root;              // 1
  10.        
  11.         while (x != null) {                     // 2
  12.             int cmp = key.compareTo(x.key);     // 3
  13.             if (cmp < 0) {                      // 4
  14.                 x = x.left;                     // 4
  15.             } else if (cmp > 0) {               // 5
  16.                 x = x.right;                    // 5
  17.             } else {                            // 6
  18.                 return x.value;                 // 6
  19.             }
  20.         }
  21.        
  22.         return null;                            // 7
  23.     }
  24.    
  25.     /* Return the size of the BST (total number of nodes) */
  26.     public int size() {
  27.         return size(root);                          // 1
  28.     }
  29.    
  30.     /* Private size() method */
  31.     private int size(Node<Key, Value> x) { 
  32.         if (x == null) {                            // 2
  33.             return 0;                               // 2
  34.         }
  35.         return x.size;                              // 3
  36.     }
  37.    
  38.     /* Add a value to the BST under a given key */
  39.     public void put(Key key, Value value) {
  40.         root = put(root, key, value);                                               // 1
  41.     }
  42.    
  43.     /* Private put() method */
  44.     private Node<Key, Value> put(Node<Key, Value> x, Key key, Value value) {       
  45.         if (x == null) {                                                            // 2
  46.             return new Node<Key, Value>(key, value);                                // 2
  47.         }
  48.        
  49.         int cmp = key.compareTo(x.key);                                             // 3
  50.         if (cmp < 0) {                                                              // 4
  51.             x.left = put(x.left, key, value);                                       // 4
  52.         } else if (cmp > 0) {                                                       // 5
  53.             x.right = put(x.right, key, value);                                     // 5
  54.         } else {                                                                    // 6
  55.             x.value = value;                                                        // 6
  56.         }
  57.        
  58.         x.size = 1 + size(x.left) + size(x.right);                                  // 7
  59.         return x;                                                                   // 8
  60.     }
  61.    
  62.     /* Find the minimum key of the BST */
  63.     public Key findMin() {  
  64.        return findMin(root).key;                                    // 1
  65.     }  
  66.    
  67.     /* Private findMin() method */
  68.     private Node<Key, Value> findMin(Node<Key, Value> x) {
  69.        if (x.left == null) {                                        // 2
  70.            return x;                                                // 2
  71.        }
  72.        return findMin(x.left);                                      // 3
  73.     }
  74.    
  75.     /* Find the maximum key of the BST */
  76.     public Key findMax() {  
  77.         return findMax(root).key;                                   // 1
  78.     }
  79.    
  80.     /* Private findMax() method */
  81.     private Node<Key, Value> findMax(Node<Key, Value> x) {
  82.         if (x.right == null) {                                      // 2
  83.             return x;                                               // 2
  84.         }
  85.         return findMax(x.right);                                    // 3
  86.     }
  87.    
  88.     /* Find the rank of a given key */
  89.     public int rank(Key key) {                     
  90.         return rank(root, key);                                 // 1
  91.     }  
  92.    
  93.     /* Private rank() method */
  94.     private int rank(Node<Key, Value> x, Key key) {
  95.         if (x == null) {                                        // 2
  96.             return 0;                                           // 2
  97.         }
  98.        
  99.         int cmp = key.compareTo(x.key);                         // 3
  100.         if (cmp < 0) {                                          // 4
  101.             return rank(x.left, key);                           // 4
  102.         } else if (cmp > 0) {                                   // 5
  103.             return 1 + size(x.left) + rank(x.right, key);       // 5
  104.         } else {                                                // 6
  105.             return size(x.left);                                // 6
  106.         }
  107.     }
  108.    
  109.     /* Delete the node with the minimum key */
  110.     public void deleteMin() {
  111.         root = deleteMin(root);                                 // 1
  112.     }
  113.    
  114.     /* Private deleteMin() method */
  115.     private Node<Key, Value> deleteMin(Node<Key, Value> x) {
  116.         if (x.left == null) {                                   // 2
  117.             return x.right;                                     // 2
  118.         }
  119.        
  120.         x.left = deleteMin(x.left);                             // 3
  121.         x.size = 1 + size(x.left) + size(x.right);              // 4
  122.        
  123.         return x;                                               // 5
  124.     }
  125.    
  126.     /* Delete a node with the given key */
  127.     public void delete(Key key) {
  128.         root = delete(root, key);                                       // 1
  129.     }
  130.    
  131.     /* Private delete() method */
  132.     private Node<Key, Value> delete(Node<Key, Value> x, Key key) {
  133.         if (x == null) {                                                // 2
  134.             return null;                                                // 2
  135.         }
  136.        
  137.         int cmp = key.compareTo(x.key);                                 // 3
  138.         if (cmp < 0) {                                                  // 4
  139.             x.left = delete(x.left, key);                               // 4
  140.         } else if (cmp > 0) {                                           // 5
  141.             x.right = delete(x.right, key);                             // 5
  142.         } else {                                                        // 6
  143.             if (x.right == null) {                                      // 6
  144.                 return x.left;                                          // 6
  145.             }
  146.             if (x.left == null) {                                       // 7
  147.                 return x.right;                                         // 7
  148.             }
  149.            
  150.             Node<Key, Value> t = x;                                     // 8
  151.             x = findMin(t.right);                                       // 8
  152.             x.right = deleteMin(t.right);                               // 8
  153.             x.left = t.left;                                            // 8
  154.         }
  155.         x.size = 1 + size(x.left) + size(x.right);                      // 9
  156.         return x;                                                       // 10
  157.     }
  158.  
  159.     /* Simple in-order traversal */
  160.     public void iterate() {
  161.         inorder(root);
  162.     }
  163.    
  164.     /* Private inorder() method */
  165.     private void inorder(Node<Key, Value> x) {
  166.         if (x == null) {
  167.             return;
  168.         }
  169.        
  170.         inorder(x.left);
  171.         System.out.println(x.key);
  172.         inorder(x.right);
  173.     }
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement