Hash table chain address method

The chain address method is better than the open address method.

Efficiency of hashing:

1. Linear detection: successful search P=(1+1/(1-L))/2; unsuccessful search is P=(1+1/(1-L)^2)/2; where P is the detection sequence , L is the filling factor

2. Secondary detection and re-hashing: successful search P=-log2(1-L)/L; unsuccessful search P=1/(1-L)

3. Chain address method: successful search and insertion of P=1+L/2; unsuccessful search of P=1+L

For example, when the loading factor L=1/2, the linear detection requires 1.5 times and 2.5 times on average for successful and unsuccessful searches; the secondary detection is 2 times and 2 times; and for the chain address method, this is a linear function, and the search is very quick. As the filling factor becomes larger, the performance of the chain-address method decreases the slowest.


import java.util.Random;

//Chain address method
class Link {
    int data;
    Link next;

    public Link(int data) {
        this.data = data;
    }
}

//Ordered linked list has no effect on successful search, but it can Reduce the time of not finding
class SortedLink {
    private Link first;

    public SortedLink() {
        first = null;
    }

    public void insert(int key) {
        Link current = first;
        Link previous = null;
        Link link = new Link(key);
        while (current != null && key > current.data) {
            previous = current;
            current = current.next;
        }
        if (previous == null)// first is null or the first key is the smallest
            first = link;

        else
            previous.next = link;
        link.next = current;
    }

    public boolean delete(int key) {
        Link current = first;
        Link previous = null;
        while (current != null && key >= current.data) {
            if (current.data == key) {
                if (previous == null)// if the delete is the first
                    first = first.next;
                else
                    previous.next = current.next;
                return true;
            }
            previous = current;
            current = current.next;
        }
        return false;
    }

    public Link find(int key) {
        Link current = first;
        while (current != null && key >= current.data) {
            if (key == current.data)
                return current;
            current = current.next;
        }
        return null;
    }

    public void displayLink() {
        Link current = first;
        while (current != null) {
            System.out.print(current.data + "/");
            current = current.next;
        }
        System.out.print("   ");
    }
}

public class HashChain {
    private SortedLink[] linkArray;
    private int size;

    public HashChain(int size) {
        this.size = size;
        linkArray = new SortedLink[size];
        for (int i = 0; i < size; i++)
            linkArray[i] = new SortedLink();
    }

    public void insert(int key) {
        int hashval = hashfunc(key);
        linkArray[hashval].insert(key);
    }

    public void find(int key) {
        int hashval = hashfunc(key);
        Link link = linkArray[hashval].find(key);
        if (link != null)
            System.out.println(link.data);
    }

    public void delete(int key) {
        int hashval = hashfunc(key);
        boolean bool = linkArray[hashval].delete(key);
        System.out.println(bool);
    }

    private int hashfunc(int key) {
        return key % size;
    }

    public void display() {
        for (int i = 0; i < size; i++)
            linkArray[i].displayLink();
        System.out.println();
    }

    public static void main(String[] args) {
        HashChain hash = new HashChain(23);
        Random rand = new Random();
        int[] array = new int[10];
        for (int i = 0; i < 10; i++)
            array[i] = rand.nextInt(1000);
        for (int n : array)
            hash.insert(n);
        hash.display();
        int temp = array[3];
        hash.find(temp);
        hash.delete(temp);
        hash.delete(temp);
        hash.display();
        hash.find(temp);
        hash.insert(200);
        hash.display();
    }
}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325202620&siteId=291194637