• Implement heap using Java


    public class HeapImpl {
        private int CAPACITY = 10;
        private int size = 0;
        private int[] data;
    
        public HeapImpl() {
            data = new int[CAPACITY];
        }
    
        //some helper methods
        private int getLeftChildIndex(int index) {
            return index * 2 + 1;
        }
    
        private int getRightChildIndex(int index) {
            return index * 2 + 2;
        }
    
        private int getParentIndex(int index) {
            return (index - 1) / 2;
        }
    
        private boolean hasLeftChild(int index) {
            return getLeftChildIndex(index) < size;
        }
    
        private boolean hasRightChild(int index) {
            return getRightChildIndex(index) < size;
        }
    
        private boolean hasParent(int index) {
            return getParentIndex(index) >= 0;
        }
    
        private void ensureCapacity() {
            if (size == CAPACITY) {
                data = Arrays.copyOf(data, CAPACITY * 2);
                CAPACITY = CAPACITY * 2;
            }
        }
    
        private void swap(int index1, int index2) {
            int temp = data[index1];
            data[index1] = data[index2];
            data[index2] = temp;
        }
    
        public int poll() {
            if (size == 0) {
                throw new IllegalStateException();
            }
            int item = data[0];
            data[0] = data[size - 1];
            heapifyDown();
            return item;
        }
    
        private void heapifyDown() {
            int index = 0;
            while (hasLeftChild(index)) {
                int smallest = getLeftChildIndex(index);
                if (hasRightChild(index) && data[getRightChildIndex(index)] < data[smallest]) {
                    smallest = getRightChildIndex(index);
                }
                if (data[index] < data[smallest]) {
                    break;
                } else {
                    swap(smallest, index);
                    index = smallest;
                }
            }
        }
    
        public void add(int item) {
            ensureCapacity();
            data[size++] = item;
            heapifyUp();
        }
    
        private void heapifyUp() {
            int index = size - 1;
            while (hasParent(index) && data[getParentIndex(index)] > data[index]) {
                swap(index, getParentIndex(index));
                index = getParentIndex(index);
            }
        }
    //    public static void main(String [] args){
    //        HeapImpl heap = new HeapImpl();
    //        heap.add(2);
    //        heap.add(4);
    //        heap.add(5);
    //        heap.add(0);
    //        heap.add(9);
    //        heap.add(100);
    //        heap.add(15);
    //        System.out.println(heap.poll());
    //        System.out.println(heap.poll());
    //        System.out.println(heap.poll());
    //        System.out.println(heap.poll());
    //
    //    }
    }
    
  • 相关阅读:
    LA 2038 Strategic game(最小点覆盖,树形dp,二分匹配)
    UVA 10564 Paths through the Hourglass(背包)
    Codeforces Round #323 (Div. 2) D 582B Once Again...(快速幂)
    UVALive 3530 Martian Mining(贪心,dp)
    UVALive 4727 Jump(约瑟夫环,递推)
    UVALive 4731 Cellular Network(贪心,dp)
    UVA Mega Man's Mission(状压dp)
    Aizu 2456 Usoperanto (贪心)
    UVA 11404 Plalidromic Subsquence (回文子序列,LCS)
    Aizu 2304 Reverse Roads(无向流)
  • 原文地址:https://www.cnblogs.com/jun-ma/p/6376512.html
Copyright © 2020-2023  润新知