Advertisement
gon2

rbhst.java

Mar 21st, 2018
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.22 KB | None | 0 0
  1. package heima9;
  2. import edu.princeton.cs.algs4.*;
  3.  
  4. public class RedBlackHashST<Key extends Comparable<Key>, Value> {
  5.     private static final int INIT_CAPACITY = 4;
  6.  
  7.     private int n;                                // number of key-value pairs
  8.     private int m;                                // hash table size
  9.     private RedBlackBST<Key, Value>[] st;  // array of linked-list symbol tables
  10.  
  11.  
  12.     /**
  13.      * Initializes an empty symbol table.
  14.      */
  15.     public RedBlackHashST() {
  16.         this(INIT_CAPACITY);
  17.     }
  18.  
  19.     /**
  20.      * Initializes an empty symbol table with {@code m} chains.
  21.      * @param m the initial number of chains
  22.      */
  23.     //@SuppressWarnings("unchecked")
  24.     public RedBlackHashST(int m) {
  25.         this.m = m;
  26.         st = (RedBlackBST<Key, Value>[]) new RedBlackBST[m];
  27.         for (int i = 0; i < m; i++)
  28.             st[i] = new RedBlackBST<Key, Value>();
  29.     }
  30.  
  31.     // resize the hash table to have the given number of chains,
  32.     // rehashing all of the keys
  33.     private void resize(int chains) {
  34.         RedBlackHashST<Key, Value> temp = new RedBlackHashST<Key, Value>(chains);
  35.         for (int i = 0; i < m; i++) {
  36.             for (Key key : st[i].keys()) {
  37.                 temp.put(key, st[i].get(key));
  38.             }
  39.         }
  40.         this.m  = temp.m;
  41.         this.n  = temp.n;
  42.         this.st = temp.st;
  43.     }
  44.  
  45.     // hash value between 0 and m-1
  46.     private int hash(Key key) {
  47.         return (key.hashCode() & 0x7fffffff) % m;
  48.     }
  49.  
  50.     /**
  51.      * Returns the number of key-value pairs in this symbol table.
  52.      *
  53.      * @return the number of key-value pairs in this symbol table
  54.      */
  55.     public int size() {
  56.         return n;
  57.     }
  58.  
  59.     /**
  60.      * Returns true if this symbol table is empty.
  61.      *
  62.      * @return {@code true} if this symbol table is empty;
  63.      *         {@code false} otherwise
  64.      */
  65.     public boolean isEmpty() {
  66.         return size() == 0;
  67.     }
  68.  
  69.     /**
  70.      * Returns true if this symbol table contains the specified key.
  71.      *
  72.      * @param  key the key
  73.      * @return {@code true} if this symbol table contains {@code key};
  74.      *         {@code false} otherwise
  75.      * @throws IllegalArgumentException if {@code key} is {@code null}
  76.      */
  77.     public boolean contains(Key key) {
  78.         if (key == null) throw new IllegalArgumentException("argument to contains() is null");
  79.         return get(key) != null;
  80.     }
  81.  
  82.     /**
  83.      * Returns the value associated with the specified key in this symbol table.
  84.      *
  85.      * @param  key the key
  86.      * @return the value associated with {@code key} in the symbol table;
  87.      *         {@code null} if no such value
  88.      * @throws IllegalArgumentException if {@code key} is {@code null}
  89.      */
  90.     public Value get(Key key) {
  91.         if (key == null) throw new IllegalArgumentException("argument to get() is null");
  92.         int i = hash(key);
  93.         return st[i].get(key);
  94.     }
  95.  
  96.     /**
  97.      * Inserts the specified key-value pair into the symbol table, overwriting the old
  98.      * value with the new value if the symbol table already contains the specified key.
  99.      * Deletes the specified key (and its associated value) from this symbol table
  100.      * if the specified value is {@code null}.
  101.      *
  102.      * @param  key the key
  103.      * @param  val the value
  104.      * @throws IllegalArgumentException if {@code key} is {@code null}
  105.      */
  106.     public void put(Key key, Value val) {
  107.         if (key == null) throw new IllegalArgumentException("first argument to put() is null");
  108.         if (val == null) {
  109.             delete(key);
  110.             return;
  111.         }
  112.  
  113.         // double table size if average length of list >= 10
  114.         if (n >= 10*m) resize(2*m);
  115.  
  116.         int i = hash(key);
  117.         if (!st[i].contains(key)) n++;
  118.         st[i].put(key, val);
  119.     }
  120.  
  121.     /**
  122.      * Removes the specified key and its associated value from this symbol table    
  123.      * (if the key is in this symbol table).    
  124.      *
  125.      * @param  key the key
  126.      * @throws IllegalArgumentException if {@code key} is {@code null}
  127.      */
  128.     public void delete(Key key) {
  129.         if (key == null) throw new IllegalArgumentException("argument to delete() is null");
  130.  
  131.         int i = hash(key);
  132.         if (st[i].contains(key)) n--;
  133.         st[i].delete(key);
  134.  
  135.         // halve table size if average length of list <= 2
  136.         if (m > INIT_CAPACITY && n <= 2*m) resize(m/2);
  137.     }
  138.  
  139.     // return keys in symbol table as an Iterable
  140.     public Iterable<Key> keys() {
  141.         Queue<Key> queue = new Queue<Key>();
  142.         for (int i = 0; i < m; i++) {
  143.             for (Key key : st[i].keys())
  144.                 queue.enqueue(key);
  145.         }
  146.         return queue;
  147.     }
  148.  
  149.  
  150.     /**
  151.      * Unit tests the {@code SeparateChainingHashST} data type.
  152.      *
  153.      * @param args the command-line arguments
  154.      */
  155.     public static void main(String[] args) {
  156.         RedBlackHashST<String, Integer> rbh = new RedBlackHashST<String, Integer>();
  157.         for (int i = 0; !StdIn.isEmpty(); i++) {
  158.             String key = StdIn.readString();
  159.             rbh.put(key, i);
  160.         }
  161.  
  162.         // print keys
  163.         for (String s : rbh.keys())
  164.             StdOut.println(s + " " + rbh.get(s));
  165.  
  166.     }
  167.  
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement