Heap的Array实现法

来源:互联网 发布:qq风险软件 编辑:程序博客网 时间:2024/06/05 06:00

总论

Heap 逻辑上二叉树形状 物理上线性 array实现
这里做的是一个大根堆,涵盖方法有:
insert(): insert a new element to the heap
this invokes a help method called moveUp() cuz the inserted element may be larger than its parent, we need to move it up
初始建堆也就用insert()
remove(): remove the root of the heap
this invokes a help method called moveDown() cuz once the root is removed,
we use the rightmost element from the last level (logical view) to substitute the original root, and then this new root may not be the larger than its two children, so we need to move it down until it reaches its right position.
Def:
parent = (index - 1) / 2;
left = 2 * index + 1;
right = 2 * index + 2;
filling array from position 0

痛点

1.什么时候定义parent, left, right 这些变量?
何时用何时定,比如moveUp()用到parent所以在那里定义parent
2.moveUp 和 moveDown 是对 index进行操作
3.moveUp写法 出loop的条件 index <= 0
4.moveDown() 出Loop的条件 – index > size/2
5.粗心错,注意 remove() 补充root时要用array[size-1];然后size–; 当然也可以写 array[0]=array[–size];

代码

public class Heap {    private static final int maxSize = 10; //max size of this array is 10    private int[] array;      private int size;    public Heap() {        array = new int[maxSize];        size = 0;    }    /**     * @param newVal being inserted into this heap     */    public boolean insert(int newVal) {        if (size == maxSize) {            return false;        }        array[size] = newVal;        moveUp(size);        size++;        return true;    }    /**     * moveUp要跟parent比较所以parent在其中有定义     * @param index     */    private void moveUp(int index) {        int parent = (index - 1) / 2;        int bottom = array[index]; //temporarily save the current element         while (index > 0 && bottom > array[parent]) {            array[index] = array[parent]; //use parent element to substitute ele in current index            index = parent;            parent = (index - 1) / 2;        }        array[index] = bottom;    }    /**     * @return the heap root     */    public int remove() {        int root = array[0];        array[0] = array[size-1];        size--;        moveDown(0);        return root;    }    private void moveDown(int index) {        int largerChild;        int top = array[index]; //先存起来最后再放入        while (index < size / 2) {            int left = 2 * index + 1;            int right = 2 * index + 2;            largerChild = (right < size && array[left] < array[right]) ? right : left;            if (top >= array[largerChild]) {                break;            }            array[index] = array[largerChild];            index = largerChild;        }        array[index] = top;    }    public void display() {        System.out.print("The current heap is: ");        for (int i = 0; i < size; i++) {            System.out.print(array[i] + " ");        }        System.out.println();    }    public static void main(String[] args) {        /* construct a heap */        Heap heap = new Heap();        heap.insert(74);        heap.insert(51);        heap.insert(26);        heap.insert(3);        heap.insert(83);        heap.insert(99);        heap.display();        //remove root        heap.remove();        heap.display();        //add a new element 90        heap.insert(90);        heap.display();    }}

欢迎点评

原创粉丝点击