-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathE1.OP.cpp
142 lines (129 loc) · 2.61 KB
/
E1.OP.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
//Group E Heap Data Structure
#include<iostream>
using namespace std;
class student
{
int marks[10];
int n;
public:
void read_marks();
void display_marks();
void max_heapify(int i);
void print_maximum();
void build_max_heap();
void min_heapify(int i);
void build_min_heap();
void print_minimum();
};
void student::print_minimum()
{
cout<<"\nminimum marks obtained by a student is "<<marks[0];
}
void student::build_min_heap()
{
int last_non_leafnode=(n/2)-1;
for(int i=last_non_leafnode;i>=0;i--)
{
min_heapify(i);
}
}
void student::build_max_heap()
{
int last_non_leafnode=(n/2)-1;
for(int i=last_non_leafnode;i>=0; i--)
{
max_heapify(i);
}
}
void student::print_maximum()
{
cout<<"\nmaximum marks obtained by student is "<<marks[0];
}
void student::min_heapify(int i)
{
int minimum=i;
int leftchild=2*i+1;
int rightchild=2*i+2;
if(leftchild<n && marks[leftchild]<marks[minimum])
{
minimum=leftchild;
}
if(rightchild<n && marks[rightchild]<marks[minimum])
{
minimum=rightchild;
}
if(minimum!=i)
{
int temp=marks[i];
marks[i]=marks[minimum];
marks[minimum]=temp;
min_heapify(minimum);
}
}
void student::max_heapify(int i)
{
int largest=i;
int leftchild=2*i+1;
int rightchild=2*i+2;
if(leftchild < n && marks[leftchild]>marks[largest])
{
largest =leftchild;
}
if(rightchild<n && marks[rightchild]>marks[largest])
{
largest =rightchild;
}
if(largest !=i)
{
int temp =marks[i];
marks[i]=marks[largest];
marks[largest]=temp;
max_heapify(largest);
}
}
void student::read_marks()
{
cout<<"\nEnter n for total no. of marks";
cin>>n;
for(int i=0;i<n;i++)
{
cout<<"\nEnter marks ";
cin>>marks[i];
}
}
void student::display_marks()
{
cout<<"\nEntered marks are ";
for(int i=0;i<n;i++)
cout<<marks[i]<<" ";
}
int main()
{
cout<<"hello";
student s;
s.read_marks();
s.display_marks();
cout<<"\nBuilding max heap:=> ";
s.build_max_heap();
s.display_marks();
s.print_maximum();
cout<<"\nbuilding min heap :=>";
s.build_min_heap();
s.display_marks();
s.print_minimum();
return(0);
}
"OUTPUT":-
Enter n for total no. of marks5
Enter marks 44
Enter marks 45
Enter marks 76
Enter marks 56
Enter marks 77
Entered marks are 44 45 76 56 77
Building max heap:=>
Entered marks are 77 56 76 44 45
maximum marks obtained by student is 77
building min heap :=>
Entered marks are 44 45 76 56 77
minimum marks obtained by a student is 44