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

#include <iostream>
#include <stdio.h>
using namespace std;

class Node {
private:
  int id;
  Node *parent;
  Node *child_l;
  Node *child_r;

public:
  Node(int id) {
    this->id = id;
    parent = NULL;
    child_l = NULL;
    child_r = NULL;
  }
  void setChildl(Node *nl);
  void setChildr(Node *nr);
  void setParent(Node *np) { parent = np; }
  Node *searchNode(int id);
  int getId() { return id; }
  int getParentId();
  int getOtherChildId(Node *child);
  int getSiblingId();
  int getDegree();
  int getDepth();
  int getHeight();
  void print();
};

void Node::setChildl(Node *nl) {
  child_l = nl;
  if (child_l != NULL)
    child_l->setParent(this);
}

void Node::setChildr(Node *nr) {
  child_r = nr;
  if (child_r != NULL)
    child_r->setParent(this);
}

Node *Node::searchNode(int id) {
  if (this->id == id)
    return this;
  if (child_l != NULL) {
    Node *sl = child_l->searchNode(id);
    if (sl != NULL)
      return sl;
  }
  if (child_r != NULL) {
    Node *sr = child_r->searchNode(id);
    if (sr != NULL)
      return sr;
  }
  return NULL;
};

int Node::getParentId() {
  if (parent == NULL)
    return -1;
  else
    return parent->getId();
}

int Node::getOtherChildId(Node *child) {
  if (child_l != child && child_l != NULL)
    return child_l->getId();
  if (child_r != child && child_r != NULL)
    return child_r->getId();
  return -1;
}

int Node::getSiblingId() {
  if (parent == NULL)
    return -1;
  return parent->getOtherChildId(this);
}

int Node::getDegree() {
  int cnt = 0;
  if (child_l != NULL)
    cnt++;
  if (child_r != NULL)
    cnt++;
  return cnt;
}

int Node::getDepth() {
  if (parent == NULL)
    return 0;
  return parent->getDepth() + 1;
}

int Node::getHeight() {
  int hl, hr;
  if (child_l == NULL)
    hl = 0;
  else
    hl = child_l->getHeight() + 1;
  if (child_r == NULL)
    hr = 0;
  else
    hr = child_r->getHeight() + 1;
  if (hl > hr)
    return hl;
  else
    return hr;
}

void Node::print() {
  cout << "node " << id << ": ";
  cout << "parent = " << getParentId() << ", ";
  cout << "sibling = " << getSiblingId() << ", ";
  cout << "degree = " << getDegree() << ", ";
  int depth = getDepth();
  cout << "depth = " << depth << ", ";
  int height = getHeight();
  cout << "height = " << height << ", ";
  if (depth == 0)
    cout << "root";
  else if (height == 0)
    cout << "leaf";
  else
    cout << "internal node";
  cout << endl;
}

class BinaryTree {
private:
  Node *root[26];

public:
  BinaryTree() {
    for (int i = 0; i < 26; i++)
      root[i] = NULL;
  }
  Node *setNode(int id);
  void setChildl(Node *node, int idl);
  void setChildr(Node *node, int idr);
};

Node *BinaryTree::setNode(int id) {
  Node *node;
  int i;
  for (i = 0; root[i] != NULL; i++) {
    if ((node = root[i]->searchNode(id)) != NULL)
      return node;
  }
  root[i] = new Node(id);
  return root[i];
}

void BinaryTree::setChildl(Node *node, int idl) {
  if (idl == -1)
    return;

  int i;
  for (i = 0; root[i] != NULL; i++) {
    if (root[i]->searchNode(idl) != NULL) {
      node->setChildl(root[i]);
      int j;
      for (j = i; root[j + 1] != NULL; j++) {
        root[j] = root[j + 1];
      }
      if (j != 0)
        root[j] = NULL;
      return;
    }
  }

  node->setChildl(new Node(idl));
  return;
}

void BinaryTree::setChildr(Node *node, int idr) {
  if (idr == -1)
    return;

  int i;
  for (i = 0; root[i] != NULL; i++) {
    if (root[i]->searchNode(idr) != NULL) {
      node->setChildr(root[i]);
      int j;
      for (j = i; root[j + 1] != NULL; j++) {
        root[j] = root[j + 1];
      }
      if (j != 0)
        root[j] = NULL;
      return;
    }
  }

  node->setChildr(new Node(idr));
  return;
}

int main(void) {
  BinaryTree bt;
  Node *node_list[25];

  int n;
  cin >> n;

  for (int i = 0; i < n; i++) {
    int id, idl, idr;
    cin >> id >> idl >> idr;
    node_list[id] = bt.setNode(id);
    bt.setChildl(node_list[id], idl);
    bt.setChildr(node_list[id], idr);
  }

  for (int i = 0; i < n; i++) {
    node_list[i]->print();
  }

  return 0;
}

