Advertisement
jakmaj97

CNode Exam

Feb 8th, 2018
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.04 KB | None | 0 0
  1. class CNode
  2. {
  3. public:
  4.     CNode(int iVal) { piVal = new int(iVal); }
  5.     void v_add(CNode *pcKid) { vKids.push_back(pcKid); }
  6.     void v_set_val(int iVal) { *piVal = iVal; }
  7.     void v_print()
  8.     {
  9.         if (!vKids.empty())
  10.             for (int i = 0; i < vKids.size(); i++)
  11.                 vKids[i]->v_print();
  12.         cout << *piVal << " ";
  13.     }
  14.     void v_delete_node()
  15.     {
  16.         if (!vKids.empty())
  17.             for (int i = 0; i < vKids.size(); i++)
  18.                 vKids[i]->v_delete_node();
  19.         delete piVal;
  20.     }
  21. private:
  22.     int *piVal;
  23.     vector<CNode*> vKids;
  24. };
  25. class CTree
  26. {
  27. public:
  28.     CTree() { pcRoot = new CNode(0); }
  29.     ~CTree() { v_delete_tree(); }
  30.     void v_delete_tree() { pcRoot->v_delete_node(); }
  31.     CNode *pc_root() { return pcRoot; }
  32.     void vPrint() { pcRoot->v_print(); }
  33. private:
  34.     CNode *pcRoot;
  35. };
  36. void v_test()
  37. {
  38.     CTree cT0;
  39.     cT0.pc_root()->v_set_val(5);
  40.     cT0.pc_root()->v_add(new CNode(1));
  41.  
  42.     CNode *pcN0;
  43.     pcN0 = new CNode(3);
  44.     pcN0->v_add(new CNode(4));
  45.     cT0.pc_root()->v_add(pcN0);
  46.  
  47.     CTree *pc_copy = new CTree(cT0);
  48.     cT0.vPrint();
  49.     pc_copy->vPrint();
  50.     //delete pc_copy;
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement