Skip to content

Commit 32f7e6b

Browse files
committed
Add noOfWays and combineHeaps.
1 parent e92e985 commit 32f7e6b

File tree

5 files changed

+65
-14
lines changed

5 files changed

+65
-14
lines changed

Graphs/a.out

0 Bytes
Binary file not shown.

Graphs/noOfWays.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,13 @@ int findWays(vector<int> grph[], bool visited[], int src, int dest)
2121
qt.pop();
2222

2323
if (root == dest)
24-
{
25-
visited[root] = true;
2624
continue;
27-
}
2825
else
2926
{
3027
for (auto i = grph[root].begin(); i != grph[root].end(); i++)
3128
{
3229
if (*i == dest)
33-
{
3430
ways++;
35-
pathCount++;
36-
}
3731

3832
if (!visited[*i])
3933
{

Heaps/MaxHeap.h

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,33 +46,37 @@ void buildHeap(Heap *heap, int n)
4646
int extractMax(Heap *&heap)
4747
{
4848
int root = heap->heapArr[1];
49-
49+
5050
swap(heap->heapArr[1], heap->heapArr[heap->currentHeapSize]);
51-
51+
5252
heap->currentHeapSize--;
5353

5454
heapifyDown(heap, 1, heap->currentHeapSize);
5555
return root;
5656
}
5757

58-
void printHeap(Heap *heap, int n) {
59-
for (int t=1;t<=n;t++)
58+
void printHeap(Heap *heap, int n)
59+
{
60+
for (int t = 1; t <= n; t++)
6061
cout << heap->heapArr[t] << " ";
61-
62+
6263
cout << endl;
6364
return;
6465
}
6566

66-
void heapSort(Heap *heap) {
67+
void heapSort(Heap *heap)
68+
{
6769
int n = heap->currentHeapSize;
68-
for (int t = 1;t<=n;t++) {
70+
for (int t = 1; t <= n; t++)
71+
{
6972
extractMax(heap);
7073
}
7174
printHeap(heap, n);
7275
return;
7376
}
7477

75-
Heap *initHeap() {
78+
Heap *initHeap()
79+
{
7680
Heap *heap = new Heap();
7781
heap->currentHeapSize = 0;
7882
heap->maxHeapSize = 64;

Heaps/a.out

224 Bytes
Binary file not shown.

Heaps/combineHeaps.cpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#include <iostream>
2+
#include "MaxHeap.h"
3+
using namespace std;
4+
5+
void printHeap(Heap *hp)
6+
{
7+
cout << endl;
8+
for (int t = 1; t < (hp->currentHeapSize / 2) + 1; t++)
9+
{
10+
cout << hp->heapArr[t] << " -> ";
11+
cout << " Left Child : ";
12+
cout << hp->heapArr[(2 * t)] << '\t';
13+
14+
cout << " Right Child : ";
15+
cout << hp->heapArr[(2 * t) + 1] << '\t';
16+
cout << endl;
17+
}
18+
cout << endl;
19+
return;
20+
}
21+
22+
void combineHeaps(Heap *h1, Heap *h2)
23+
{
24+
int n = h2->currentHeapSize;
25+
for (int t=1;t<=n;t++)
26+
{
27+
insert(h1, h2->heapArr[t]);
28+
}
29+
buildHeap(h1, h1->currentHeapSize);
30+
return;
31+
}
32+
33+
int main()
34+
{
35+
Heap *h1 = initHeap();
36+
Heap *h2 = initHeap();
37+
38+
insert(h1, 50);
39+
insert(h1, 40);
40+
insert(h1, 30);
41+
insert(h1, 20);
42+
insert(h1, 31);
43+
insert(h1, 25);
44+
45+
insert(h2, 100);
46+
insert(h2, 56);
47+
insert(h2, 80);
48+
insert(h2, 4);
49+
50+
printHeap(h1);
51+
combineHeaps(h1, h2);
52+
printHeap(h1);
53+
}

0 commit comments

Comments
 (0)