Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/383.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 二进制堆下堆方法无法正常工作_Java_Data Structures_Binary Heap - Fatal编程技术网

Java 二进制堆下堆方法无法正常工作

Java 二进制堆下堆方法无法正常工作,java,data-structures,binary-heap,Java,Data Structures,Binary Heap,我正在尝试用Java编写一些数据结构,但二进制堆有问题,如果您能帮助我,我将非常感激 首先,我为将存储在堆中的对象创建了一个类 public class Person { private String name; private String surname; private int age; public Person(String name, String surname, int age) { this.name = name; this.surname = surna

我正在尝试用Java编写一些数据结构,但二进制堆有问题,如果您能帮助我,我将非常感激

首先,我为将存储在堆中的对象创建了一个类

public class Person {

private String name;
private String surname;
private int age;

public Person(String name, String surname, int age) {
    this.name = name;
    this.surname = surname;
    this.age = age;
}

public void setName(String n) {
    name = n;
}

public void setSurname(String n) {
    surname = n;
}

public void setAge(int a) {
    age = a;
}

public String getName() {
    return name;
}

public String getSurname() {
    return surname;
}

public int getAge() {
    return age;
}
}
然后我为堆节点创建了一个类,其中包含Person对象以及节点优先级

public class BinNode {

private Person element;
private int priority;

BinNode(Person element, int priority) {
    this.element = element;
    this.priority = priority;
}

public boolean isEmpty() {
    if (element == null)
        return true;
    else
        return false;
}

public Person getElement() {
    return element;
}

public int getPriority() {
    return priority;
}

@Override
public String toString() {
    return "Data: " + element.getName() + " " + element.getSurname() + 
            ". Age: " + element.getAge() + "." + " Priority: " + priority + ".";
}   
}
最后,我为二进制堆创建了一个类

public class BinHeap {

public BinNode[] tab;
public int counter;

BinHeap(Person root, int priority) {
    counter = 1;
    tab = new BinNode[10];
    BinNode rt = new BinNode(root, priority);   
    tab[counter++] = rt;
}

public void upheap(int i) {
    while((i > 1) && (tab[i].getPriority() < tab[i-1].getPriority())) {
        BinNode temp = tab[i-1];
        tab[i-1] = tab[i];
        tab[i] = temp;
        i--;
    }
}

//error somewhere here
public void downheap(int i) {
    int l = 2 * i;
    int r = 2 * i + 1;      
    while (r < counter-1 && l < counter) {
        l = 2 * i;
        r = 2 * i + 1;
        if(tab[l].getPriority() < tab[r].getPriority()) {
            BinNode temp = tab[i];
            tab[i] = tab[l];
            tab[l] = temp;
            i = l;
        } else if (tab[l].getPriority() >= tab[r].getPriority()) {
            BinNode temp = tab[i];
            tab[i] = tab[r];
            tab[r] = temp;
            i = r;
        }   
    }   
}

public void insert(Person p, int priority) {
    BinNode node = new BinNode(p, priority);
    tab[counter++] = node;
    upheap(counter-1);
}

public BinNode findMin() {
    return tab[1];
}

//or here   
public void delMin() {
    tab[1] = tab[counter];
    tab[counter--] = null;
    downheap(1);
}

public void showTree() {
    for (int i = 1; i < tab.length; i++) {
        if(tab[i] != null) {
        System.out.println(i + " - " + tab[i].toString());
        }
    }
}
}
生成以下输出:

1 - Data: Mary Green. Age: 20. Priority: 2.

1 - Data: Mary Green. Age: 20. Priority: 2.
2 - Data: Mark Yellow. Age: 60. Priority: 4.
3 - Data: John Dark. Age: 12. Priority: 12.

1 - Data: Adam Bright. Age: 30. Priority: 1.
2 - Data: Mary Green. Age: 20. Priority: 2.
3 - Data: Mark Yellow. Age: 60. Priority: 4.
4 - Data: John Dark. Age: 12. Priority: 12.

root: Data: Adam Bright. Age: 30. Priority: 1.

1 - Data: Adam Bright. Age: 30. Priority: 1.
2 - Data: Mary Green. Age: 20. Priority: 2.
3 - Data: Mark Yellow. Age: 60. Priority: 4.
4 - Data: Martha King. Age: 33. Priority: 5.
5 - Data: John Dark. Age: 12. Priority: 12.
6 - Data: Greg Pawn. Age: 1. Priority: 15.

1 - Data: Mary Green. Age: 20. Priority: 2.
2 - Data: Martha King. Age: 33. Priority: 5.
3 - Data: Mark Yellow. Age: 60. Priority: 4.
5 - Data: John Dark. Age: 12. Priority: 12.
6 - Data: Greg Pawn. Age: 1. Priority: 15.
如您所见,只要我们将新节点放入堆中,一切都会正常工作。然而,当我们想要删除根节点时,downheap方法(或者可能是delMin方法)不能正常工作

如您所见,堆中优先级为5的节点“Martha King”比优先级为4的“Mark Yellow”节点高。

编辑:我似乎不理解二进制堆作为数组实现时的行为。所以上面粗体的句子是不正确的。原始输出是错误的,因为堆失去了完整性属性,因为数组[4]是空的


我就是搞不懂这个。你能帮帮我吗?非常感谢。另外,如对编码风格等有一般性意见,将不胜感激。这是我关于堆栈溢出的第一篇文章,如果我把事情搞砸了,我很抱歉

在UPCAP中,您将比较新子级与父级的值,而不是与i-1的值

int parent(int i)
{
    return i/2;
}

public void upheap(int i) {
    while((i > 1) && (tab[i].getPriority() < tab[parent(i)].getPriority())) {
        BinNode temp = tab[parent(i)]; // it should not be i-1 but parent of i that is i/2
        tab[parent(i)] = tab[i];
        tab[i] = temp;
        i = parent(i);
    }
}
下面是采用上述更改后将生成的输出和堆树的结果图像。结果是正确的。

您可以参考本文以获取代码参考

<>代码是C++的,但是对于java学生来说,使用类来完成代码是很清楚的。
MinHeapify是其中的下堆函数,而insertKey()中的循环则是上堆函数。计数器也从0开始,而不是从1开始。

我认为您在计数器变量中保留了下一个空索引。实施@Adarsh Anurag纠正。当删除时,您将在这个空索引之前获取元素,而不是之后

public void delMin() {
    tab[1] = tab[--counter];
    tab[counter] = null;
    downheap(1); 
}

@greg_pio现在检查一下,朋友是的,现在可以正常工作了。谢谢你的图表,它帮助我理解了它。Łukasz-也感谢您的反馈!
public void downheap(int i) {
    int l = 2 * i;
    int r = 2 * i + 1; 
    int small = i;     
    if(l<counter && tab[l].getPriority() < tab[small].getPriority())
         small = l;
    if(r<counter && tab[r].getPriority() < tab[small].getPriority())
         small = r;
    if(small!=i)
    {
        BinNode temp = tab[i];
        tab[i] = tab[small];
        tab[small] = temp;
        downheap(small); //you need to call downheap again on the small to find new pos for i (i became small after swap). 
        //i = 1, when you call downheap after delete. The node at i=1 will move down if it is bigger.
    }
}
public void delMin() {
    tab[1] = tab[--counter];
    tab[counter] = null;
    downheap(1);
}
public void delMin() {
    tab[1] = tab[--counter];
    tab[counter] = null;
    downheap(1); 
}