Question

Java Language Implement hash tables using open addressing and chaining Test your implementation with sample values.

Java Language


Implement hash tables using open addressing and chaining

Test your implementation with sample values.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please find the code below:

Custom Node class:

package different;

//custom Node class
public class Node<K,V> {
    //key
    K key;
    //value
    V value;
    //next node
    Node<K,V> next;

    //default constructor
    public Node(K key, V value){
        this.key = key;
        this.value = value;
    }
}

Custom Map class:


import java.util.ArrayList;


public class MyMap<K,V> {
    //list to store values
    ArrayList<Node<K,V>> myList;
    //capacity of list
    int capacity;
    //size of the list
    int size;
    //default constructor
    public MyMap(){
        myList = new ArrayList<>();
        //intial capacity
        capacity = 10;
        //intital size;
        size = 0;

        for(int i=0;i<capacity;i++){
            myList.add(null);
        }
    }
    //get size
    public int size(){
        return size;
    }
    //check if it's empty
    public boolean isEmpty(){
        return myList.isEmpty();
    }
    //find index
    int getIndex(K key){
        return key.hashCode() % capacity;
    }
    //add a key
    public void add(K key, V value){
        //get the first index for the key
        int index = getIndex(key);
        //get the node at index
        Node<K,V> chain_head = myList.get(index);

        //check if the key is present at that value
        while(chain_head!=null){
            if(chain_head.key.equals(key)){
                //update the value
                chain_head.value = value;
                return;
            }
            chain_head = chain_head.next;
        }

        //Else insert key into the cahin
        size++;
        chain_head = myList.get(index);
        Node<K,V> newNode = new Node<>(key, value);
        newNode.next = chain_head;

        myList.set(index,newNode);


        //If list is full by 80% then increase the capacity of the list

        if((size*1.0)/capacity >= 0.80){
            ArrayList<Node<K,V>> newList = myList;
            myList = new ArrayList<>();
            capacity = 2 * capacity;
            size = 0;
            for(int i=0;i<capacity;i++){
                myList.add(null);
            }

            for(Node<K,V> node : newList){
                while(node!=null){
                    add(node.key,node.value);
                    node = node.next;
                }
            }
        }
    }

    //get the value for given key
    public V get(K key){
        //find the fitst node in chain for given key
        int index = getIndex(key);
        Node<K,V> chain_head = myList.get(index);

        while(chain_head!=null){
            if(chain_head.key.equals(key)){
                return chain_head.value;
            }
            chain_head = chain_head.next;
        }

        return null;
    }

    //remove value for given key
    public V remove(K key){
        //find the fitst node in chain for given key
        int index = getIndex(key);
        Node<K,V> chain_head = myList.get(index);

        //serach for the given key node
        Node<K,V> prev = null;
        while(chain_head!=null){
            if(chain_head.key.equals(key)){
                break;
            }
            prev = chain_head;
            chain_head = chain_head.next;
        }

        if(chain_head==null){
            return null;
        }

        size--;

        if(prev!=null){
            prev.next = chain_head.next;
        }
        else{
            myList.set(index,chain_head.next);
        }

        return chain_head.value;
    }

    public static void main(String[] args) {
        MyMap<String,Integer> myMap = new MyMap<>();
        myMap.add("One",1);
        myMap.add("Two",2);
        myMap.add("Three",3);
        myMap.add("Four",4);
        System.out.println("Size: " + myMap.size());
        System.out.println("Removed Value: " + myMap.remove("Three"));
        System.out.println("Size: " + myMap.size());
        System.out.println("Is Empty?: " + myMap.isEmpty());
        System.out.println("Value with Key \"One\": " + myMap.get("One"));
    }

}

Output:

C:\Program Files\Javaljakl. 8.0-151\bin\java Size: 4 Removed Value: 3 11 | size : 3 Is Empty?: false Value with Key One 1

Add a comment
Know the answer?
Add Answer to:
Java Language Implement hash tables using open addressing and chaining Test your implementation with sample values.
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT