首页 > 解决方案 > 在制作自己的可扩展散列项目时,如何让目录“指向”正确的存储桶?

问题描述

我已经阅读了有关创建可扩展哈希表Extendable Hashing的 wiki 页面。现在我正在尝试将其实现到一个程序中以更好地理解它,但我无法弄清楚如何将目录实际指向存储桶。

目录类:

int GlobalDepth = 1;
int directories[];

public Directory() {
    int temp = (int)Math.pow(2, GlobalDepth);

    loadDirectories(temp);

}

public void loadDirectories(int n) {
    for (int i = 0; i < n; i ++) {
        String BinaryKey = Integer.toBinaryString(i);
        String tempKey = BinaryKey.substring(0, this.GlobalDepth); // LSB
        int depthKey = Integer.parseUnsignedInt(tempKey);

        this.directories[i] = depthKey;

    }

}

public void increaseGlobalDepth() {
    this.GlobalDepth ++;

    loadDirectories((int)(Math.pow(2, this.GlobalDepth))); // This method might throw an error because i think im changing the array size illegally and should instead create a temp array and copy/equal that array to this.directories

}

桶类:

private SomeObject[] item; // Class I'm using to hold all the information of something
private int Key, MaxBucketsize, LocalDepth = 1;
//Bucket next;

public Bucket() {

}

public Bucket(int BucketSize) {
    this.item = new SomeObject[BucketSize]; // Initalises the number of items held in the bucket
    this.MaxBucketsize = BucketSize; // Stores the max number of items that can be held in the bucket

}


public SomeObject[] getObjects() {
    return this.item;

}

public boolean addObjects(int key, SomeObject item) {
    boolean inserted = false;

    for (int i = 0; i < this.MaxBucketsize; i ++) {
        if (this.item[i] == null) { // only inserts if there is an empty spot in the bucket
            this.item[i] = item;
            this.item[i].setKey(key);

            inserted = true;

            break;

        }

    }

    return inserted;

}

完成所有这些之后,我不确定现在如何将 2 链接在一起,就像在 wiki 页面中一样。

标签: javadata-structureshashhashtable

解决方案


推荐阅读