Advertisement
ivana_andreevska

Zadaca za merenje i opstini HASH

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