Filip_Markoski

[ADSx] - HashFun

Jan 29th, 2018
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.14 KB | None | 0 0
  1. import java.util.Map;
  2. import java.util.Scanner;
  3.  
  4. public class HashFun {
  5.     public static void main(String args[]) {
  6.         String text = "24\n" +
  7.                 "Mila 19.6.1989\n" +
  8.                 "Ivana 19.6.1977\n" +
  9.                 "Simon 19.6.1994\n" +
  10.                 "Ognen 19.6.1998\n" +
  11.                 "Leona 19.6.1999\n" +
  12.                 "Martin 19.6.1981\n" +
  13.                 "Vesna 13.4.2015\n" +
  14.                 "Katerina 12.3.1945\n" +
  15.                 "Milan 14.2.1996\n" +
  16.                 "Olja 13.2.1988\n" +
  17.                 "Bojan 19.6.1988\n" +
  18.                 "Bojan 19.7.1978\n" +
  19.                 "Bojan 19.7.1988\n" +
  20.                 "Bojana 19.6.1988\n" +
  21.                 "Bojan 21.6.1988\n" +
  22.                 "Bojan 22.7.1988\n" +
  23.                 "Bojan 24.7.1988\n" +
  24.                 "Bojan 27.7.1988\n" +
  25.                 "Ana 19.7.1987\n" +
  26.                 "Bojana 19.6.1988\n" +
  27.                 "Elena 1.1.1988\n" +
  28.                 "Filip 1.12.1988\n" +
  29.                 "Filip 2.11.1976\n" +
  30.                 "Elena 13.1.1966\n" +
  31.                 "6";
  32.  
  33.         Scanner scanner = new Scanner(text);
  34.         int numOfPeople = Integer.parseInt(scanner.nextLine());
  35.  
  36.         // Month -> List of Names
  37.         CBHT<Integer, String> map = new CBHT<>(12);
  38.         for (int i = 0; i < numOfPeople; i++) {
  39.             String[] split = scanner.nextLine().split("\\s+");
  40.             String name = split[0];
  41.             Integer month = Integer.parseInt(split[1].split("\\.")[1]);
  42.  
  43.             map.insertLast(month, name);
  44.         }
  45.  
  46.         Integer target = Integer.parseInt(scanner.nextLine());
  47.  
  48.         SLLNode<MapEntry<Integer, String>> current = map.searchList(target);
  49.  
  50.         System.out.println();
  51.         System.out.println(map);
  52.  
  53.         if (current == null) {
  54.             System.out.println("Nema");
  55.             return;
  56.         }
  57.  
  58.         while (current != null) {
  59.             System.out.print(current.element.value + " ");
  60.             current = current.succ;
  61.         }
  62.  
  63.         // deleteFirst a whole SLL at a certain key
  64.  
  65.         System.out.println("=====================");
  66.         map.deleteEntry(6, "Mila");
  67.  
  68.         System.out.println();
  69.         System.out.println(map);
  70.  
  71.  
  72.     }
  73. }
  74.  
  75. class SLLNode<E> {
  76.     protected E element;
  77.     protected SLLNode<E> succ;
  78.  
  79.     public SLLNode(E elem, SLLNode<E> succ) {
  80.         this.element = elem;
  81.         this.succ = succ;
  82.     }
  83.  
  84.     @Override
  85.     public String toString() {
  86.         return element.toString();
  87.     }
  88. }
  89.  
  90. class MapEntry<K extends Comparable<K>, V> implements Comparable<K> {
  91.  
  92.     // Each MapEntry object is a pair consisting of a key (a Comparable
  93.     // object) and a value (an arbitrary object).
  94.     K key;
  95.     V value;
  96.  
  97.     public MapEntry(K key, V val) {
  98.         this.key = key;
  99.         this.value = val;
  100.     }
  101.  
  102.     public int compareTo(K that) {
  103.         // Compare this map entry to that map entry.
  104.         @SuppressWarnings("unchecked")
  105.         MapEntry<K, V> other = (MapEntry<K, V>) that;
  106.         return this.key.compareTo(other.key);
  107.     }
  108.  
  109.     public String toString() {
  110.         return "<" + key + "," + value + ">";
  111.     }
  112. }
  113.  
  114. class CBHT<K extends Comparable<K>, V> {
  115.  
  116.     // An object of class CBHT is a closed-bucket hash table, containing
  117.     // entries of class MapEntry.
  118.     private SLLNode<MapEntry<K, V>>[] buckets;
  119.  
  120.     @SuppressWarnings("unchecked")
  121.     public CBHT(int m) {
  122.         // Construct an empty CBHT with m buckets.
  123.         buckets = (SLLNode<MapEntry<K, V>>[]) new SLLNode[m];
  124.     }
  125.  
  126.     private int hash(K key) {
  127.         // Translate key to an index of the array buckets.
  128.         return Math.abs(key.hashCode()) % buckets.length;
  129.     }
  130.  
  131.     public SLLNode<MapEntry<K, V>> search(K targetKey) {
  132.         // Find which if any node of this CBHT contains an entry whose key is
  133.         // equal
  134.         // to targetKey. Return a link to that node (or null if there is none).
  135.         int b = hash(targetKey);
  136.         for (SLLNode<MapEntry<K, V>> curr = buckets[b]; curr != null; curr = curr.succ) {
  137.             if (targetKey.equals(curr.element.key)) return curr;
  138.         }
  139.         return null;
  140.     }
  141.  
  142.     public void insert(K key, V val) {        // Insert the entry <key, val> into this CBHT.
  143.         MapEntry<K, V> newEntry = new MapEntry<K, V>(key, val);
  144.         int b = hash(key);
  145.         for (SLLNode<MapEntry<K, V>> curr = buckets[b]; curr != null; curr = curr.succ) {
  146.             if (key.equals(((MapEntry<K, V>) curr.element).key)) {
  147.                 // Make newEntry replace the existing entry ...
  148.                 curr.element = newEntry;
  149.                 return;
  150.             }
  151.         }
  152.         // Insert newEntry at the front of the 1WLL in bucket b ...
  153.         buckets[b] = new SLLNode<MapEntry<K, V>>(newEntry, buckets[b]);
  154.     }
  155.  
  156.     public void insertFirst(K key, V val) {
  157.         MapEntry<K, V> entry = new MapEntry<>(key, val);
  158.         int b = hash(key);
  159.         /**
  160.          * Prepending an element in the SLL */
  161.         SLLNode<MapEntry<K, V>> insert = new SLLNode<>(entry, buckets[b]);
  162.         buckets[b] = insert;
  163.     }
  164.  
  165.     public void insertLast(K key, V val) {
  166.         MapEntry<K, V> entry = new MapEntry<>(key, val);
  167.         int b = hash(key);
  168.         /**
  169.          * Append an element in the SLL */
  170.         if (buckets[b] != null) {
  171.             SLLNode<MapEntry<K, V>> insert = new SLLNode<>(entry, null);
  172.             SLLNode<MapEntry<K, V>> current = buckets[b];
  173.             if (current.element.value.equals(val)) return;
  174.             while (current.succ != null) {
  175.                 if (current.element.value.equals(val)) return;
  176.                 current = current.succ;
  177.             }
  178.             if (current.element.value.equals(val)) return;
  179.             current.succ = insert;
  180.         } else {
  181.             insertFirst(key, val);
  182.         }
  183.     }
  184.  
  185.     public SLLNode<MapEntry<K, V>> searchList(K targetKey) {
  186.         int b = hash(targetKey);
  187.         return buckets[b];
  188.     }
  189.  
  190.     public void deleteFirst(K key) {
  191.         // Delete the entry (if any) whose key is equal to key from this CBHT.
  192.         int b = hash(key);
  193.         for (SLLNode<MapEntry<K, V>> pred = null, curr = buckets[b]; curr != null; pred = curr, curr = curr.succ) {
  194.             if (key.equals(curr.element.key)) {
  195.                 if (pred == null)
  196.                     buckets[b] = curr.succ;
  197.                 else
  198.                     pred.succ = curr.succ;
  199.                 return;
  200.             }
  201.         }
  202.     }
  203.  
  204.     public void deleteLast(K key) {
  205.         int b = hash(key);
  206.         SLLNode<MapEntry<K, V>> current = buckets[b];
  207.         SLLNode<MapEntry<K, V>> successor = buckets[b].succ;
  208.         while (successor.succ != null) {
  209.             current = current.succ;
  210.             successor = successor.succ;
  211.         }
  212.         current.succ = null;
  213.     }
  214.  
  215.     public void deleteSLL(K key) {
  216.         int b = hash(key);
  217.         buckets[b] = null;
  218.     }
  219.  
  220.     public void deleteEntry(K key, V val) {
  221.         MapEntry<K, V> entry = new MapEntry<>(key, val);
  222.         int b = hash(key);
  223.  
  224.         SLLNode<MapEntry<K, V>> previous = buckets[b];
  225.         SLLNode<MapEntry<K, V>> current = buckets[b].succ;
  226.         SLLNode<MapEntry<K, V>> successor = buckets[b].succ.succ;
  227.  
  228.         if (previous.element.value.equals(val)) {
  229.             deleteFirst(key);
  230.             return;
  231.         }
  232.  
  233.         while (current.succ != null) {
  234.             if (current.element.value.equals(val)) {
  235.                 previous.succ = current.succ;
  236.             }
  237.  
  238.             previous = previous.succ;
  239.             current = current.succ;
  240.             successor = successor.succ;
  241.         }
  242.  
  243.         if (current.element.value.equals(val)){
  244.             previous.succ = null;
  245.         }
  246.  
  247.     }
  248.  
  249.     public String toString() {
  250.         StringBuilder temp = new StringBuilder();
  251.         for (int i = 0; i < buckets.length; i++) {
  252.             temp.append(i).append(":");
  253.             for (SLLNode<MapEntry<K, V>> curr = buckets[i]; curr != null; curr = curr.succ) {
  254.                 temp.append(curr.element.toString()).append(" ");
  255.             }
  256.             temp.append("\n");
  257.         }
  258.         return temp.toString();
  259.     }
  260.  
  261. }
Advertisement
Add Comment
Please, Sign In to add comment