Hash Table With No Collisions When You Know the Keys

Title picture

Hash Tables, Hashing and Standoff Handling

Visualizing the hashing process

A hash table is a data structure that implements an associative array abstract information type, a structure that can map keys to values. A hash table uses a hash function to compute an index, also called a hash code, into an array of buckets or slots, from which the desired value tin exist institute. — Wikipedia

Separate Chaining technique to handle collisions
          // this is the custom generic node course
public grade HashNode<K,V> {

1000 key;
5 value;
HashNode<K,V> next;

public HashNode(M key,5 value){
this.key = key;
this.value = value;
}
}

          public form CustomHashMap<Yard,V> {
private ArrayList<HashNode<Thou,V>> bucketArray;
private int numBucket; //current capacity of list
private int size; // current size of list

public CustomHashMap(){
bucketArray = new ArrayList<>();
size = 0;
numBucket = 10;

//we create empty chains
for(int i=0;i<numBucket;i++){
bucketArray.add(naught);
}
}

public int getSize(){
return size;
}

public boolean isEmpty(){
return size==0;
}

individual int getBucketIndex(K key){
int hashCode = key.hashCode(); //built in java hash func
int index = hashCode%numBucket;//modernistic to traverse circularly
return Math.abs(alphabetize); // since the key could be negative
}

public V remove(K key){
//we find the required alphabetize
int bucketIndex = getBucketIndex(key);
HashNode<1000,Five> head = bucketArray.get(bucketIndex);
HashNode<K,Five> prev = null;
while(caput!=zip){
if(head.key.equals(key)){
break;
}
prev = caput;
head = head.next;
}
if(caput==null){
return null;
}
size--;
if(prev!=nada){
prev.adjacent = caput.side by side;
}
//here nosotros are handling if key is first chemical element when prev Volition be null
else{
bucketArray.gear up(bucketIndex,head.next);
}
return head.value;
}

public Five get(K cardinal){
int bucketIndex = getBucketIndex(key);
HashNode<G,V> head = bucketArray.get(bucketIndex);
while(head!=null){
if(head.key.equals(key)){
pause;
}
head = head.next;
}
if(head==nix){
render null;
}
return head.value;
}

//add key value pair to hash tabular array
public void add(K cardinal, V value){
int bucketIndex = getBucketIndex(key);
HashNode<K,Five> head = bucketArray.get(bucketIndex);

//if fundamental already exists, we update the value
while(head!=cipher){
if(caput.key.equals(cardinal)){
head.value = value;
return;
}
head = head.side by side;
}
//insertion at head for o(1)
size++;
HashNode<K,V> newNode = new HashNode<>(central,value);
caput = bucketArray.get(bucketIndex);
newNode.adjacent = caput;
bucketArray.set up(bucketIndex, newNode);

//now to cheque for the load factor
if((1.0*size)/numBucket>=.7){
size = 0;
ArrayList<HashNode<Yard,V>> temp = bucketArray;
bucketArray = new ArrayList<>();
numBucket = ii*numBucket;

//we create the chains once more outset
for(int i=0;i<numBucket;i++){
bucketArray.add together(null);
}

//finally we re-create from one-time to new
for(HashNode<G,V> node:temp){
while(node!=naught){
//recursively
add(node.fundamental,node.value);
node = node.side by side;
}
}
}
}
}

          public course LinearProbing{
private String [] keys;
individual String [] values;
individual int size;
private int capacity;

public LinearProbing(int capacity){
keys = new String[chapters];
values = new String[capacity];
this.size = 0;
this.capacity = chapters;
}

public void makeEmpty(){
size = 0;
keys = new Cord[chapters];
values = new String[chapters];
}

public int getSize(){
return size;
}

public int getCapacity(){
return capacity;
}

public boolean isFull(){
return size==capacity;
}

public boolean isEmpty(){
return size==0;
}

private int getHash(String fundamental){
return Math.abs(cardinal.hashCode()%capacity);
}

public void insert(String central,String value){
if(isFull()){
System.out.println("No room to insert");
return;
}
int temp = getHash(key);
int i = temp;
do{
if(keys[i]==zilch || keys[i].equals("DELETED")){
keys[i] = key;
values[i] = value;
size++;
return;
}
if(keys[i].equals(key)){
values[i] = value;
return;
}
i = (i+1)%capacity;
}while(i!=temp);
}

public String get(String cardinal){
int i = getHash(key);
while(keys[i]!=aught){
if(keys[i].equals(central)){
return values[i];
}
i = (i+ane)%capacity;
}
return "not found";
}

public boolean contains(String primal){
return get(key)!=zilch;
}

public void remove(String cardinal){
if(!contains(primal)){
return;
}
int i = getHash(key);
while(!keys[i].equals(key)){
i = (i+1)%capacity;
}
keys[i] = values[i] = "DELETED";
size--;
}
}

meekswitoo1972.blogspot.com

Source: https://medium.com/codex/hash-tables-hashing-and-collision-handling-8e4629506572

0 Response to "Hash Table With No Collisions When You Know the Keys"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel