Sunday, September 13, 2015

[Data Structure] Map using array

public class HashEntry {
      private int key;
      private int value;

   HashEntry(int key, int value) {
            this.key = key;
            this.value = value;
   }     
 
      public int getKey() {
            return key;
   }
 
      public int getValue() {
            return value;
   }
}
public class HashMap {
      private final static int TABLE_SIZE = 128;
 
   HashEntry[] table;
 
   HashMap() {
            table = new HashEntry[TABLE_SIZE];
            for (int i = 0; i < TABLE_SIZE; i++)
                  table[i] = null;
   }
 
      public int get(int key) {
            int hash = (key % TABLE_SIZE);
      // algorithm tries to find an empty one by probing consequent slots in the array. 
            while (table[hash] != null && table[hash].getKey() != key)
               hash = (hash + 1) % TABLE_SIZE;
            if (table[hash] == null)
                  return -1;
            else
                  return table[hash].getValue();
   }
 
      public void put(int key, int value) {
            int hash = (key % TABLE_SIZE);
// algorithm tries to find an empty one by probing consequent slots in the array.
            while (table[hash] != null && table[hash].getKey() != key)
               hash = (hash + 1) % TABLE_SIZE;
            table[hash] = new HashEntry(key, value);
   }
}



Chaining /Remove
public class LinkedHashEntry {
      private int key;
      private int value;
      private LinkedHashEntry next;
 
  	LinkedHashEntry(int key, int value) {
            this.key = key;
            this.value = value;
            this.next = null;
  	}
 
      public int getValue() {
            return value;
  	}
 
      public void setValue(int value) {
            this.value = value;
  	}
 
      public int getKey() {
            return key;
  	}
 
      public LinkedHashEntry getNext() {
            return next;
  	}
 
      public void setNext(LinkedHashEntry next) {
            this.next = next;
  	}
}
public class HashMap {
      private final static int TABLE_SIZE = 128;
 
  	LinkedHashEntry[] table;
 
  	HashMap() {
            table = new LinkedHashEntry[TABLE_SIZE];
            for (int i = 0; i < TABLE_SIZE; i++)
                  table[i] = null;
  	}
 
      public int get(int key) {
            int hash = (key % TABLE_SIZE);
            if (table[hash] == null)
                  return -1;
            else {
              	LinkedHashEntry entry = table[hash];
                  // Go to end or key
                  while (entry != null && entry.getKey() != key)
                    	entry = entry.getNext();
                  // End
                  if (entry == null)
                        return -1;
                  // Key found
                  else
                        return entry.getValue();
        	}
  	}
 
      public void put(int key, int value) {
            int hash = (key % TABLE_SIZE);
            if (table[hash] == null)
                  table[hash] = new LinkedHashEntry(key, value);
            else {
              	LinkedHashEntry entry = table[hash];
                  // Go to end or key
                  while (entry.getNext() != null && entry.getKey() != key)
                        entry = entry.getNext();
                  // Key => Update
                  if (entry.getKey() == key)
                    	entry.setValue(value);
                  // End => New Node
                  else
                    	entry.setNext(new LinkedHashEntry(key, value));
        	}
  	}
 
      public void remove(int key) {
            int hash = (key % TABLE_SIZE);
            if (table[hash] != null) {
              	LinkedHashEntry prevEntry = null;
              	LinkedHashEntry entry = table[hash];
                  while (entry.getNext() != null && entry.getKey() != key) {
                    	prevEntry = entry;
                    	entry = entry.getNext();
              	}
                  if (entry.getKey() == key) {
                        if (prevEntry == null)
                             table[hash] = entry.getNext();
                        else
                         	prevEntry.setNext(entry.getNext());
                  }
        	}
  	}
}
Dynamic Size
public static class LinkedHashEntry
{
K key;
V value;
LinkedHashEntry next;

//contructors, getters and setters below
...
}
public class HashMap
{
private double loadFactor = 0.75;
private int elemCount;;
// private final static int TABLE_SIZE = 128;
 
private LinkedHashEntry[] table;

//contructors, getters and setters below
...


/**
 * Insert your super-mega-hash-function below :)
 */
static int hash(int h)
{
    h ^= (h >>> 20) ^ (h >>> 12);
    return h ^ (h >>> 7) ^ (h >>> 4);
}

public V get(K key)
{
int hash = hash(key.hashCode())%table.length;
if (table[hash]==null) 
            return null;
else
{
LinkedHashEntry entry = table[hash];
while(true)
{
if (entry.getKey().equals(key))
{
return entry.getValue();
}
if (entry.next()==null) break;
entry = entry.next();
}
return null;
}
}

public void put(K key, V value)
{
if (elemCount > table.length*loadFactor) 
      resize();
int hash = hash(key.hashCode())%table.length;
if (table[hash]==null) 
         table[hash] = new LinkedHashEntry(key,value);
else
{
LinkedHashEntry entry = table[hash];
while(true)
{
if (entry.getKey().equals(key))
{
entry.setValue(value);
break;
}
if (entry.next()==null) break;
entry = entry.next();
}
entry.setNext(new LinkedHashEntry(key,value));
}
}
public void resize()
{
int newSize = table.length*1.5;
LinkedHashEntry[] newTable = new LinkedHashEntry[newSize];
// Move old element to new table
for (int i=0; i

No comments:

Post a Comment