-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHeap.cpp
More file actions
144 lines (128 loc) · 2.89 KB
/
Heap.cpp
File metadata and controls
144 lines (128 loc) · 2.89 KB
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
#include <iostream>
#include<limits>
using namespace std ;
class heap {
public:
int size;
int *arr;
int totalsize;
heap(int n) {
arr = new int[n+1];
size = 0;
totalsize = n;
}
void insert(int data) {
if (size >= totalsize)
{
cout<<"Heap is full! "<<endl;
return;
}
size++;
arr[size] = data;
}
void heapify(int *arr, int n) {
int index = n;
while (index>1)
{
int parent = index/2;
if (arr[parent] < arr[index]) {
swap(arr[parent],arr[index]);
}
index--;
}
}
void heapsort() {
if (size == 0)
{
cout<<"Heap is empty, cannot sort!"<<endl;
return;
}
int *temp = new int[size+1];
for (int i = 1; i <= size; i++)
{
temp[i] = arr[i];
}
int t = size;
while (t>1)
{
heapify(temp, t);
swap(temp[1],temp[t]);
t--;
}
cout<<"Heap after sorting:";
for (int i = 1; i <= size; i++)
{
cout<<temp[i]<<" ";
}
cout<<endl;
}
void deleteNode() {
if (size == 0) {
cout<<"Heap is empty! "<<endl;
return;
}
arr[1] = arr[size];
size--;
heapify(arr, size);
}
void print() {
if (size == 0) {
cout << "Heap is empty! "<<endl;
return;
}
for (int i = 1; i <= size; i++) {
cout << arr[i] << " ";
}
cout << endl;
}
} ;
int main() {
int n;
cout << "Enter the total size of the heap: ";
while (!(cin >> n)) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(),'\n');
cout<<"Invalid input"<<endl;
cout<<"Enter integer constants"<<endl;
}
if (n <= 0) {
cout << "Heap size must be greater than 0"<<endl;
return 0;
}
heap h1(n);
int operation;
while (operation != 5) {
cout << "Enter the operation: 1-Insert, 2-Delete, 3-Print, 4-Heap sort, 5-Exit";
cin >> operation;
switch (operation) {
case 1: {
int data;
cout << "Enter the value to insert: ";
cin >> data;
h1.insert(data);
break;
}
case 2:{
h1.deleteNode();
cout << "Root deleted "<<endl;
break;
}
case 3:{
cout << "Heap: ";
h1.print();
break;
}
case 4:{
h1.heapsort();
break;
}
case 5:{
cout << "Exit ";
break;
}
default:
cout << "Invalid operation, please try again.";
}
}
return 0;
}