Interview must ask: shredded hash table

1. Shred the hash table?

Hash table includes insert, delete, and include operations.
Taking insert operation as an example, the tearing process is mainly divided into the following steps:
1. Convert the inserted object to int type and call the hashCode() function;
2. Convert to a legal subscript (hash function: hash(key)=key%capacity), such as: int index=hashValue%array.length;
3. Traverse the linked list at the index position to determine whether the key is in the element
4. Put The key is installed in the node and inserted into the corresponding linked list.
5. Maintain the number of elements
6... By maintaining the load factor, the lower load factor is maintained

These six steps are indispensable. During the interview, the main purpose is to accurately describe these six steps with your own. With these six steps in mind, the code is easier to write.

Second, the tearing process

1. Insert

The insertion process is the above six steps. It is mainly necessary to pay attention to the expansion of the original linked list elements directly. The expansion method is written in detail later.

public class MyHashTable {
    
    
    //实现一个HashTable首先需要一个数组
    private Node[] array=new Node[11];
    //维护哈希表中所有元素的个数
    private int size;

    //插入操作
    public boolean insert(Integer key){
    
    
        //1.把对象转为int类型
        int hashValue=key.hashCode();


        //2.把hashValue转成合法的下标
        int index=hashValue%array.length;

        //3.遍历index位置处的链表,确定key在不在元素中
        Node current=array[index];
        while(current!=null){
    
    
            if(key.equals(current.key)){
    
    
                return false;
            }

            current=current.next;
        }


        //4.把key装进结点中,并插入到对应的链表中
        Node node=new Node(key);
        node.next=array[index];
        array[index]=node;

        //5.维护元素的个数
        size++;

        //6.通过维护负载因子,进而维护较低的负载因子
        if(size/array.length*100>75){
    
    
            dilatation();
        }
        return true;
    }

2. Delete

The deletion process also includes the above six steps. The difference is that a predecessor node of the element to be deleted is also defined. The error-prone point is: when prev is null, the head node is deleted, and this step cannot be ignored.

 public boolean  remove(Integer key){
    
    
        //1.采用hashCode转为int类型
        int hashValue=key.hashCode();

        //2.得到合法的下标
        int index=hashValue%array.length;

        Node prev=null;
        Node current=array[index];
        while(current!=null){
    
    
            //删除操作
            if(key.equals(current.key)){
    
    
                if(prev!=null){
    
    
                    prev.next=current.next;
                }else{
    
    
                    //prev为null,删除的是头节点
                    array[index]=current.next;
                }
                size--;
                return true;

            }
            prev=current;
            current=current.next;

            }
        return false;

    }

3. Contains

public boolean contains(Integer key){
    
    
        int hashValue=key.hashCode();
        int index=hashValue%array.length;
        Node current=array[index];
        while(current!=null){
    
    
            if(key.equals(current.key)){
    
    
                return true;
            }
            current=current.next;
        }
        return false;

    }

4. Expansion

When expanding the capacity, the original elements must be moved to the new array, but cannot be moved according to the original linked list, because the index of the saved element is related to the length of the array, and the length of the array changes, the index will also change, so it needs to be recalculated The subscript of each element.

public void dilatation(){
    
    
        Node[] newArray=new Node[array.length*2];
   
        for(int i=0;i<array.length;i++){
    
    
            Node current=array[i];
            while (current!=null){
    
    
                Integer key=current.key;
                int hashValue=key.hashCode();
                int index=hashValue%newArray.length;
                //头插
                Node node=new Node(key);
                node.next=newArray[index];
                newArray[index]=node;

                current=current.next;

            }
        }
        array=newArray;
    }

Guess you like

Origin blog.csdn.net/m0_46551861/article/details/109632979