Repository navigation
Expand file tree
/
Copy pathHeapTree.java
More file actions
241 lines (226 loc) · 7.7 KB
/
Copy pathHeapTree.java
File metadata and controls
241 lines (226 loc) · 7.7 KB
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import java.util.Arrays;
/**
* Heap is a Complete Binary Tree.
* A complete binary tree is a binary tree in which every level, except possibly the last, is completely filled, and all nodes are as far left as possible.
* A heap tree may be of two types
* 1. Max Heap
* 2. Min Heap
*
* In Max Heap, the value of a node should be greater than or equal to the value of its children.
* In Min Heap, the value of a node should be less than or equal to the value of its children.
*
* Heap may be represented in the form of an array.
*
* The major operations that can be performed on a heap is:
* 1. Insertion
* 2. Deletion
* 3. Build Heap
*/
public class HeapTree {
/**
* Insertion in a Max Heap Tree:
*
* Insertion is the process of adding a new element to the heap.
*
* Algorithm:
* 1. Insert the new element at the end of the heap.
* 2. Compare the new element with its parent.
* 3. If the new element is greater than its parent, swap the new element with its parent.
* 4. Repeat steps 2 and 3 until the new element is less than or equal to its parent.
*
* @param heap: The heap in which the element is to be inserted.
* @param n: The number of elements in the heap.
* @param x: The element to be inserted.
*/
public static void insertMax(int[] heap,int n, int x) {
int i = n-1;
while((i > 0) && (x > heap[(i-1)/2])) {
heap[i] = heap[(i-1)/2];
i = (i-1)/2;
}
heap[i] = x;
}
/**
* Insertion in a Min Heap Tree:
*
* Insertion is the process of adding a new element to the heap.
*
* Algorithm:
* 1. Insert the new element at the end of the heap.
* 2. Compare the new element with its parent.
* 3. If the new element is less than its parent, swap the new element with its parent.
* 4. Repeat steps 2 and 3 until the new element is greater than or equal to its parent.
*
* @param heap: The heap in which the element is to be inserted.
* @param n: The number of elements in the heap.
* @param x: The element to be inserted.
*/
public static void insertMin(int[] heap,int n, int x) {
int i = n-1;
while((i > 0) && (x < heap[(i-1)/2])) {
heap[i] = heap[(i-1)/2];
i = (i-1)/2;
}
heap[i] = x;
}
/**
* Adjust Function for Max Heap:
* This function is used to adjust the heap after deletion of an element.
*
* Algorithm:
* 1. Compare the element with its children.
* 2. If the element is less than the children, swap the element with the greater child.
* 3. Repeat steps 1 and 2 until the element is greater than or equal to its children.
*
* @param heap: The heap to be adjusted.
* @param n: The number of elements in the heap.
* @param i: The index of the element to be adjusted.
*/
public static void adjustMax(int[] heap, int n, int i) {
// This adjusts the max heap stored in a[i..n-1]
int x = heap[i];
int j = 2*(i+1)-1;
while(j <= n-1) {
if((j < n-1) && (heap[j] < heap[j+1]))
j++;
if(x >= heap[j])
break;
heap[(j-1)/2] = heap[j];
j = 2*(j+1)-1;
}
heap[(j-1)/2] = x;
}
/**
* Adjust Function for Min Heap:
* This function is used to adjust the heap after deletion of an element.
*
* Algorithm:
* 1. Compare the element with its children.
* 2. If the element is greater than the children, swap the element with the smaller child.
* 3. Repeat steps 1 and 2 until the element is less than or equal to its children.
*
* @param heap: The heap to be adjusted.
* @param n: The number of elements in the heap.
* @param i: The index of the element to be adjusted.
*/
public static void adjustMin(int[] heap, int n, int i) {
// This adjusts the min heap stored in a[i..n-1]
int x = heap[i];
int j = 2*(i+1)-1;
while(j <= n-1) {
if((j < n-1) && (heap[j] > heap[j+1]))
j++;
if(x <= heap[j])
break;
heap[(j-1)/2] = heap[j];
j = 2*(j+1)-1;
}
heap[(j-1)/2] = x;
}
/**
* Deletion in a Max Heap Tree:
* This function is used to delete an element from the heap.
*
* Algorithm:
* 1. Replace the root element with the last element of the heap.
* 2. Adjust the heap.
*
* @param heap: The heap from which the element is to be deleted.
* @param n: The number of elements in the heap.
* @return The element deleted from the heap.
*/
public static int deleteMax(int[] heap, int n) {
int x = heap[0];
heap[0] = heap[n-1];
adjustMax(heap, n-1, 0);
return x;
}
/**
* Deletion in a Min Heap Tree:
* This function is used to delete an element from the heap.
*
* Algorithm:
* 1. Replace the root element with the last element of the heap.
* 2. Adjust the heap.
*
* @param heap: The heap from which the element is to be deleted.
* @param n: The number of elements in the heap.
* @return The element deleted from the heap.
*/
public static int deleteMin(int[] heap, int n) {
int x = heap[0];
heap[0] = heap[n-1];
adjustMin(heap, n-1, 0);
return x;
}
/**
* Build Max Heap - HeapifyMax:
* This function is used to build a max heap from an array of elements.
*
* Algorithm:
* 1. Start from the last non-leaf node.
* 2. Adjust the heap.
* 3. Repeat step 2 until the root node.
*
* @param heap: The array of elements from which the max heap is to be built.
* @param n: The number of elements in the array.
*/
public static void heapify(int[] heap, int n) {
for(int i = (n-2)/2; i >= 0; i--)
adjustMax(heap, n, i);
}
/**
* Build Min Heap - HeapifyMin:
* This function is used to build a min heap from an array of elements.
*
* Algorithm:
* 1. Start from the last non-leaf node.
* 2. Adjust the heap.
*
*
* @param heap: The array of elements from which the min heap is to be built.
* @param n: The number of elements in the array.
*/
public static void heapifyMin(int[] heap, int n) {
for(int i = (n-2)/2; i >= 0; i--)
adjustMin(heap, n, i);
}
/**
* Heap Sort
* This function is used to sort an array of elements using heap sort.
*
* Algorithm:
* 1. Build a max heap from the array.
* 2. Delete the root element and adjust the heap.
* 3. Repeat step 2 until the heap is empty.
*
* Time Complexity:
* The time complexity of heap sort is O(nlogn).
*
* @param heap: The array of elements to be sorted.
* @param n: The number of elements in the array.
*/
public static void heapSort(int[] heap, int n) {
heapify(heap, n);
for(int i = n-1; i >= 1; i--) {
int x = heap[0];
heap[0] = heap[i];
heap[i] = x;
adjustMax(heap, i, 0);
}
}
// Main
public static void main(String[] args) {
// create an array with following values: 40 35 45 60 90 70 80
int[] heap = {40, 35, 45, 60, 90, 70, 80};
// print the array
System.out.println(Arrays.toString(heap));
// call the heapify function
heapify(heap, heap.length);
System.out.println(Arrays.toString(heap));
// perform heap sort
heapSort(heap, heap.length);
System.out.println("After Sorting");
System.out.println(Arrays.toString(heap));
}
}