FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Data-Structure-Algorithm/15Heap.cpp at main · mayurgalhate/Data-Structure-Algorithm · GitHub
mayurgalhate
/
Data-Structure-Algorithm
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
Data-Structure-Algorithm
/
15Heap.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
182 lines (141 loc) · 3.73 KB
Breadcrumbs
Data-Structure-Algorithm
/
15Heap.cpp
Copy path
File metadata and controls
182 lines (141 loc) · 3.73 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#
include
<
iostream
>
using
namespace
std
;
#
include
<
queue
>
class
heap
{
public:
int
arr[
100
];
int
size;
heap
(){
arr[
1
] = -
1
;
size =
0
;
}
void
insert
(
int
val){
size = size +
1
;
int
index = size;
arr[index] = val;
while
(index >
1
){
int
parent = index/
2
;
if
(arr[parent] < arr[index]){
swap
(arr[parent], arr[index]);
index = parent;
}
else
{
return
;
}
}
}
void
print
(){
for
(
int
i =
1
; i <= size; i++)
{
cout << arr[i] <<
"
"
;
} cout << endl;
}
void
deletefromHeap
(){
if
(size ==
0
){
cout <<
"
nothing to delete
"
;
return
;
}
//
step1: put last element in first
arr[
1
] = arr[size];
//
step2: remove last element
size--;
//
step3: take root node to its correct position
int
i =
1
;
while
(i < size){
int
leftindex =
2
*i;
int
rightindex =
2
*i+
1
;
if
(leftindex < size && arr[i] < arr[leftindex]){
swap
(arr[i], arr[leftindex]);
i = leftindex;
}
else
if
(rightindex < size && arr[i] < arr[rightindex]){
swap
(arr[i], arr[rightindex]);
i = rightindex;
}
else
{
return
;
}
}
}
void
heapify
(
int
arr[],
int
n,
int
i){
int
largest = i;
//
1 base indexing logic if 0 base indexing then for left = 2*i+1 amd right = 2*i+2
int
left =
2
*i;
int
right =
2
*i+
1
;
//
no need of <=
if
(left <= n && arr[largest] < arr[left]){
largest = left;
}
//
no need of <=
if
(right <= n && arr[largest] < arr[right]){
largest = right;
}
if
(largest != i){
swap
(arr[largest], arr[i]);
heapify
(arr, n, largest);
}
}
void
heapSort
(
int
arr[],
int
n){
int
size = n;
while
(size >
1
){
//
step1: swap
swap
(arr[size], arr[
1
]);
size--;
//
step2
heapify
(arr, size,
1
);
}
}
};
int
main
(){
heap h;
h.
insert
(
50
);
h.
insert
(
55
);
h.
insert
(
53
);
h.
insert
(
52
);
h.
insert
(
54
);
h.
print
();
h.
deletefromHeap
();
h.
print
();
int
arr[
6
] = {-
1
,
54
,
53
,
55
,
52
,
50
};
int
n =
5
;
//
if 1 base indexing so below i = n/2 or if 0 base indexing then i = n/2-1
for
(
int
i = n/
2
; i >
0
; i--){
h.
heapify
(arr, n, i);
}
cout <<
"
printing the array now:
"
<< endl ;
for
(
int
i =
1
; i <= n; i++)
{
cout << arr[i] <<
"
"
;
}cout << endl;
h.
heapSort
(arr, n);
cout <<
"
printing the Sorted array now:
"
<< endl ;
for
(
int
i =
1
; i <= n; i++)
{
cout << arr[i] <<
"
"
;
}
cout << endl;
cout <<
"
Max heapusing Priority Queue
"
<< endl;
priority_queue<
int
> pq;
pq.
push
(
5
);
pq.
pop
();
pq.
push
(
4
);
pq.
push
(
2
);
pq.
push
(
9
);
cout <<
"
Top :
"
<< pq.
top
() << endl;
cout <<
"
size :
"
<< pq.
size
() << endl;
string ans = (pq.
empty
()) ?
"
empty
"
:
"
Notempty
"
;
cout << ans ;
cout << endl;
cout <<
"
Using Min heap
"
<< endl;
priority_queue<
int
, vector<
int
>, greater<
int
>> minheap;
minheap.
push
(
5
);
minheap.
pop
();
minheap.
push
(
4
);
minheap.
push
(
2
);
minheap.
push
(
9
);
cout <<
"
Top :
"
<< minheap.
top
() << endl;
cout <<
"
size :
"
<< minheap.
size
() << endl;
string res = (minheap.
empty
()) ?
"
empty
"
:
"
Notempty
"
;
cout << res ;
return
0
;
}
Back
|
FazBrowse Home
|
New Git URL