Problem p02277 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <stdio.h>

#define MAX 100000

struct TRAMP {
  char mark;
  int number;
};

TRAMP cards[MAX];

struct Node {
  Node() {
    number = 0;
    mark_array_index = 0;
    l_p = r_p = p_p = -1;
  }
  int number, mark_array_index, l_p, r_p, p_p;
  char mark_array[5];
};

class Tree {
public:
  Tree(int n) {
    num_of_nodes = 0;
    nodes = new Node[n];
  };
  ~Tree() { delete[] nodes; }
  void push(TRAMP data) {
    int t = 0;
    while (true) {
      if (num_of_nodes == 0) {
        nodes[num_of_nodes].number = data.number;
        nodes[num_of_nodes].mark_array[nodes[num_of_nodes].mark_array_index++] =
            data.mark;
        num_of_nodes++;
        break;
      } else {
        if (nodes[t].number == data.number) {
          nodes[t].mark_array[nodes[t].mark_array_index++] = data.mark;
          break;
        } else if (nodes[t].number > data.number) {
          if (nodes[t].l_p == -1) {
            nodes[t].l_p = num_of_nodes;
            nodes[num_of_nodes].p_p = t;
            nodes[num_of_nodes].number = data.number;
            nodes[num_of_nodes]
                .mark_array[nodes[num_of_nodes].mark_array_index++] = data.mark;
            num_of_nodes++;
            break;
          } else {
            t = nodes[t].l_p;
          }
        } else {
          if (nodes[t].r_p == -1) {
            nodes[t].r_p = num_of_nodes;
            nodes[num_of_nodes].p_p = t;
            nodes[num_of_nodes].number = data.number;
            nodes[num_of_nodes]
                .mark_array[nodes[num_of_nodes].mark_array_index++] = data.mark;
            num_of_nodes++;
            break;
          } else {
            t = nodes[t].r_p;
          }
        }
      }
    }
  }

  bool compare(int cards_index) {
    int k = 0, number = cards[cards_index].number, t = 0;

    while (true) {
      if (nodes[t].number == number)
        break;
      else {
        t = (nodes[t].number > number) ? nodes[t].l_p : nodes[t].r_p;
      }
    }

    while (number == cards[cards_index].number) {
      if (cards[cards_index].mark != nodes[t].mark_array[k])
        return false;
      else {
        cards_index++;
        k++;
      }
    }
    return true;
  }

private:
  int num_of_nodes;
  Node *nodes;
};

int n;

int Partition(int left, int right) {
  int i = left - 1, pivot = cards[right].number;
  for (int start = left; start < right; start++) {
    if (cards[start].number <= pivot) {
      i++;
      std::swap(cards[i], cards[start]);
    }
  }

  std::swap(cards[i + 1], cards[right]);

  return i + 1;
}

void quickSort(int left, int right) {
  if (left < right) {
    int q = Partition(left, right);
    quickSort(left, q - 1);
    quickSort(q + 1, right);
  }
}

int main() {

  bool stableFLG = true;
  scanf("%d", &n);
  Tree tree(n);

  for (int i = 0; i < n; i++) {
    scanf("%s %d", &cards[i].mark, &cards[i].number);
    tree.push(cards[i]);
  }

  quickSort(0, n - 1);

  int history = 0;
  for (int i = 1; i < n; i++) {
    if (cards[history].number != cards[i].number || i == n - 1) {
      if (!tree.compare(history)) {
        stableFLG = false;
        break;
      } else {
        history = i;
      }
    }
  }

  if (stableFLG) {
    printf("Stable\\n");
  } else {
    printf("Not stable\\n");
  }

  for (int i = 0; i < n; i++)
    printf("%c %d\\n", cards[i].mark, cards[i].number);
}

Error: -11

Fixed submission:

#include <stdio.h>

#define MAX 100000

struct TRAMP {
  char mark;
  int number;
};

TRAMP cards[MAX];

struct Node {
  Node() {
    number = 0;
    mark_array_index = 0;
    l_p = r_p = p_p = -1;
  }
  int number, mark_array_index, l_p, r_p, p_p;
  char mark_array[5];
};

class Tree {
public:
  Tree(int n) {
    num_of_nodes = 0;
    nodes = new Node[n];
  };
  ~Tree() { delete[] nodes; }
  void push(TRAMP data) {
    int t = 0;
    while (true) {
      if (num_of_nodes == 0) {
        nodes[num_of_nodes].number = data.number;
        nodes[num_of_nodes].mark_array[nodes[num_of_nodes].mark_array_index++] =
            data.mark;
        num_of_nodes++;
        break;
      } else {
        if (nodes[t].number == data.number) {
          nodes[t].mark_array[nodes[t].mark_array_index++] = data.mark;
          break;
        } else if (nodes[t].number > data.number) {
          if (nodes[t].l_p == -1) {
            nodes[t].l_p = num_of_nodes;
            nodes[num_of_nodes].p_p = t;
            nodes[num_of_nodes].number = data.number;
            nodes[num_of_nodes]
                .mark_array[nodes[num_of_nodes].mark_array_index++] = data.mark;
            num_of_nodes++;
            break;
          } else {
            t = nodes[t].l_p;
          }
        } else {
          if (nodes[t].r_p == -1) {
            nodes[t].r_p = num_of_nodes;
            nodes[num_of_nodes].p_p = t;
            nodes[num_of_nodes].number = data.number;
            nodes[num_of_nodes]
                .mark_array[nodes[num_of_nodes].mark_array_index++] = data.mark;
            num_of_nodes++;
            break;
          } else {
            t = nodes[t].r_p;
          }
        }
      }
    }
  }

  bool compare(int cards_index) {
    int k = 0, number = cards[cards_index].number, t = 0;

    while (true) {
      if (nodes[t].number == number)
        break;
      else {
        t = (nodes[t].number > number) ? nodes[t].l_p : nodes[t].r_p;
      }
    }

    while (number == cards[cards_index].number) {
      if (cards[cards_index].mark != nodes[t].mark_array[k])
        return false;
      else {
        cards_index++;
        k++;
      }
    }
    return true;
  }

private:
  int num_of_nodes;
  Node *nodes;
};

int n;

int Partition(int left, int right) {
  int i = left - 1, pivot = cards[right].number;
  TRAMP tmp;
  for (int start = left; start <= right; start++) {
    if (cards[start].number <= pivot) {
      i++;
      tmp = cards[i];
      cards[i] = cards[start];
      cards[start] = tmp;
    }
  }

  return i + 1;
}

void quickSort(int left, int right) {
  if (left < right) {
    int q = Partition(left, right);
    quickSort(left, q - 1);
    quickSort(q + 1, right);
  }
}

int main() {

  bool stableFLG = true;
  scanf("%d", &n);
  Tree tree(n);

  for (int i = 0; i < n; i++) {
    scanf("%s %d", &cards[i].mark, &cards[i].number);
    tree.push(cards[i]);
  }

  quickSort(0, n - 1);

  int history = 0;
  for (int i = 1; i < n; i++) {
    if (cards[history].number != cards[i].number || i == n - 1) {
      if (!tree.compare(history)) {
        stableFLG = false;
        break;
      } else {
        history = i;
      }
    }
  }

  if (stableFLG) {
    printf("Stable\\n");
  } else {
    printf("Not stable\\n");
  }

  for (int i = 0; i < n; i++)
    printf("%c %d\\n", cards[i].mark, cards[i].number);
}