Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.11 KB | None | 0 0
  1. // EPT5_2017.cpp : This file contains the 'main' function. Program execution begins and ends there.
  2. //
  3.  
  4. // EPT5_CM.cpp : This file contains the 'main' function. Program execution begins and ends there.
  5. //
  6. //#include "pch.h"
  7. #include <iostream>
  8. #include <vector>
  9. #include <string>
  10. using namespace std;
  11.  
  12.  
  13. class  CNodeStatic {
  14. public:
  15.  
  16.     CNodeStatic() { i_val = 0; parent = NULL; };
  17.     ~CNodeStatic();
  18.  
  19.     void  vSetValue(int  iNewVal) { cout << "changing node id: " << i_val<< " to "<< iNewVal<<endl;  i_val = iNewVal; };
  20.  
  21.     int  iGetChildrenNumber() { return(v_children.size()); };
  22.     void  vAddNewChild();
  23.     CNodeStatic* pcGetChild(int  iChildOffset);
  24.     CNodeStatic* getParent() { return parent; };
  25.    
  26.     void  vPrint() { cout << " " << i_val; };
  27.     void  vPrintAllBelow();
  28.     void  vPrintUp();
  29.     void vAddNewChild(CNodeStatic* newChild);
  30.     void removeChild(CNodeStatic* oldChild);
  31.  
  32. private:
  33.     vector<CNodeStatic>  v_children;
  34.     int  i_val;
  35.     CNodeStatic* parent;
  36.     //void setParent();
  37. };//class  CNodeStatic
  38.  
  39. CNodeStatic::~CNodeStatic()
  40. {
  41.    
  42. }
  43.  
  44.  
  45.  
  46. void CNodeStatic::vAddNewChild()
  47. {
  48.     CNodeStatic toAdd = CNodeStatic();
  49.     toAdd.parent = this;
  50.     v_children.push_back(toAdd);
  51.  
  52.     cout << "Node Added to node " << i_val  << endl;
  53.  
  54. }
  55.  
  56.  
  57.  
  58. CNodeStatic* CNodeStatic::pcGetChild(int iChildOffset)
  59. {
  60.     if (iChildOffset < 0 || iChildOffset >= v_children.size()) {
  61.         return NULL;
  62.     }
  63.  
  64.     return &v_children[iChildOffset];
  65. }
  66.  
  67. void CNodeStatic::vPrintAllBelow() {
  68.    
  69.     vPrint();
  70.     for (int i = 0; i < v_children.size(); i++) {
  71.         this->pcGetChild(i)->vPrintAllBelow();
  72.     }
  73.  
  74. }
  75.  
  76.  
  77.  
  78.  
  79. void CNodeStatic::vPrintUp()
  80. {
  81.     //vPrint();
  82.    
  83.     if (parent != NULL) {
  84.         parent->vPrint();
  85.         parent->vPrintUp();
  86.     }
  87. }
  88.  
  89.  
  90. //6
  91. void CNodeStatic::vAddNewChild(CNodeStatic* newChild) {
  92.  
  93.     newChild->parent=this;
  94.    
  95.     v_children.push_back(*newChild);
  96.    
  97. }
  98. void CNodeStatic::removeChild(CNodeStatic* oldChild) {
  99.  
  100.     for (int i = 0; i < iGetChildrenNumber(); i++) {
  101.         if (v_children[i].i_val == oldChild->i_val)
  102.             v_children.erase(v_children.begin() + 1);
  103.     }
  104. }
  105.  
  106.  
  107.  
  108.  
  109. /////////////////////////////////////////////////////////////////////////////////////////
  110.  
  111. class  CTreeStatic {
  112. public:
  113.     CTreeStatic();
  114.     ~CTreeStatic() {};
  115.  
  116.     CNodeStatic* pcGetRoot() { return(&c_root); }
  117.     void  vPrintTree();
  118.     bool bMoveSubtree(CNodeStatic* pcParentNode, CNodeStatic* pcNewChildNode, CNodeStatic* pc2ParentNode);
  119. private:
  120.     CNodeStatic  c_root;
  121.  
  122.  
  123.  
  124. };//class  CTreeStatic
  125.  
  126. CTreeStatic::CTreeStatic() {
  127.     c_root = CNodeStatic();
  128. }
  129.  
  130. void CTreeStatic::vPrintTree() {
  131.     c_root.vPrintAllBelow();
  132. }
  133.  
  134.  
  135. bool CTreeStatic::bMoveSubtree(CNodeStatic* pcParentNode, CNodeStatic* pcNewChildNode, CNodeStatic* pc2ParentNode) {
  136.     pcParentNode->vAddNewChild(pcNewChildNode);
  137.     pc2ParentNode->removeChild(pcNewChildNode);
  138.  
  139.     return true;
  140. }
  141. ////////////////////////////////////////////////////////////////////////////////////
  142.                          //DYNAMIC CLASSES//
  143.  
  144. class CNodeDynamic
  145. {
  146. public:
  147.     CNodeDynamic() { i_val = 0; parent = NULL;};
  148.     ~CNodeDynamic();
  149.     void vSetValue(int iNewVal) { cout << "changing node id: " << i_val << " to " << iNewVal << endl; i_val = iNewVal; };
  150.     int iGetChildrenNumber() { return v_children.size(); };
  151.     void vAddNewChild(); //
  152.     CNodeDynamic* pcGetChild(int iChildOffset); //
  153.     void vPrint() { cout << " " << i_val; };
  154.     void vPrintAllBelow(); //
  155.     //void  vPrintUp();
  156.     void vAddNewChild(CNodeDynamic* newChild);
  157.     void removeChild(CNodeDynamic* oldChild);
  158. private:
  159.     vector<CNodeDynamic*> v_children;
  160.     int i_val;
  161.     CNodeDynamic* parent;
  162. };
  163.  
  164. void CNodeDynamic::vAddNewChild(CNodeDynamic* newChild) {
  165.     v_children.push_back(newChild);
  166. }
  167. void CNodeDynamic::removeChild(CNodeDynamic* oldChild) {
  168.     for (int i = 0; i < iGetChildrenNumber(); i++) {
  169.         if ((*v_children[i]).i_val == oldChild->i_val)
  170.             v_children.erase(v_children.begin() + 1);
  171.     }
  172. }
  173.  
  174. //CNodeDynamic::~CNodeDynamic()
  175. //{
  176. //  if (!v_children.empty()) {
  177. //      for (int i = 0; i < v_children.size(); i++) {
  178. //          delete this->pcGetChild(i);
  179. //      }
  180. //  }
  181. //  delete[] this;
  182. //}
  183.  
  184. //CNodeDynamic::~CNodeDynamic() {
  185.  
  186.  
  187.     //if (iGetChildrenNumber() == 0) {
  188.         //delete[] this;
  189.     //}
  190. //  else {
  191.  
  192.     //}
  193.  
  194.     /*if (iGetChildrenNumber() != 0) {
  195.         for (int i = 0; i < iGetChildrenNumber(); i++) {
  196.             v_children[i]->~CNodeDynamic();
  197.         }
  198.     }
  199.     else {
  200.         delete[] this;
  201.     }*/
  202.  
  203.     //cout << "deleting node";
  204.     //v_children.clear();
  205. //}
  206.  
  207. CNodeDynamic::~CNodeDynamic() {
  208.  
  209.     if (iGetChildrenNumber() == 0) {
  210.         return;
  211.  
  212.     }
  213.     else {
  214.         for (int i = 0; i < iGetChildrenNumber(); i++) {
  215.             v_children[i]->~CNodeDynamic();
  216.             delete v_children[i];
  217.         }
  218.     }
  219.  
  220. }
  221.  
  222.  
  223. void CNodeDynamic::vAddNewChild()
  224. {
  225.     CNodeDynamic * toAdd =new CNodeDynamic(); //
  226.     toAdd->parent = this;
  227.     v_children.push_back(toAdd);
  228.  
  229.     cout << "Node Added to node " << i_val  << endl;
  230.  
  231. }
  232.  
  233.  
  234.  
  235. CNodeDynamic* CNodeDynamic::pcGetChild(int iChildOffset)
  236. {
  237.     if (iChildOffset < 0 || iChildOffset >= v_children.size()) {
  238.         return NULL;
  239.     }
  240.  
  241.     return v_children[iChildOffset];
  242. }
  243.  
  244. void CNodeDynamic::vPrintAllBelow() {
  245.  
  246.     vPrint();
  247.     for (int i = 0; i < v_children.size(); i++) {
  248.         this->pcGetChild(i)->vPrintAllBelow();
  249.     }
  250.  
  251. }
  252.  
  253. /**void CNodeDynamic::vPrintUp()
  254. {
  255.     //vPrint();
  256.  
  257.     if (parent != NULL) {
  258.         parent->vPrint();
  259.         parent->vPrintUp();
  260.     }
  261. }*/
  262.  
  263.  
  264. ///////////////////////////////////////////////////////
  265.  
  266. class CTreeDynamic
  267. {
  268. public:
  269.     CTreeDynamic();
  270.     ~CTreeDynamic() { delete c_root; };
  271.     CNodeDynamic* pcGetRoot() { return c_root; }
  272.     void vPrintTree();
  273.     bool bMoveSubtree(CNodeDynamic* pcParentNode, CNodeDynamic* pcNewChildNode, CNodeDynamic* pc2ParentNode);
  274. private:
  275.     CNodeDynamic* c_root;
  276. };
  277.  
  278. CTreeDynamic::CTreeDynamic() {
  279.     CNodeDynamic root =  CNodeDynamic();
  280.     c_root = &root;
  281. }
  282.  
  283. void CTreeDynamic::vPrintTree() {
  284.     c_root->vPrintAllBelow();
  285. }
  286.  
  287. //6
  288.  
  289. bool CTreeDynamic::bMoveSubtree(CNodeDynamic* pcParentNode, CNodeDynamic* pcNewChildNode, CNodeDynamic* pc2ParentNode) {
  290.     pcParentNode->vAddNewChild(pcNewChildNode);
  291.     pc2ParentNode->removeChild(pcNewChildNode);
  292.  
  293.     return true;
  294. }
  295.  
  296.  
  297.  
  298. //////////////////////////////////////////////////
  299. void  v_tree_test() {
  300.     CNodeStatic  c_root;
  301.  
  302.     c_root.vAddNewChild();                                          //root:0
  303.     c_root.vAddNewChild();                                    //    1       2  
  304.                                                                    
  305.     c_root.pcGetChild(0)->vSetValue(1);                     // 11    12      21  22
  306.     c_root.pcGetChild(1)->vSetValue(2);
  307.  
  308.     c_root.pcGetChild(0)->vAddNewChild();
  309.     c_root.pcGetChild(0)->vAddNewChild();
  310.  
  311.     c_root.pcGetChild(0)->pcGetChild(0)->vSetValue(11);
  312.     c_root.pcGetChild(0)->pcGetChild(1)->vSetValue(12);
  313.  
  314.  
  315.     c_root.pcGetChild(1)->vAddNewChild();
  316.     c_root.pcGetChild(1)->vAddNewChild();
  317.  
  318.     c_root.pcGetChild(1)->pcGetChild(0)->vSetValue(21);
  319.     c_root.pcGetChild(1)->pcGetChild(1)->vSetValue(22);
  320.  
  321.     c_root.vPrintAllBelow();
  322.     cout << endl << "vPrint up";
  323.     c_root.pcGetChild(1)->pcGetChild(0)->vPrintUp();
  324.  
  325. }//void  v_tree_test()
  326. void testMovingStaticTree() {
  327.     cout << "\n\nTesting Move Static Tree";
  328.     cout << "\n tree 1\n";
  329.     CTreeStatic c_tree;
  330.     CNodeStatic c_root = *c_tree.pcGetRoot();
  331.     c_root.vAddNewChild();
  332.     c_root.vAddNewChild();
  333.     c_root.pcGetChild(0)->vSetValue(1);
  334.     c_root.pcGetChild(1)->vSetValue(2);
  335.     c_root.pcGetChild(0)->vAddNewChild();
  336.     c_root.pcGetChild(0)->vAddNewChild();
  337.     c_root.pcGetChild(0)->pcGetChild(0)->vSetValue(11);
  338.     c_root.pcGetChild(0)->pcGetChild(1)->vSetValue(12);
  339.     c_root.pcGetChild(1)->vAddNewChild();
  340.     c_root.pcGetChild(1)->vAddNewChild();
  341.     c_root.pcGetChild(1)->pcGetChild(0)->vSetValue(21);
  342.     c_root.pcGetChild(1)->pcGetChild(1)->vSetValue(22);
  343.  
  344.     cout << "Print Below: ";
  345.     c_root.vPrintAllBelow();
  346.     cout << "\nPrint Up: ";
  347.     c_root.pcGetChild(0)->pcGetChild(1)->vPrintUp();
  348.  
  349.     cout << "\n\ tree 3\n";
  350.     CTreeStatic c_tree3;
  351.     CNodeStatic c_root3 = *c_tree3.pcGetRoot();
  352.     c_root3.vAddNewChild();
  353.     c_root3.vAddNewChild();
  354.     c_root3.pcGetChild(0)->vSetValue(31);
  355.     c_root3.pcGetChild(1)->vSetValue(32);
  356.     c_root3.pcGetChild(0)->vAddNewChild();
  357.     c_root3.pcGetChild(0)->vAddNewChild();
  358.     c_root3.pcGetChild(0)->pcGetChild(0)->vSetValue(33);
  359.     c_root3.pcGetChild(0)->pcGetChild(1)->vSetValue(34);
  360.     c_root3.pcGetChild(1)->vAddNewChild();
  361.     c_root3.pcGetChild(1)->vAddNewChild();
  362.     c_root3.pcGetChild(1)->pcGetChild(0)->vSetValue(35);
  363.     c_root3.pcGetChild(1)->pcGetChild(1)->vSetValue(36);
  364.  
  365.     cout << "Print Below";
  366.     c_root3.vPrintAllBelow();
  367.  
  368.     cout << "\n\n Moving tree 3 to tree 1 \n";
  369.     c_tree.bMoveSubtree(&c_root, c_root3.pcGetChild(0), &c_root3);
  370.  
  371.     cout << "\nTREE1 after moving subtree: ";
  372.     c_root.vPrintAllBelow();
  373.     cout << "\nTREE3 after moving subtree:";
  374.     c_root3.vPrintAllBelow();
  375.     cout << "\n";
  376. }
  377.  
  378.  
  379.  
  380. void testMovingDynamicTree() {
  381.     cout << "\n\nTesting Move Dynamic Tree";
  382.     cout << "\n Creating tree 1\n";
  383.     CTreeDynamic c_tree;
  384.     CNodeDynamic c_root = *c_tree.pcGetRoot();
  385.     c_root.vAddNewChild();
  386.     c_root.vAddNewChild();
  387.     c_root.pcGetChild(0)->vSetValue(11);
  388.     c_root.pcGetChild(1)->vSetValue(12);
  389.     c_root.pcGetChild(0)->vAddNewChild();
  390.     c_root.pcGetChild(0)->vAddNewChild();
  391.     c_root.pcGetChild(0)->pcGetChild(0)->vSetValue(13);
  392.     c_root.pcGetChild(0)->pcGetChild(1)->vSetValue(14);
  393.     c_root.pcGetChild(1)->vAddNewChild();
  394.     c_root.pcGetChild(1)->vAddNewChild();
  395.     c_root.pcGetChild(1)->pcGetChild(0)->vSetValue(15);
  396.     c_root.pcGetChild(1)->pcGetChild(1)->vSetValue(16);
  397.  
  398.     cout << "Print Below: ";
  399.     c_root.vPrintAllBelow();
  400.  
  401.     cout << "\n\nCreating tree 3\n";
  402.     CTreeDynamic c_tree3;
  403.     CNodeDynamic c_root3 = *c_tree3.pcGetRoot();
  404.     c_root3.vAddNewChild();
  405.     c_root3.vAddNewChild();
  406.     c_root3.pcGetChild(0)->vSetValue(31);
  407.     c_root3.pcGetChild(1)->vSetValue(32);
  408.     c_root3.pcGetChild(0)->vAddNewChild();
  409.     c_root3.pcGetChild(0)->vAddNewChild();
  410.     c_root3.pcGetChild(0)->pcGetChild(0)->vSetValue(33);
  411.     c_root3.pcGetChild(0)->pcGetChild(1)->vSetValue(34);
  412.     c_root3.pcGetChild(1)->vAddNewChild();
  413.     c_root3.pcGetChild(1)->vAddNewChild();
  414.     c_root3.pcGetChild(1)->pcGetChild(0)->vSetValue(35);
  415.     c_root3.pcGetChild(1)->pcGetChild(1)->vSetValue(36);
  416.  
  417.     cout << "Print Below";
  418.     c_root3.vPrintAllBelow();
  419.  
  420.     cout << "\n\nMoving tree 3 to tree 1 \n";
  421.     c_tree.bMoveSubtree(&c_root, c_root3.pcGetChild(0), &c_root3);
  422.  
  423.     cout << "\nTREE1 after moving subtree: ";
  424.     c_root.vPrintAllBelow();
  425.     cout << "\nTREE3 after moving subtree:";
  426.     c_root3.vPrintAllBelow();
  427.     cout << "\n";
  428. }
  429. void testPrintUp()
  430. {
  431.     CNodeStatic c_root;
  432.  
  433.     c_root.vAddNewChild();
  434.     c_root.vAddNewChild();
  435.     c_root.pcGetChild(0)->vSetValue(1);
  436.     c_root.pcGetChild(1)->vSetValue(2);
  437.     c_root.pcGetChild(0)->vAddNewChild();
  438.     c_root.pcGetChild(0)->vAddNewChild();
  439.     c_root.pcGetChild(0)->pcGetChild(0)->vSetValue(11);
  440.     c_root.pcGetChild(0)->pcGetChild(1)->vSetValue(12);
  441.     c_root.pcGetChild(1)->vAddNewChild();
  442.     c_root.pcGetChild(1)->vAddNewChild();
  443.     c_root.pcGetChild(1)->pcGetChild(0)->vSetValue(21);
  444.     c_root.pcGetChild(1)->pcGetChild(1)->vSetValue(22);
  445.  
  446.     cout << "\n\nTesting Print up";
  447.     cout << "\nPrint Down:\n";
  448.     c_root.vPrintAllBelow();
  449.     cout << "\nPrint Up:\n";
  450.     c_root.pcGetChild(0)->pcGetChild(1)->vPrintUp();
  451. }
  452.  
  453.  
  454.  
  455.  
  456. int main()
  457. {
  458.     v_tree_test();
  459.     testMovingStaticTree();
  460.     testMovingDynamicTree();
  461.     testPrintUp();
  462. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement