Java-堆操作

        使用Java实现堆操作

class Heap_Test {
    public static final int MAXSIZE = 500;

    class heap {
        int[] data;
        int MaxSize;
        boolean Flag;
        int len;
    }

    public boolean HeapInit(heap heap, boolean Flag) throws Exception {
        if (heap == null) {
            return false;
        }
        heap.data = new int[MAXSIZE];
        heap.Flag = Flag;
        heap.MaxSize = MAXSIZE;
        heap.len = 0;
        return true;
    }

    public void HeapInsert(heap heap, int InsertData) throws Exception{//向堆中插入一个元素
        if (heap == null) {
            return;
        }
        if(heap.len>=heap.MaxSize){
            throw new Exception("Insert:堆已满");
        }
        heap.data[heap.len] = InsertData;//先往数组尾插一个元素
        int Tuning = heap.len;//要调整的元素下标
        int Parent = 0;//双亲下标
        int temp = 0;//交换变量
        heap.len++;
        while (Tuning != 0) {
            Parent = (Tuning - 1) / 2;
            if (heap.Flag) {//小堆
                if (InsertData >= heap.data[Parent]) {
                    break;
                }
                temp = heap.data[Parent];
                heap.data[Parent] = heap.data[Tuning];
                heap.data[Tuning] = temp;
                Tuning = Parent;
            } else {//大堆
                if (InsertData <= heap.data[Parent]) {
                    break;
                }
                temp = heap.data[Parent];
                heap.data[Parent] = heap.data[Tuning];
                heap.data[Tuning] = temp;
                Tuning = Parent;
            }
        }
    }

    public int GetHeapTop(heap heap) throws Exception {//返回堆顶元素并删除
        if (heap == null) {
            throw new Exception("GetHeapTop时堆不存在");
        }
        if(heap.len == 0){
            throw new Exception("GetHeapTop时堆为空");
        }
        int ret = heap.data[0];//存放堆顶元素
        heap.len--;
        if (heap.len == 0) {
            return ret;
        }
        int TuningData = heap.data[heap.len];//存放需要调整的元素
        int i = 0;
        int j = 2 * i + 1;
        if(heap.Flag) {
            while (j <= heap.len - 1) {//找到另一个需要调整元素的位置
                if (heap.data[j + 1] < heap.data[j]) {//如果右孩子比左孩子小,就让j的下标变为右孩子的
                    j++;//现在j下标是较小的孩子的位置
                }
                if (TuningData < heap.data[j]) {//如果调整元素比左孩子和右孩子都小,就结束循环
                    break;
                }
                heap.data[i] = heap.data[j];//将孩子元素放在双亲位置
                i = j;//调整位置变为较小的孩子的位置
                j = 2 * i + 1;//新的调整位置变为新的位置的左孩子的位置
            }
            heap.data[i] = TuningData;//i就是最终找到的,另一个要换的位置
            return ret;
        }else {
            while (j <= heap.len - 1) {//找到另一个需要调整元素的位置
                if (heap.data[j + 1] > heap.data[j]) {//如果右孩子比左孩子大,就让j的下标变为右孩子的
                    j++;//现在j下标是较小的孩子的位置
                }
                if (TuningData > heap.data[j]) {//如果调整元素比左孩子和右孩子都大,就结束循环
                    break;
                }
                heap.data[i] = heap.data[j];//将孩子元素放在双亲位置
                i = j;//调整位置变为较大的孩子的位置
                j = 2 * i + 1;//新的调整位置变为新的位置的左孩子的位置
            }
            heap.data[i] = TuningData;//i就是最终找到的,另一个要换的位置
            return ret;
        }
    }

    public boolean IsEmptyHeap(heap heap)throws Exception{//检查是否是空堆
        if(heap == null){
            throw new Exception("IsEmpty:堆不存在");
        }
        if(heap.len == 0){
            return true;
        }
        return false;
    }

    public void DestoryHeap(heap heap)throws Exception{//销毁堆
        if(heap == null){
            throw new Exception("Destory:堆不存在");
        }
        heap.len = 0;
    }

    public int[] HeapSort(heap heap)throws Exception{
        if(heap == null){
            throw new Exception("Sort:堆不存在");
        }
            int[] ret = new int[heap.len];
            for(int i = 0;i<ret.length;i++) {
                ret[i] = GetHeapTop(heap);
            }
            return ret;
    }

}

public class Main {
    public static void main(String[] args) throws Exception {
        Heap_Test Test = new Heap_Test();
        Heap_Test.heap heap = new Heap_Test().new heap();
        int[] data = new int[]{1, 2, 3};
        Test.HeapInit(heap, true);//ture为小堆,false为大堆
        Test.HeapInsert(heap, 28);
        Test.HeapInsert(heap, 55);
        Test.HeapInsert(heap, 41);
        Test.HeapInsert(heap, 60);
        Test.HeapInsert(heap, 38);
        Test.HeapInsert(heap, 52);
        Test.HeapInsert(heap, 12);
        Test.HeapInsert(heap, 21);
//        while (heap.len != 0) {
//            System.out.println(Test.GetHeapTop(heap));
//        }
        System.out.println(Test.IsEmptyHeap(heap));//检查堆是否为空
        int[] AfrerSort = Test.HeapSort(heap);//堆排序
        for (int i : AfrerSort) {
            System.out.print(i);
        }
    }
}

猜你喜欢

转载自blog.csdn.net/qq_38449518/article/details/80310078
今日推荐