Advertisement
teodor_dalavera

Преведувач - CBHT

Dec 23rd, 2017
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.85 KB | None | 0 0
  1. package PreveduvacCBHT;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6.  
  7. class CBHT<K extends Comparable<K>, E> {
  8.  
  9.     // An object of class CBHT is a closed-bucket hash table, containing
  10.     // entries of class MapEntry.
  11.     private SLLNode<MapEntry<K,E>>[] buckets;
  12.  
  13.     @SuppressWarnings("unchecked")
  14.     public CBHT(int m) {
  15.         // Construct an empty CBHT with m buckets.
  16.         buckets = (SLLNode<MapEntry<K,E>>[]) new SLLNode[m];
  17.     }
  18.  
  19.     private int hash(K key) {
  20.         // Translate key to an index of the array buckets.
  21.         return Math.abs(key.hashCode()) % buckets.length;
  22.     }
  23.  
  24.     public SLLNode<MapEntry<K,E>> search(K targetKey) {
  25.         // Find which if any node of this CBHT contains an entry whose key is
  26.         // equal
  27.         // to targetKey. Return a link to that node (or null if there is none).
  28.         int b = hash(targetKey);
  29.         for (SLLNode<MapEntry<K,E>> curr = buckets[b]; curr != null; curr = curr.succ) {
  30.             if (targetKey.equals(((MapEntry<K, E>) curr.element).key))
  31.                 return curr;
  32.         }
  33.         return null;
  34.     }
  35.  
  36.     public void insert(K key, E val) {      // Insert the entry <key, val> into this CBHT.
  37.         MapEntry<K, E> newEntry = new MapEntry<K, E>(key, val);
  38.         int b = hash(key);
  39.         for (SLLNode<MapEntry<K,E>> curr = buckets[b]; curr != null; curr = curr.succ) {
  40.             if (key.equals(((MapEntry<K, E>) curr.element).key)) {
  41.                 // Make newEntry replace the existing entry ...
  42.                 curr.element = newEntry;
  43.                 return;
  44.             }
  45.         }
  46.         // Insert newEntry at the front of the 1WLL in bucket b ...
  47.         buckets[b] = new SLLNode<MapEntry<K,E>>(newEntry, buckets[b]);
  48.     }
  49.  
  50.     public void delete(K key) {
  51.         // Delete the entry (if any) whose key is equal to key from this CBHT.
  52.         int b = hash(key);
  53.         for (SLLNode<MapEntry<K,E>> pred = null, curr = buckets[b]; curr != null; pred = curr, curr = curr.succ) {
  54.             if (key.equals(((MapEntry<K,E>) curr.element).key)) {
  55.                 if (pred == null)
  56.                     buckets[b] = curr.succ;
  57.                 else
  58.                     pred.succ = curr.succ;
  59.                 return;
  60.             }
  61.         }
  62.     }
  63.  
  64.     public String toString() {
  65.         String temp = "";
  66.         for (int i = 0; i < buckets.length; i++) {
  67.             temp += i + ":";
  68.             for (SLLNode<MapEntry<K,E>> curr = buckets[i]; curr != null; curr = curr.succ) {
  69.                 temp += curr.element.toString() + " ";
  70.             }
  71.             temp += "\n";
  72.         }
  73.         return temp;
  74.     }
  75.  
  76. }
  77.  
  78. class MapEntry<K extends Comparable<K>,E> implements Comparable<K> {
  79.  
  80.     // Each MapEntry object is a pair consisting of a key (a Comparable
  81.     // object) and a value (an arbitrary object).
  82.     K key;
  83.     E value;
  84.  
  85.     public MapEntry (K key, E val) {
  86.         this.key = key;
  87.         this.value = val;
  88.     }
  89.    
  90.     public int compareTo (K that) {
  91.     // Compare this map entry to that map entry.
  92.         @SuppressWarnings("unchecked")
  93.         MapEntry<K,E> other = (MapEntry<K,E>) that;
  94.         return this.key.compareTo(other.key);
  95.     }
  96.  
  97.     public String toString () {
  98.         return "<" + key + "," + value + ">";
  99.     }
  100. }
  101.  
  102. class SLLNode<E> {
  103.     protected E element;
  104.     protected SLLNode<E> succ;
  105.  
  106.     public SLLNode(E elem, SLLNode<E> succ) {
  107.         this.element = elem;
  108.         this.succ = succ;
  109.     }
  110.  
  111.     @Override
  112.     public String toString() {
  113.         return element.toString();
  114.     }
  115. }
  116.  
  117. public class Preveduvac {
  118.  
  119.     public static void main(String[] args) throws IOException {
  120.        
  121.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  122.         String expr = br.readLine();
  123.         String[] exp;
  124.        
  125.         int n = Integer.parseInt(expr);
  126.         CBHT<String, String> h = new CBHT<>(2*n-1);
  127.        
  128.         for(int i=0; i<n; i++){
  129.             expr = br.readLine();
  130.             exp = expr.split(" ");
  131.             String mk = exp[0];
  132.             String en = exp[1];
  133.             h.insert(en, mk);
  134.         }
  135.        
  136.         while(!(expr = br.readLine()).equals("KRAJ")){
  137.             String word = expr;
  138.             if(h.search(word) != null){
  139.                 System.out.println(h.search(word).element.value);
  140.             }
  141.             else{
  142.                 System.out.println("/");
  143.             }
  144.         }
  145.        
  146.         br.close();
  147.  
  148.     }
  149.  
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement