Skip to content

Commit 388a108

Browse files
author
Ram swaroop
committed
min heap and max heap : done
1 parent 73c82e2 commit 388a108

File tree

3 files changed

+98
-33
lines changed

3 files changed

+98
-33
lines changed

src/me/ramswaroop/arrays/KthLargestElement.java

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package me.ramswaroop.arrays;
22

3-
import me.ramswaroop.common.Heap;
3+
import me.ramswaroop.common.MaxHeap;
44

55
import java.util.Arrays;
66

@@ -40,10 +40,10 @@ public static int getKthLargestElementNaive(int[] a, int k) {
4040
*/
4141
public static int getKthLargestElement(int[] a, int k) {
4242
while (true) {
43-
Heap.buildMaxHeap(a);
43+
MaxHeap.buildMaxHeap(a);
4444
if (k == 1) break;
4545

46-
Heap.swap(a, 0, a.length - 1);
46+
MaxHeap.swap(a, 0, a.length - 1);
4747
a = Arrays.copyOfRange(a, 0, a.length - 1);
4848
k--;
4949
}
@@ -52,7 +52,11 @@ public static int getKthLargestElement(int[] a, int k) {
5252
}
5353

5454
public static void main(String a[]) {
55-
System.out.println(getKthLargestElementNaive(new int[]{2, 4, 5, 7, 1, 8, 9}, 3));
56-
System.out.println(getKthLargestElement(new int[]{2, 4, 5, 7, 1, 8, 9}, 3));
55+
int[] ar = new int[]{2, 4, 5, 7, 1, 8, 9};
56+
System.out.println(Arrays.toString(ar));
57+
System.out.println(getKthLargestElementNaive(ar, 3));
58+
System.out.println(Arrays.toString(ar));
59+
System.out.println(getKthLargestElement(ar, 3));
60+
System.out.println(Arrays.toString(ar));
5761
}
5862
}

src/me/ramswaroop/common/Heap.java renamed to src/me/ramswaroop/common/MaxHeap.java

+9-28
Original file line numberDiff line numberDiff line change
@@ -22,37 +22,17 @@
2222
* @date: 8/2/15
2323
* @time: 11:57 AM
2424
*/
25-
public class Heap {
26-
27-
public static void minHeapify(int[] a, int index) {
28-
int smallest = index;
29-
int leftIndex = 2 * index + 1;
30-
int rightIndex = 2 * index + 2;
31-
32-
if (leftIndex < a.length && a[index] > a[leftIndex]) {
33-
smallest = leftIndex;
34-
}
35-
if (rightIndex < a.length && a[smallest] > a[rightIndex]) {
36-
smallest = rightIndex;
37-
}
38-
39-
if (smallest != index) {
40-
swap(a, index, smallest);
41-
minHeapify(a, smallest);
42-
}
43-
}
25+
public class MaxHeap {
4426

4527
/**
46-
* Converts array {@param a} in to a max heap.
28+
* Makes the array {@param a} satisfy the max heap property starting from
29+
* {@param index} till the end of array.
30+
* <p/>
31+
* Time complexity: O(log n).
4732
*
4833
* @param a
34+
* @param index
4935
*/
50-
public static void buildMinHeap(int[] a) {
51-
for (int i = a.length / 2 - 1; i >= 0; i--) {
52-
minHeapify(a, i);
53-
}
54-
}
55-
5636
public static void maxHeapify(int[] a, int index) {
5737
int largest = index;
5838
int leftIndex = 2 * index + 1;
@@ -73,6 +53,8 @@ public static void maxHeapify(int[] a, int index) {
7353

7454
/**
7555
* Converts array {@param a} in to a max heap.
56+
* <p/>
57+
* Time complexity: O(n) and is not O(n log n).
7658
*
7759
* @param a
7860
*/
@@ -88,12 +70,11 @@ public static void swap(int[] a, int firstIndex, int secondIndex) {
8870
a[firstIndex] = a[firstIndex] - a[secondIndex];
8971
}
9072

73+
// test cases
9174
public static void main(String[] args) {
9275
int[] a = new int[]{2, 4, 5, 1, 6, 7, 8};
9376
System.out.println(Arrays.toString(a));
9477
buildMaxHeap(a);
9578
System.out.println(Arrays.toString(a));
96-
buildMinHeap(a);
97-
System.out.println(Arrays.toString(a));
9879
}
9980
}

src/me/ramswaroop/common/MinHeap.java

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package me.ramswaroop.common;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* Created by IntelliJ IDEA.
7+
* <p/>
8+
* A HEAP is a specialized tree-based ABSTRACT DATA TYPE that satisfies the heap property:
9+
* min-heap: All non-leaf elements are either smaller than or equal to their left and right child.
10+
* max-heap: All non-leaf elements are either greater than or equal to their left and right child.
11+
* <p/>
12+
* Often implemented as an array, where the children of the element at index i are at index
13+
* 2i+1 (left child) and 2i+2 (right child).
14+
* <p/>
15+
* The first element (minimum or maximum, depending on chosen order) can be found in O(1).
16+
* Each successor can be found in O(log n). The algorithm in minHeapify() takes O(log n) time
17+
* Therefore, buildMinHeap() would take O(n log n) time BUT IF OBSERVED CAREFULLY IT TAKES 0(N) TIME.
18+
* <p/>
19+
* Used in the HeapSort algorithm. Also can be used to implement a PriorityQueue.
20+
*
21+
* @author: ramswaroop
22+
* @date: 8/2/15
23+
* @time: 11:57 AM
24+
*/
25+
public class MinHeap {
26+
27+
/**
28+
* Makes the array {@param a} satisfy the min heap property starting from
29+
* {@param index} till the end of array.
30+
* <p/>
31+
* Time complexity: O(log n).
32+
*
33+
* @param a
34+
* @param index
35+
*/
36+
public static void minHeapify(int[] a, int index) {
37+
int smallest = index;
38+
int leftIndex = 2 * index + 1;
39+
int rightIndex = 2 * index + 2;
40+
41+
if (leftIndex < a.length && a[index] > a[leftIndex]) {
42+
smallest = leftIndex;
43+
}
44+
if (rightIndex < a.length && a[smallest] > a[rightIndex]) {
45+
smallest = rightIndex;
46+
}
47+
48+
if (smallest != index) {
49+
swap(a, index, smallest);
50+
minHeapify(a, smallest);
51+
}
52+
}
53+
54+
/**
55+
* Converts array {@param a} in to a min heap.
56+
* <p/>
57+
* Time complexity: O(n) and is not O(n log n).
58+
*
59+
* @param a
60+
*/
61+
public static void buildMinHeap(int[] a) {
62+
for (int i = a.length / 2 - 1; i >= 0; i--) {
63+
minHeapify(a, i);
64+
}
65+
}
66+
67+
public static void swap(int[] a, int firstIndex, int secondIndex) {
68+
a[firstIndex] = a[firstIndex] + a[secondIndex];
69+
a[secondIndex] = a[firstIndex] - a[secondIndex];
70+
a[firstIndex] = a[firstIndex] - a[secondIndex];
71+
}
72+
73+
// test cases
74+
public static void main(String[] args) {
75+
int[] a = new int[]{2, 4, 5, 1, 6, 7, 8};
76+
System.out.println(Arrays.toString(a));
77+
buildMinHeap(a);
78+
System.out.println(Arrays.toString(a));
79+
}
80+
}

0 commit comments

Comments
 (0)