java——最大堆 MaxHeap


 使用數組來實現最大堆

堆是平衡二叉樹

import Date_pacage.Array;

public class MaxHeap<E extends Comparable <E>> {
    private Array<E> data;
    public MaxHeap(int capacity) {
        data = new Array<>(capacity);
    }
    public MaxHeap() {
        data = new Array<>();
    }
    //heapify:講任意數組整理成堆的形狀 O(n)
    public MaxHeap(E[] arr) {
        data = new Array<>(arr);
        //從最后一個非葉子節點開始,逐個siftDown
        for(int i = parent(arr.length - 1) ; i >= 0 ; i-- ) {
            siftDown(i);
        }
    }
    public int size() {
        return data.getSize();
    }
    public boolean isEmpty() {
        return data.isEmpty();
    }
    //返回完全二叉樹的數組(索引從0 開始)表示中,一個索引所表示的元素的父親節點的索引
    private int parent(int index) {
        if(index == 0)
            throw new IllegalArgumentException("index-0 doesn't have parent.");
        return (index - 1) / 2;
    }
    
    private int leftChild(int index) {
        return index * 2 +1;
    }
    private int rightChild(int index) {
        return index * 2 + 2;
    }
    //向堆中添加元素
    public void add(E e) {
        data.addLast(e);
        siftUp(data.getSize() - 1);
    }
    private void siftUp(int k) {
        while(k > 0 && data.get(parent(k)).compareTo(data.get(k)) < 0){
            data.swap(k, parent(k));
            k = parent(k);
        }
    }
    //看堆中最大元素
    public E findMax() {
        if(data.getSize() == 0)
            throw new IllegalArgumentException("Can not findMax when heap is empty");
        return data.get(0);
    }
    //取出元素,只能取出最大的那個元素
    public E extractMax(){
        E ret = findMax();
        
        data.swap(0,  data.getSize() - 1);
        data.removeLast();
        siftDown(0);
        return ret;
    }
    private void siftDown(int k) {
        while(leftChild(k) < data.getSize()) {
            int j = leftChild(k);
            if(j + 1 < data.getSize() && 
                    data.get(j + 1).compareTo(data.get(j)) > 0) {
                j ++;
            }
            if(data.get(k).compareTo(data.get(j)) >= 0)
                break;
            data.swap(k,  j);
            k = j;
        }
    }
    // 取出堆中最大的元素,並且替換成元素e
    public E replace(E e) {
        E ret = findMax();
        data.set(0, e);
        siftDown(0);
        return ret;
    }

    
    
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM