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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctype.h>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>

using namespace std;

class tree {
public:
  void putparent(int n);
  void putleft(int n);
  void putright(int n);
  int getparent();
  string gettype();
  int getleft();
  int getright();
  int getdegree();
  tree();

private:
  int parent;
  int left;
  int right;
};

typedef struct tree Tree;

tree::tree() {
  parent = -1;
  left = -1;
  right = -1;
}

void tree::putparent(int n) {
  parent = n;
  return;
}

void tree::putleft(int n) {
  left = n;
  return;
}

void tree::putright(int n) {
  right = n;
  return;
}

int tree::getparent() { return parent; }

string tree::gettype() {
  if (getparent() == -1)
    return "root";
  else if (getleft() == -1 && getright() == -1)
    return "leaf";
  else
    return "internal node";
}

int tree::getleft() { return left; }

int tree::getright() { return right; }

int tree::getdegree() {
  int cnt = 0;
  if (getleft() != -1)
    cnt++;
  if (getright() != -1)
    cnt++;
  return cnt;
}

int getdepth(Tree a[], int n) {
  int ans = 0;
  if (a[n].getparent() != -1)
    return getdepth(a, a[n].getparent()) + 1;
  else
    return 0;
}

int getsibling(Tree a[], int n) {
  int p = a[n].getparent();
  if (p == -1)
    return -1;
  int ans = a[p].getleft() + a[p].getright() - n;
  return ans;
}

int getheight(Tree a[], int n) {
  if (a[n].getleft() == -1 && a[n].getright() == -1)
    return 0;
  else if (a[n].getleft() == -1)
    return getheight(a, a[n].getright()) + 1;
  else if (a[n].getright() == -1)
    return getheight(a, a[n].getleft()) + 1;
  else
    return max(getheight(a, a[n].getleft()), getheight(a, a[n].getright())) + 1;
}

int main() {
  int n;
  cin >> n;
  Tree t[25];
  int id, l, r;
  for (int i = 0; i < n; i++) {
    cin >> id >> l >> r;
    t[id].putleft(l);
    t[id].putright(r);
    if (l != -1)
      t[l].putparent(id);
    if (r != -1)
      t[r].putparent(id);
  }
  for (int i = 0; i < n; i++) {
    cout << "node " << i << ": ";
    cout << "parent = " << t[i].getparent() << ", ";
    cout << "sibling = " << getsibling(t, i) << ", ";
    cout << "degree = " << t[i].getdegree() << ", ";
    cout << "depth = " << getdepth(t, i) << ", ";
    cout << "height = " << getheight(t, i) << ", ";
    cout << t[i].gettype() << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctype.h>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>

using namespace std;

class tree {
public:
  void putparent(int n);
  void putleft(int n);
  void putright(int n);
  int getparent();
  string gettype();
  int getleft();
  int getright();
  int getdegree();
  tree();

private:
  int parent;
  int left;
  int right;
};

typedef struct tree Tree;

tree::tree() {
  parent = -1;
  left = -1;
  right = -1;
}

void tree::putparent(int n) {
  parent = n;
  return;
}

void tree::putleft(int n) {
  left = n;
  return;
}

void tree::putright(int n) {
  right = n;
  return;
}

int tree::getparent() { return parent; }

string tree::gettype() {
  if (getparent() == -1)
    return "root";
  else if (getleft() == -1 && getright() == -1)
    return "leaf";
  else
    return "internal node";
}

int tree::getleft() { return left; }

int tree::getright() { return right; }

int tree::getdegree() {
  int cnt = 0;
  if (getleft() != -1)
    cnt++;
  if (getright() != -1)
    cnt++;
  return cnt;
}

int getdepth(Tree a[], int n) {
  int ans = 0;
  if (a[n].getparent() != -1)
    return getdepth(a, a[n].getparent()) + 1;
  else
    return 0;
}

int getsibling(Tree a[], int n) {
  int p = a[n].getparent();
  if (p == -1)
    return -1;
  int ans = a[p].getleft() + a[p].getright() - n;
  return ans;
}

int getheight(Tree a[], int n) {
  if (a[n].gettype() == "leaf")
    return 0;
  else
    return max(getheight(a, a[n].getleft()), getheight(a, a[n].getright())) + 1;
}

int main() {
  int n;
  cin >> n;
  Tree t[25];
  int id, l, r;
  for (int i = 0; i < n; i++) {
    cin >> id >> l >> r;
    t[id].putleft(l);
    t[id].putright(r);
    if (l != -1)
      t[l].putparent(id);
    if (r != -1)
      t[r].putparent(id);
  }
  for (int i = 0; i < n; i++) {
    cout << "node " << i << ": ";
    cout << "parent = " << t[i].getparent() << ", ";
    cout << "sibling = " << getsibling(t, i) << ", ";
    cout << "degree = " << t[i].getdegree() << ", ";
    cout << "depth = " << getdepth(t, i) << ", ";
    cout << "height = " << getheight(t, i) << ", ";
    cout << t[i].gettype() << endl;
  }
}