// TreeNode.cpp

#include "TreeNode.h"

// Default constructor:
TreeNode::TreeNode()
{
  int i = 0;

  _data = 0;
  _numChildren = 0;
  _children = new TreeNode*[giMaxChildren];

  for (i = 0; i < giMaxChildren; i++)
  {
    _children[i] = NULL;
  }
}

// Data constructor:
TreeNode::TreeNode(int d)
{
  int i = 0;

  _data = d;
  _numChildren = 0;
  _children = new TreeNode*[giMaxChildren];

  for (i = 0; i < giMaxChildren; i++)
  {
    _children[i] = NULL;
  }
}

// Mutators:
void TreeNode::addChild(TreeNode* tn)
{
  if (_numChildren < giMaxChildren)
  {
    _children[_numChildren] = tn;
    _numChildren++;
  }
  else
  {
    cout << "Can't add child: too many children." << endl;
  }
}

// Accessors:
int TreeNode::getData()
{
  return _data;
}

TreeNode* TreeNode::getChild(int i)
{
  return _children[i];
}

TreeNode* TreeNode::findChild(int data)
{
  int i = 0;

  TreeNode* foundChild = NULL;

  if (_data == data)
  {
    foundChild = this;
  }
  else
  {
    for (i = 0; i < _numChildren; i++)
    {
      foundChild = _children[i]->findChild(data);
      if (foundChild != NULL) break;                          // Break out of for()
    }
  }
  return foundChild;
}
