-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathcombineHeaps.cpp
53 lines (46 loc) · 983 Bytes
/
combineHeaps.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
#include <iostream>
#include "MaxHeap.h"
using namespace std;
void printHeap(Heap *hp)
{
cout << endl;
for (int t = 1; t < (hp->currentHeapSize / 2) + 1; t++)
{
cout << hp->heapArr[t] << " -> ";
cout << " Left Child : ";
cout << hp->heapArr[(2 * t)] << '\t';
cout << " Right Child : ";
cout << hp->heapArr[(2 * t) + 1] << '\t';
cout << endl;
}
cout << endl;
return;
}
void combineHeaps(Heap *h1, Heap *h2)
{
int n = h2->currentHeapSize;
for (int t=1;t<=n;t++)
{
insert(h1, h2->heapArr[t]);
}
buildHeap(h1, h1->currentHeapSize);
return;
}
int main()
{
Heap *h1 = initHeap();
Heap *h2 = initHeap();
insert(h1, 50);
insert(h1, 40);
insert(h1, 30);
insert(h1, 20);
insert(h1, 31);
insert(h1, 25);
insert(h2, 100);
insert(h2, 56);
insert(h2, 80);
insert(h2, 4);
printHeap(h1);
combineHeaps(h1, h2);
printHeap(h1);
}