Error: WA

Fixed submission:

#include <iostream>
#include <stdio.h>
using namespace std;

class Node {
private:
  int id;
  Node *parent;
  Node *child_l;
  Node *child_r;

public:
  Node(int id) {
    this->id = id;
    parent = NULL;
    child_l = NULL;
    child_r = NULL;
  }
  void setChildl(Node *nl);
  void setChildr(Node *nr);
  void setParent(Node *np) { parent = np; }
  Node *searchNode(int id);
  int getId() { return id; }
  int getParentId();
  int getOtherChildId(Node *child);
  int getSiblingId();
  int getDegree();
  int getDepth();
  int getHeight();
  void print();
};

void Node::setChildl(Node *nl) {
  child_l = nl;
  if (child_l != NULL)
    child_l->setParent(this);
}

void Node::setChildr(Node *nr) {
  child_r = nr;
  if (child_r != NULL)
    child_r->setParent(this);
}

Node *Node::searchNode(int id) {
  if (this->id == id)
    return this;
  if (child_l != NULL) {
    Node *sl = child_l->searchNode(id);
    if (sl != NULL)
      return sl;
  }
  if (child_r != NULL) {
    Node *sr = child_r->searchNode(id);
    if (sr != NULL)
      return sr;
  }
  return NULL;
};

int Node::getParentId() {
  if (parent == NULL)
    return -1;
  else
    return parent->getId();
}

int Node::getOtherChildId(Node *child) {
  if (child_l != child && child_l != NULL)
    return child_l->getId();
  if (child_r != NULL)
    return child_r->getId();
  return -1;
}

int Node::getSiblingId() {
  if (parent == NULL)
    return -1;
  return parent->getOtherChildId(this);
}

int Node::getDegree() {
  int cnt = 0;
  if (child_l != NULL)
    cnt++;
  if (child_r != NULL)
    cnt++;
  return cnt;
}

int Node::getDepth() {
  if (parent == NULL)
    return 0;
  return parent->getDepth() + 1;
}

int Node::getHeight() {
  int hl, hr;
  if (child_l == NULL)
    hl = 0;
  else
    hl = child_l->getHeight() + 1;
  if (child_r == NULL)
    hr = 0;
  else
    hr = child_r->getHeight() + 1;
  if (hl > hr)
    return hl;
  else
    return hr;
}

void Node::print() {
  cout << "node " << id << ": ";
  cout << "parent = " << getParentId() << ", ";
  cout << "sibling = " << getSiblingId() << ", ";
  cout << "degree = " << getDegree() << ", ";
  int depth = getDepth();
  cout << "depth = " << depth << ", ";
  int height = getHeight();
  cout << "height = " << height << ", ";
  if (depth == 0)
    cout << "root";
  else if (height == 0)
    cout << "reaf";
  else
    cout << "internal node";
  cout << endl;
}

class BinaryTree {
private:
  Node *root[26];

public:
  BinaryTree() {
    for (int i = 0; i < 26; i++)
      root[i] = NULL;
  }
  Node *setNode(int id);
  void setChildl(Node *node, int idl);
  void setChildr(Node *node, int idr);
};

Node *BinaryTree::setNode(int id) {
  Node *node;
  int i;
  for (i = 0; root[i] != NULL; i++) {
    if ((node = root[i]->searchNode(id)) != NULL)
      return node;
  }
  root[i] = new Node(id);
  return root[i];
}

void BinaryTree::setChildl(Node *node, int idl) {
  if (idl == -1)
    return;

  int i;
  for (i = 0; root[i] != NULL; i++) {
    if (root[i]->searchNode(idl) != NULL) {
      node->setChildl(root[i]);
      int j;
      for (j = i; root[j + 1] != NULL; j++) {
        root[j] = root[j + 1];
      }
      if (j != 0)
        root[j] = NULL;
      return;
    }
  }

  node->setChildl(new Node(idl));
  return;
}

void BinaryTree::setChildr(Node *node, int idr) {
  if (idr == -1)
    return;

  int i;
  for (i = 0; root[i] != NULL; i++) {
    if (root[i]->searchNode(idr) != NULL) {
      node->setChildr(root[i]);
      int j;
      for (j = i; root[j + 1] != NULL; j++) {
        root[j] = root[j + 1];
      }
      if (j != 0)
        root[j] = NULL;
      return;
    }
  }

  node->setChildr(new Node(idr));
  return;
}

int main(void) {
  BinaryTree bt;
  Node *node_list[25];

  int n;
  cin >> n;

  for (int i = 0; i < n; i++) {
    int id, idl, idr;
    cin >> id >> idl >> idr;
    node_list[id] = bt.setNode(id);
    bt.setChildl(node_list[i], idl);
    bt.setChildr(node_list[i], idr);
  }

  for (int i = 0; i < n; i++) {
    node_list[i]->print();
  }

  return 0;
}