-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinaryHeap.java
115 lines (91 loc) · 2.67 KB
/
BinaryHeap.java
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
import java.util.Arrays;
import java.util.stream.Collectors;
// Original Array
// 6
//
// 2 17
//
// 1 3 9 10
// Min Heap
// 1
//
// 2 9
// 6 3 17 10
//
//
// Max Heap
// 17
// 6 10
// 2 3 9 1
//
class BinaryHeap {
public static void main(String args[]) {
int arr[] = {6, 2, 17, 1, 3, 9, 10};
System.out.println("Original = " + Arrays.stream(arr).mapToObj(String::valueOf).collect(Collectors.joining(" ")));
buildMinHeap(arr, arr.length);
System.out.println("Min Heap = " + Arrays.stream(arr).mapToObj(String::valueOf).collect(Collectors.joining(" ")));
buildMaxHeap(arr, arr.length);
System.out.println("Max Heap = " + Arrays.stream(arr).mapToObj(String::valueOf).collect(Collectors.joining(" ")));
}
private static void buildMinHeap(int arr[], int n) {
int index = (n / 2) - 1;
for (int i = index; i >= 0; i--) {
minHeapify(arr, i, n);
}
}
private static void buildMaxHeap(int arr[], int n) {
int index = (n / 2) - 1;
for (int i = index; i >= 0; i--) {
maxHeapify(arr, i, n);
}
}
private static int findLeftChild(int currentIndex) {
return (2 * currentIndex) + 1;
}
private static int findRightChild(int currentIndex) {
return (2 * currentIndex) + 2;
}
private static int findParent(int currentIndex) {
return (currentIndex - 1) / 2;
}
private static void minHeapify(int arr[], int currentIndex, int n) {
int smallest = currentIndex;
int left = findLeftChild(currentIndex);
int right = findRightChild(currentIndex);
if (left < n && arr[smallest] > arr[left]) {
smallest = left;
}
if (right < n && arr[smallest] > arr[right]) {
smallest = right;
}
if (smallest != currentIndex) {
swap(arr, smallest, currentIndex);
minHeapify(arr, smallest, n);
}
}
private static void maxHeapify(int arr[], int currentIndex, int n) {
int largest = currentIndex;
int left = findLeftChild(currentIndex);
int right = findRightChild(currentIndex);
if (left < n && arr[largest] < arr[left]) {
largest = left;
}
if (right < n && arr[largest] < arr[right]) {
largest = right;
}
if (largest != currentIndex) {
swap(arr, largest, currentIndex);
maxHeapify(arr, largest, n);
}
}
private static void swap(int arr[], int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
private static int remove(int arr[], int n) {
int element = arr[0];
arr[0] = arr[n - 1];
return element;
}
}