M
M
muller922014-10-10 16:29:53
C++ / C#
muller92, 2014-10-10 16:29:53

Binary heap implementations. What is wrong code?

there is a task for building a heap, I tried in different ways I reach 3 tests and then they know the wrong answer. I checked for different conditions but I'm fine. If anyone can tell me please
Programming problem. This task is to implement a binary heap. The first input line contains the number n (1≤n≤105), then n lines of the form Insert X, where X is a natural number not exceeding 109, or Extract. The first operation should add the number X to the heap, the second should extract the maximum from the heap and print it on the next output line.

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>

using namespace std;

class SimpleBinaryHeap {

  vector<int> heap_data;

public:

  void add(int value)
  {
    heap_data.push_back(value);
    int i = heap_data.size() - 1;
    int parent = (i - 1) / 2;

    while (i > 0 && heap_data[parent] < heap_data[i])
    {
      int temp = heap_data[i];
      heap_data[i] = heap_data[parent];
      heap_data[parent] = temp;

      i = parent;
      parent = (i - 1) / 2;
    }

  }
  void heapify(int i){
    int leftChild;
    int rightChild;
    int largestChild;

    for (;;)
    {
      leftChild = 2 * i + 1;
      rightChild = 2 * i + 2;
      largestChild = i;

      if (leftChild < heap_data.size() && heap_data[leftChild] > heap_data[largestChild])
      {
        largestChild = leftChild;
      }

      if (rightChild < heap_data.size() && heap_data[rightChild] > heap_data[largestChild])
      {
        largestChild = rightChild;
      }

      if (largestChild == i)
      {
        break;
      }

      int temp = heap_data[i];
      heap_data[i] = heap_data[largestChild];
      heap_data[largestChild] = temp;
      i = largestChild;
    }

  }

  int extract() {

    int res = heap_data[0];
    heap_data[0] = heap_data[heap_data.size() - 1];

    int index = 0;

    for (int i = heap_data.size() / 2; i >= 0; i--)
    {
      heapify(i);
    }
    heap_data.pop_back();

    return res;

  }
  int heapSort()
  {

    for (int i = heap_data.size() - 1; i >= 0; i--)
    {

      heapify(0);
      return extract();
    }
  }
};

int main() {

  SimpleBinaryHeap bh;

  string command;
  const char* com_insert = "Insert";
  const char* com_extract = "Extract";

  short num;
  cin >> num;

  for (short int i = 0; i < num; i++) {

    cin >> command;
    if (command == com_insert) {
      int value;
      cin >> value;
      bh.add(value);
    }
    if (command == com_extract) {
      cout << bh.heapSort() << endl;
    }

  }

  return 0;
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
throughtheether, 2014-10-10
@throughtheether

if (command == com_extract) {
      cout << bh.heapSort() << endl;
    }

You need to fetch the root value, restore the heap property, and return the previously fetched value. It's not entirely clear to me what you are doing:
int heapSort()
  {

    for (int i = heap_data.size() - 1; i >= 0; i--)
    {

      heapify(0);
      return extract();
    }
  }

Why, for example, return in a loop?
If I understand correctly, then you need to cout << bh.heapSort() << endl;replace the line withcout << bh.extract() << endl;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question