zoukankan      html  css  js  c++  java
  • 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());
    //
    //    }
    }
    
  • 相关阅读:
    mysql的一些不常用语句
    redis的使用1
    linux理论知识点(用于考试)
    服务器负载均衡数据同步的实现
    解决com.ibatis.sqlmap.client.SqlMapException: There is no statement named in this SqlMap
    cvc-complex-type.2.3: Element 'beans' cannot have character [children]
    Oracle11g服务详细介绍及哪些服务是必须开启的
    Oracle
    oracle 帐号scott被锁定 如何解锁
    记录
  • 原文地址:https://www.cnblogs.com/jun-ma/p/6376512.html
Copyright © 2011-2022 走看看