微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在制作我自己的可扩展散列项目时,如何让目录“指向”正确的存储桶?

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

我已经阅读了有关创建可扩展哈希表 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;

}

做完这一切之后,我不知道如何像在 wiki 页面中那样将这两个链接在一起。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。