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());
    //
    //    }
    }
    
  • 相关阅读:
    sharpen和filter2D
    Changing the contrast and brightness of an image!(opencv对比度和亮度调节)
    人脸表情识别
    Pycharm下载和安装
    Anaconda下载与安装
    图像人脸检测+人眼检测 (opencv + c++)
    cv2.VideoWriter()指定写入视频帧编码格式
    python_openCV例程遇到error: (-215) !empty() in function cv::CascadeClassifier::detectMultiScale的简单解决方法
    图像处理库 Pillow与PIL
    IDE bulid构建隐藏了什么(预处理->编译->汇编->链接)
  • 原文地址:https://www.cnblogs.com/jun-ma/p/6376512.html
Copyright © 2011-2022 走看看