简体   繁体   English

最大堆插入无法正常工作

[英]Max Heap Insertion Not Working Properly

I am trying to implement a heap in java. 我正在尝试在Java中实现堆。 I am testing the insertion. 我正在测试插入。 When I insert the elements and print out the tree at the insertion method in my Heaper class, the output I receive is 当我在Heaper类中的插入方法中插入元素并打印出树时,收到的输出是

[1] [1]

[1, 16] [1,16]

[16, 1, 1, 16, 4] [16、1、1、16、4]

[16, 4, 1, 1, 16, 1, 4, 23] [16、4、1、1、1、16、1、4、23]

Any ideas as to why this is happening? 有什么想法为什么会这样? Thanks! 谢谢!

Main Method 主要方法

public class HeapTester {


public static void main(String[] args) {
   Heaper heap = new Heaper();
   heap.insert(1);
   heap.insert(16);
   heap.insert(4);
   heap.insert(23);


}}

Heaper Class 疗愈班

    package heaptester;

    import java.util.ArrayList;


    public class Heaper {
     private ArrayList<Integer> tree;



    /**

    * Constructs an empty heap

    */

public Heaper()

{

tree=new ArrayList<Integer>();

}

public boolean isEmpty()

{

if (tree.size() == 0)

{

return true;

}

return false;



}



public void insert(Integer obj)

{

if (isEmpty())

{

tree.add(obj);
System.out.println(tree);

}

else

{



int place = tree.size()-1;

int parent = (place -1)/2;



while(parent >= 0 && (tree.get(place) > tree.get(parent)))

{

swap(tree, place, parent);

place=parent;

parent = (place -1)/2;

}
tree.add(obj);
System.out.println(tree);

}

}

public Integer remove()

{

Integer root = tree.get(0);

tree.set(0 , tree.get(tree.size()-1));
System.out.println(tree);
reheapify(0);
System.out.println(tree);
return root;

}

public Integer peek()

{

return tree.get(0);

}

public int size()

{

return tree.size();

}

/**

* Swaps a parent and child elements of this heap at the specified indices

* @param place an index of the child element on this heap

* @param parent an index of the parent element on this heap

*/

private void swap(ArrayList<Integer> tree, int place, int parent)

{

Integer temp=tree.get(place);

tree.add(place, tree.get(parent));

tree.add(parent, temp);


}

/**

* Rebuilds the heap to ensure that the heap property of the tree is preserved.

* @param root the root index of the subtree to be rebuilt

*/

private void reheapify(int root)

{

if (!isLeaf(root))

{

if ( (tree.get(root) < tree.get(leftChild(root))) || (tree.get(root) < tree.get(rightChild(root))))

{

if (tree.get(leftChild(root)) > tree.get(rightChild(root)))

{

swap(tree,root, leftChild(root));

reheapify(leftChild(root));

}else

{

swap(tree,root, rightChild(root));

reheapify(rightChild(root));

}

}

}

}

private int leftChild(int pos)

{

return (2 * pos);

}

private int rightChild(int pos)

{

return (2 * pos) + 1;

}

private boolean isLeaf(int pos)

{

if (pos >= (size() / 2) && pos <= size())

{

return true;

}

return false;

}

}

In your insert method, you should add the obj value to the tree before getting the size of the tree. 在您的insert方法中,应该在获取树的大小之前将obj值添加到树中。

public void insert(Integer obj)

{

 //...
else
{
    tree.add(obj)
    int place = tree.size()-1;
    int parent = (place -1)/2;

    while(parent >= 0 && (tree.get(place) > tree.get(parent)))
    {
        swap(tree, place, parent);
        place=parent;
        parent = (place -1)/2;
    }
    System.out.println(tree);

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM