From 8ca99495432bba4b5f34e7f39864bac2570c02b7 Mon Sep 17 00:00:00 2001 From: email Date: Tue, 15 Dec 2020 20:44:35 +0800 Subject: [PATCH] test7 --- .../com/company/Heap.java" | 121 ++++++++++++++++++ .../com/company/Main.java" | 27 ++++ .../com/company/heap1.java" | 53 ++++++++ 3 files changed, 201 insertions(+) create mode 100644 "\346\230\257\344\275\234\344\270\232\345\221\220\357\274\201/com/company/Heap.java" create mode 100644 "\346\230\257\344\275\234\344\270\232\345\221\220\357\274\201/com/company/Main.java" create mode 100644 "\346\230\257\344\275\234\344\270\232\345\221\220\357\274\201/com/company/heap1.java" diff --git "a/\346\230\257\344\275\234\344\270\232\345\221\220\357\274\201/com/company/Heap.java" "b/\346\230\257\344\275\234\344\270\232\345\221\220\357\274\201/com/company/Heap.java" new file mode 100644 index 0000000..d1799e0 --- /dev/null +++ "b/\346\230\257\344\275\234\344\270\232\345\221\220\357\274\201/com/company/Heap.java" @@ -0,0 +1,121 @@ +package com.company; + +public class Heap{ + private int[] heap; + private int SIZE; + private int count; + + + Heap(int size){ + SIZE = size; + // reference: https://stackoverflow.com/questions/34827626/cannot-be-cast-to-ljava-lang-comparable + heap = new int[SIZE]; + count = 0; + } + //小顶堆的实现 +//入堆函数 + public void add (int item){ + if(count >= SIZE){ + // throw new Exception("Heap Overflow"); + System.out.println("Heap Full");//满了 + return; + } + heap[count] = item; + count++; + shiftUp(heap, count - 1); + + } + //向下调整 + private static void shiftDown(int[] array, int size, int index) { + int parent = index; + int child = parent * 2 + 1; + while (child < size) { + if (child + 1 < size && array[child + 1] > array[child]) { + child = child + 1; + } + if (array[child] < array[parent]) { + swap(array, parent, child); + } else { + break; + } + parent = child; + child = parent * 2 + 1; + } + } + + //向上调整 + private void shiftUp(int[] array, int index) { + int child = index; + int parent = (child - 1) / 2; + while (child > 0) { + if (heap[parent]>heap[child]) { + swap(heap, parent, child); + }else { + break; + } + child = parent; + parent = (child - 1) / 2; + } + } + + //交换 + private static void swap(int[] array, int x, int y) { + int temp = array[x]; + array[x] = array[y]; + array[y] = temp; + } + + + //出堆函数 + public void delete(int item){ + if (count == 0) { + System.out.println("count=0,heap是空的"); + } + for (int i = 0; i < heap.length; i++) { + if (heap[i]==item) { + for(int n =i;nheap[j]){ + int temp=heap[i]; + heap[i]=heap[j]; + heap[j]=temp; + } + } + } + + } + + +} \ No newline at end of file diff --git "a/\346\230\257\344\275\234\344\270\232\345\221\220\357\274\201/com/company/Main.java" "b/\346\230\257\344\275\234\344\270\232\345\221\220\357\274\201/com/company/Main.java" new file mode 100644 index 0000000..63ba3b1 --- /dev/null +++ "b/\346\230\257\344\275\234\344\270\232\345\221\220\357\274\201/com/company/Main.java" @@ -0,0 +1,27 @@ +package com.company; + +public class Main { + + public static void main(String[] args) { + // TODO 自动生成的方法存根 + Heap py = new Heap(10); + py.add(9); + py.add(8); + py.add(7); + py.add(5); + py.add(3); + py.add(2); + py.add(11); + py.print(); + System.out.println("删除数据8:"); + py.delete(8); + py.print(); + System.out.println("删除数据5:"); + py.delete(5); + py.print(); + System.out.println("实现堆排序"); + py.bubbleSort(); + py.print(); + } + +} \ No newline at end of file diff --git "a/\346\230\257\344\275\234\344\270\232\345\221\220\357\274\201/com/company/heap1.java" "b/\346\230\257\344\275\234\344\270\232\345\221\220\357\274\201/com/company/heap1.java" new file mode 100644 index 0000000..a39ddf4 --- /dev/null +++ "b/\346\230\257\344\275\234\344\270\232\345\221\220\357\274\201/com/company/heap1.java" @@ -0,0 +1,53 @@ +package com.company; + +public class heap1 { + + public static void main(String[] args) { + int i,size,data[]={0,5,6,10,8,3,2,19,9,11}; //原始数组内容 + size=data.length; + + System.out.print("原始数组:"); + + for(i=1;i0;i--) //建立堆积树节点 + ad_heap(data,i,size-1); + System.out.print("\n堆积内容:"); + for(i=1;idata[j+1]) //小顶堆的比较 + if(data[j]=data[j]) //若树根较大,结束比较过程 + post=1; + else + { + data[j/2]=data[j]; //若树根较小,则继续比较,这里将最大子节点赋值给父节点 + j=2*j; + } + } + data[j/2]=tmp; //指定树根为父节点 + } +} \ No newline at end of file -- Gitee