-
Notifications
You must be signed in to change notification settings - Fork 0
/
MaxHeap.cpp
134 lines (121 loc) · 2.56 KB
/
MaxHeap.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
#include <bits/stdc++.h>
using namespace std;
class MaxHeap
{
public:
int arr[100];
int size;
MaxHeap()
{
arr[0] = -1;
size = 0;
}
void insert(int value)
{
size = size + 1;
int index = size;
arr[index] = value;
while (index > 1)
{
int parent = index / 2;
if (arr[parent] < value)
{
swap(arr[parent], arr[index]);
index = parent;
}
else
{
return;
}
}
}
void Remove()
{
if (size == 0)
{
cout << "There is nothing to Delete\n";
return;
}
arr[1] = arr[size];
size--;
int i = 1;
while (i < size)
{
int leftIndex = 2 * i;
int rightIndex = 2 * i + 1;
if (leftIndex < size && arr[i] < arr[leftIndex])
{
swap(arr[leftIndex], arr[i]);
i = leftIndex;
}
else if (rightIndex < size && arr[i] < arr[rightIndex])
{
swap(arr[rightIndex], arr[i]);
i = rightIndex;
}
else
{
return;
}
}
}
void Print()
{
for (int i = 1; i <= size; i++)
{
cout << arr[i] << " ";
}
cout << "\n";
}
};
void Heapify(int arr[], int size, int index)
{
int largest = index;
int left = index * 2;
int right = index * 2 + 1;
if (left <= size && arr[largest] < arr[left])
{
largest = left;
}
if (right <= size && arr[largest] < arr[right])
{
largest = right;
}
if (largest != index)
{
swap(arr[largest], arr[index]);
Heapify(arr, size, largest);
}
}
int main()
{
MaxHeap H;
while (true)
{
cout << "(1).Press 1 for adding Element in MaxHeap\n";
cout << "(2).Press 2 for deleting Element from MaxHeap\n";
cout << "(3).Press 3 for printing all Element of MaxHeap\n";
cout << "(4).Press 4 for exiting the program\n";
int n;
cin >> n;
switch (n)
{
case 1:
cout << "Enter the Element to add in heap\n";
int x;
cin >> x;
H.insert(x);
break;
case 2:
H.Remove();
break;
case 3:
H.Print();
break;
}
if (n == 4)
{
break;
}
}
}