Home c++ Structure Heap C++

Structure Heap C++




Please help me debug my code. In programming, a full 0.

In this task, you need to organize Heap data structure for storing integers over which the following operations are defined:

Insert (X) – Add Heap number of X;
Extract – to get the largest number of the Heap (removing it at the same time)


This problem must be solved without the use of built-in data structures to find the maximum number. Input

In the input file is written the number of commands n (1 ≤ n ≤ 100 000), then the sequence of n commands, each on its own line.

Each command has the following format: “0 & lt; number & gt;” or “1”, which means correspondingly operation Insert (& lt; number & gt;) and Extract. The added number in the range from 1 to 107, inclusive.

It is guaranteed that if the Extract command in the structure is at least one element. Output

The output file you want to display for each retrieval command number obtained when the Extract command.

Here is my code:

# include & lt; iostream & gt;
#Include & lt; algorithm & gt;
#Include & lt; vector & gt;
Using Namespace STD;
class SimpleBinaryHeap {
  Vector & lt; int & gt; heap_data;
  void insert (int value) {
    heap_data.push_back (value);
    int index = heap_data.size () - 1;
    int parent = (index - 1) / 2;
    while (index & gt; 0 & amp; & amp; heap_data [parent] & lt; heap_data [index]) {
      swap (heap_data [parent], heap_data [index]);
      index = parent;
      parent = (index - 1) / 2;
  int extract (int & amp; i, int & amp; num) {
    int j = i;
    int cl = i * 2 + 1;
    if (cl & lt; num & amp; & amp; heap_data [cl] & gt; heap_data [j]) j = cl;
    int cr = cl + 1;
    if (cr & lt; num & amp; & amp; heap_data [cr] & gt; heap_data [j]) j = cr;
    if (i == j) return heap_data [0];
    swap (heap_data [i], heap_data [j]);
    extract (j, num);
    return heap_data [0];
  SimpleBinaryHeap bh;
  int command;
  int num;
  CIN & GT; & GT; num;
  for (int i = 0; i & lt; num; i ++) {
    CIN & GT; & GT; command;
    if (command == 0) {
      int value;
      CIN & GT; & GT; Value;
      bh.insert (value);
    if (command == 1) {
      COUT & LT; & LT; bh.extract (i, num) & lt; & lt; Endl;
  Return 0;

Answer 1, Authority 100%

Here it is necessary to clarify what a bunch of you need, if it is necessary for example to sort the vector approach, but the bunch should meet the properties such that: the ancestor node is always greater than its children. This means the maximum will be at the top allowing it promptly removed. Heaps are commonly used in the queues for priority to O (log (n)) take a maximum. You are what you do, a little weird, it’s more like the sort. You need to use obviously not a vector, since it does not allow to quickly retrieve the item in a bad scenario, when using permanent sorting is O (nlog (n)), and when pasting by value, so that the vector was kept always sorted is O (n). If you want to own a bunch of, no matter what the speed, the easier sort after inserting the end and retrieve the last item

& gt; & gt; & gt; code
vector & lt; int & gt; data;
void insert (int num) {
  data.push_back (num);
  sort (data.begin (), data.end ());
int eraseMax () {
  int mm = data.back ();
  data.pop_back ();
  return mm;

It’s over a crutch, but what to do if you do everything through hchoesh vector.
Otherwise, if you want a bunch of a bunch of, you will need to implement a binomial, the simplest of them, then it will be in log and paste and delete in log. Here about it can be read

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions