-
Notifications
You must be signed in to change notification settings - Fork 0
/
KthMinElementfromStreamUsingHeap.cpp
147 lines (126 loc) · 3.4 KB
/
KthMinElementfromStreamUsingHeap.cpp
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
// A C++ program to find k'th smallest element in a stream
#include<iostream>
#include<climits>
using namespace std;
// Prototype of a utility function to swap two integers
void swap(int *x, int *y);
// A class for Min Heap
class MinHeap
{
int *harr; // pointer to array of elements in heap
int capacity; // maximum possible size of min heap
int heap_size; // Current number of elements in min heap
public:
MinHeap(int a[], int size); // Constructor
void buildHeap();
void MinHeapify(int i); //To minheapify subtree rooted with index i
int parent(int i) { return (i-1)/2; }
int left(int i) { return (2*i + 1); }
int right(int i) { return (2*i + 2); }
int extractMin(); // extracts root (minimum) element
int getMin() { return harr[0]; }
// to replace root with new node x and heapify() new root
void replaceMin(int x) { harr[0] = x; MinHeapify(0); }
};
MinHeap::MinHeap(int a[], int size)
{
heap_size = size;
harr = a; // store address of array
}
void MinHeap::buildHeap()
{
int i = (heap_size - 1)/2;
while (i >= 0)
{
MinHeapify(i);
i--;
}
}
// Method to remove minimum element (or root) from min heap
int MinHeap::extractMin()
{
if (heap_size == 0)
return INT_MAX;
// Store the minimum vakue.
int root = harr[0];
// If there are more than 1 items, move the last item to root
// and call heapify.
if (heap_size > 1)
{
harr[0] = harr[heap_size-1];
MinHeapify(0);
}
heap_size--;
return root;
}
// A recursive method to heapify a subtree with root at given index
// This method assumes that the subtrees are already heapified
void MinHeap::MinHeapify(int i)
{
int l = left(i);
int r = right(i);
int smallest = i;
if (l < heap_size && harr[l] < harr[i])
smallest = l;
if (r < heap_size && harr[r] < harr[smallest])
smallest = r;
if (smallest != i)
{
swap(&harr[i], &harr[smallest]);
MinHeapify(smallest);
}
}
// A utility function to swap two elements
void swap(int *x, int *y)
{
int temp = *x;
*x = *y;
*y = temp;
}
// Function to return k'th largest element from input stream
void kthLargest(int k)
{
// count is total no. of elements in stream seen so far
int count = 0, x; // x is for new element
// Create a min heap of size k
int *arr = new int[k];
MinHeap mh(arr, k);
while (1)
{
// Take next element from stream
cout << "Enter next element of stream ";
cin >> x;
// Nothing much to do for first k-1 elements
if (count < k-1)
{
arr[count] = x;
count++;
}
else
{
// If this is k'th element, then store it
// and build the heap created above
if (count == k-1)
{
arr[count] = x;
mh.buildHeap();
}
// If next element is greater than k'th
// largest, then replace the root
if (x > mh.getMin())
mh.replaceMin(x); // replaceMin calls heapify()
// Root of heap is k'th largest element
cout << "K'th largest element is "
<< mh.getMin() << endl;
count++;
}
}
}
// Driver program to test above methods
int main()
{
int k = 3;
cout << "K is " << k << endl;
kthLargest(k);
return 0;
}