Specification coding question 2

Write the implementation of the following IntTree class (a tree data structure for storing integers).
/* Name:    inttree.h
   Author:  Leo Liberti
*/

#ifndef _INTTREEH
#define _INTTREEH

#include<vector>

class IntTree {
 public:
  // constructs an empty tree
  IntTree();

  // destroys a tree
  ~IntTree();

  // add a new subnode with value n
  void addSubNode(int n);

  // get the number of subtrees
  int getNumberOfSubTrees(void);

  // get a pointer to the i-th subtree
  IntTree* getSubTree(int i);

  // removes a subtree without deleting it (and returning it)
  IntTree* removeSubTree(int i);
  
  // adds a tree as a subtree of the current tree
  void addSubTree(IntTree* t);

  // get pointer to the parent tree
  IntTree* getParent(void);

  // set parent pointer
  void setParent(IntTree* p);

  // get value of current tree
  int getValue(void);
  
  // set value in current tree
  void setValue(int n);

  // find the first subtree containing given value by depth-first search
  // and put its pointer in 'found'
  // PRECONDITION: found must be NULL on entry
  // POSTCONDITION: if found is NULL on exit, value was not found
  void findValue(int n, IntTree* &found);

  // print a tree to standard output
  void print(void);

 protected:

  // value stored in the node (tree)
  int value;

  // pointer to parent tree
  IntTree* parent;

  // vector of subtree pointers
  std::vector<IntTree*> subTree;

 private:

  // iterator sometimes used in methods
  std::vector<IntTree*>::iterator stit;
};

#endif
Test it with with the following main program:
/* name:   itmain.cxx
   author: Leo Liberti
*/

#include <iostream>
#include "inttree.h"

int print(IntTree& t) {
  using namespace std;
  cout << "T = ";
  t.print();
  cout << endl;
}

int main(int argc, char** argv) {
  using namespace std;
  IntTree myTree;
  myTree.setValue(0);
  myTree.addSubNode(1);
  myTree.addSubNode(2);
  myTree.addSubNode(3);
  print(myTree);

  int st = myTree.getNumberOfSubTrees();
  for(int i = 0; i < st; i++) {
    IntTree* t = myTree.getSubTree(i);
    t->addSubNode(2*i + st);
    t->addSubNode(2*i + 1 + st);
  }
  print(myTree);

  IntTree* anotherTree = new IntTree;
  anotherTree->setValue(-1);
  anotherTree->addSubNode(-2);
  anotherTree->addSubNode(-3);
  
  IntTree* t = NULL;
  myTree.findValue(2, t);
  if (t) {
    st = t->getNumberOfSubTrees();
    IntTree* u = t->removeSubTree(st - 1);
    print(myTree);
    delete u;
    t->addSubTree(anotherTree);
  }  
  print(myTree);
  
  myTree.findValue(-3, t);
  int level = 1;
  while(t != NULL) {
    t = t->getParent();
    level++;
  }
  cout << "node (-3) is at level " << level << " (root has level 1)" << endl;
  return 0;
}
and check that the output is as follows.
T = 0 ( 1 2 3 ) 
T = 0 ( 1 ( 3 4 ) 2 ( 5 6 ) 3 ( 7 8 ) ) 
T = 0 ( 1 ( 3 4 ) 2 ( 5 ) 3 ( 7 8 ) ) 
T = 0 ( 1 ( 3 4 ) 2 ( 5 -1 ( -2 -3 ) ) 3 ( 7 8 ) ) 
node (-3) is at level 3 (root has level 1)



Subsections

Leo Liberti 2008-01-12