-
Notifications
You must be signed in to change notification settings - Fork 20
/
MaxHeap.java
110 lines (88 loc) · 2.47 KB
/
MaxHeap.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
import java.util.*;
public class MaxHeap {
private int[] heap;
private int size;
private int capacity;
public MaxHeap(int capacity) {
this.capacity = capacity;
this.size = 0;
this.heap = new int[capacity];
}
private int parent(int index) {
return (index - 1) / 2;
}
private int leftChild(int index) {
return 2 * index + 1;
}
private int rightChild(int index) {
return 2 * index + 2;
}
private void swap(int a, int b) {
int temp = heap[a];
heap[a] = heap[b];
heap[b] = temp;
}
public void insert(int key) {
if (size >= capacity) {
System.out.println("Heap is full. Cannot insert more elements.");
return;
}
size++;
int currentIndex = size - 1;
heap[currentIndex] = key;
while (currentIndex > 0 && heap[currentIndex] > heap[parent(currentIndex)]) {
swap(currentIndex, parent(currentIndex));
currentIndex = parent(currentIndex);
}
}
private void heapify(int index) {
int largest = index;
int left = leftChild(index);
int right = rightChild(index);
if (left < size && heap[left] > heap[largest]) {
largest = left;
}
if (right < size && heap[right] > heap[largest]) {
largest = right;
}
if (largest != index) {
swap(index, largest);
heapify(largest);
}
}
public int extractMax() {
if (size <= 0) {
System.out.println("Heap is empty.");
return -1;
}
if (size == 1) {
size--;
return heap[0];
}
int max = heap[0];
heap[0] = heap[size - 1];
size--;
heapify(0);
return max;
}
public void printHeap() {
for (int i = 0; i < size; i++) {
System.out.print(heap[i] + " ");
}
System.out.println();
}
public static void main(String[] args) {
MaxHeap maxHeap = new MaxHeap(10);
maxHeap.insert(4);
maxHeap.insert(10);
maxHeap.insert(8);
maxHeap.insert(15);
maxHeap.insert(20);
System.out.println("Max Heap:");
maxHeap.printHeap();
int max = maxHeap.extractMax();
System.out.println("Extracted max element: " + max);
System.out.println("Max Heap after extraction:");
maxHeap.printHeap();
}
}