This repository has been archived by the owner on Nov 8, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 610
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #195 from dhiaeddineTounakti/push_heap
added push_heap function
- Loading branch information
Showing
1 changed file
with
36 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# push_heap | ||
|
||
**Description** : Given a heap in the range [first,last-1), this function extends the range considered a heap to [first,last) by placing the value in (last-1) into its corresponding location within it. | ||
|
||
**Example**: | ||
```cpp | ||
#include <iostream> // std::cout | ||
#include <algorithm> // std::make_heap, std::pop_heap, std::push_heap, std::sort_heap | ||
#include <vector> // std::vector | ||
|
||
int main () { | ||
int myints[] = {10,20,30,5,15}; | ||
std::vector<int> v(myints,myints+5); | ||
|
||
std::make_heap (v.begin(),v.end()); | ||
std::cout << "initial max heap : " << v.front() << '\n'; | ||
|
||
std::pop_heap (v.begin(),v.end()); v.pop_back(); | ||
std::cout << "max heap after pop : " << v.front() << '\n'; | ||
|
||
v.push_back(99); std::push_heap (v.begin(),v.end()); | ||
std::cout << "max heap after push: " << v.front() << '\n'; | ||
|
||
std::sort_heap (v.begin(),v.end()); | ||
|
||
std::cout << "final sorted range :"; | ||
for (unsigned i=0; i<v.size(); i++) | ||
std::cout << ' ' << v[i]; | ||
|
||
std::cout << '\n'; | ||
|
||
return 0; | ||
} | ||
``` | ||
|
||
**[Run Code](https://rextester.com/WWHJ67453)** |