首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >堆数据结构重堆方法

堆数据结构重堆方法
EN

Stack Overflow用户
提问于 2013-07-30 21:14:31
回答 1查看 3.3K关注 0票数 4

我正在做我的作业,它是从文本文件中读取,将前10个单词存储在一个堆中。然后继续从文本文件中读取,如果单词小于堆的根,则替换它并重新堆整个堆。我的代码似乎大部分都在工作,但是我遇到了一些问题。

  • 有些单词,即使它们小于词根,也不会被交换。
  • 复词

我应该得到一个包含单词abandoning abandons abased abash abashed abashes abasing abate abatement abbe的堆

不管怎么说,abashes abashed abash abased abandons abandoning bewilderedly abandoning armful abandoning

到目前为止,我的代码如下:

代码语言:javascript
复制
public static void readFile() {
    BufferedReader reader;
    String inputLine;
    int counter = 0;

    try {
        reader = new BufferedReader(new FileReader(".\\src\\dictionary.txt"));
        while((inputLine = reader.readLine()) != null) {
            if(counter < 10) {
                heap.insert(inputLine);
                counter++;
            }

            if(inputLine.compareTo(heap.find(0)) < 0) {
                heap.change(0, inputLine);
            }
        }
    } catch (IOException e) {
        System.out.println("Error: " + e);
    }
}

public boolean insert(String value) {
    if(currentSize == maxSize) { return false; }

    Node newNode = new Node(value);
    heap[currentSize] = newNode;
    trickleUp(currentSize++);
    return true;
}

public void trickleUp(int index) {
    int parent = (index - 1) / 2;
    Node bottom = heap[index];

    while(index > 0 && heap[parent].getData().compareTo(bottom.getData()) < 0) {
        heap[index] = heap[parent];
        index = parent;
        parent = (parent - 1) / 2;
    }
    heap[index] = bottom;
}

public void trickleDown(int index) {
    int largerChild;
    Node top = heap[index];

    while(index < currentSize / 2) {
        int leftChild = 2 * index + 1;
        int rightChild = index + 1;

        if(rightChild < currentSize && heap[leftChild].getData().compareTo(heap[rightChild].getData()) < 0) {
            largerChild = rightChild;
        } else {
            largerChild = leftChild;
        }

        if(top.getData().compareTo(heap[largerChild].getData()) > 0) {
            break;
        }

        heap[index] = heap[largerChild];
        index = largerChild;
    }
    heap[index] = top;
}

public boolean change(int index, String newValue) {
    if(index < 0 || index >= currentSize) { return false; }

    String oldValue = heap[index].getData();
    heap[index].setData(newValue);

    if(oldValue.compareTo(newValue) < 0) {
        trickleUp(index);
    } else {
        trickleDown(index);
    }
    return true;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-04-08 08:55:22

如果使用这样的索引,就不会得到二叉树:

代码语言:javascript
复制
    int leftChild = 2 * index + 1;
    int rightChild = index + 1;

我想你是想写这个

代码语言:javascript
复制
    int leftChild = 2 * index + 1;
    int rightChild = 2 * index + 2;

所以这棵树会像这样

代码语言:javascript
复制
       0
     /   \
    1     2
   / \   / \
  3   4 5   6 
 / \
7   8 ... and so on

至于重复元素,据我所知,堆可以包含重复项,并且不支持重复删除。例如,这是一个有效的数字堆。

代码语言:javascript
复制
      10
    /    \
   9      8
  / \    / \
 5   7  7   6
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17957512

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档