最大堆最小堆的实现
最大堆最小堆的实现
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/**
* 文件名:BinaryHeap.java
* 时间:2014年11月3日下午7:15:34
* 作者:修维康
*/
package chapter6;
import java.util.*;
/**
* 类名:BinaryHeap 说明:建立一个最小堆
*/
class MinHeap<AnyType extends Comparable<? super AnyType>> {
private int currentSize;
private static final int DEFAULT_CAPACITY = 10;
private AnyType[] array;
public MinHeap(AnyType[] items) {
currentSize = items.length;
array = (AnyType[]) new Comparable[currentSize + 1];
int i = 1;
for (AnyType item : items)
array[i++] = item;
buildHeap();
}
/**
* 方法名:percolateDown
* 说明:
*/
private void percolateDown(int position) {
AnyType temp = array[position];
int child;
for (; position * 2 <= currentSize; position = child) {
child = 2 * position;
if (child != currentSize
&& array[child + 1].compareTo(array[child]) < 0)
child++;
if (array[child].compareTo(temp) < 0)
array[position] = array[child];
else
break;
}
array[position] = temp;
}
/**
* 方法名:buildHeap
* 说明:下滤的顺序很关键 从中间开始不断向上依次下滤
*/
private void buildHeap() {
for (int i = currentSize / 2; i > 0; i--)
percolateDown(i);
}
public void insert(AnyType x) {
if (currentSize >= array.length - 1)
enlargeArray(array.length * 2 + 1);
int hole = ++currentSize;
while (hole > 1 && x.compareTo(array[hole / 2]) < 0) {
array[hole] = array[hole / 2];
hole /= 2;
}
array[hole] = x;
}
private void enlargeArray(int capacity) {
AnyType[] oldArr = array;
AnyType[] newArr = (AnyType[]) new Comparable[capacity];
for (int i = 1; i < array.length; i++)
newArr[i] = oldArr[i];
array = newArr;
}
public boolean isEmpty() {
return currentSize == 0;
}
public AnyType deleMin() {
if (!isEmpty()) {
AnyType min = array[1];
array[1] = array[currentSize--];
percolateDown(1);
return min;
}
return null;
}
public String toString() {
StringBuffer sb = new StringBuffer();
for (int i = 1; i <= currentSize; i++)
sb.append(array[i] + " ");
return new String(sb);
}
}
/**
* 类名:MaxHeap 说明:最大堆操作与建堆
*/
class MaxHeap<AnyType extends Comparable<? super AnyType>> {
private AnyType[] array;
private int currentSize;
public MaxHeap(AnyType[] arr) {
currentSize = arr.length;
array = (AnyType[]) new Comparable[currentSize + 1];
int i = 1;
for (int j = 0; j < arr.length; j++)
array[i++] = arr[j];
buildHeap();
}
/**
* 方法名:buildHeap
* 说明:从中间开始到结束 不断上滤
*/
private void buildHeap() {
for (int i = currentSize /2; i <= currentSize; i++)
percolateUp(i);
}
private void percolateUp(int position) {
AnyType temp = array[position];
while (position > 1) {
if (array[position / 2].compareTo(temp) < 0) {
array[position] = array[position / 2];
position /= 2;
} else
break;
}
array[position] = temp;
}
private boolean isEmpty(){
return currentSize == 0;
}
public AnyType deleMax(){
AnyType max = array[1];
AnyType temp = array[currentSize--];
int position = 1;
int child;
while(position *2 <= currentSize){
child = 2 * position;
if(child!=currentSize&&array[child].compareTo(array[child + 1]) < 0)
child++;
if(array[child].compareTo(temp) > 0)
array[position] = array[child];
else
break;
position = child;
}
array[position] = temp;
return max;
}
public void insert(AnyType x){
if(currentSize == array.length -1)
enlargeArray( 2* currentSize +1);
array[++currentSize] = x;
percolateUp(currentSize);
}
private void enlargeArray(int capacity) {
AnyType[] oldArr = array;
AnyType[] newArr = (AnyType[]) new Comparable[capacity];
for (int i = 1; i < array.length; i++)
newArr[i] = oldArr[i];
array = newArr;
}
public String toString() {
StringBuffer sb = new StringBuffer();
for (int i = 1; i <= currentSize; i++)
sb.append(array[i] + " ");
return new String(sb);
}
}
public class HeapTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
Integer[] arr = new Integer[] { 9,8,7,6,5, 4, 3, 2};
MinHeap min = new MinHeap(arr);
min.insert(10);
min.deleMin();
System.out.println(min);
Integer[] arr2 = new Integer[] { 1, 2, 3, 4, 5,6,7,8,9,10};
MaxHeap max = new MaxHeap(arr2);
max.deleMax();
max.insert(10);
System.out.println(max);
}
}