Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.89 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.  
  204. void CNodeDynamic::vAddNewChild()
  205. {
  206.     CNodeDynamic * toAdd = &CNodeDynamic();
  207.     toAdd->parent = this;
  208.     v_children.push_back(toAdd);
  209.  
  210.     cout << "Node Added to node " << i_val  << endl;
  211.  
  212. }
  213.  
  214.  
  215.  
  216. CNodeDynamic* CNodeDynamic::pcGetChild(int iChildOffset)
  217. {
  218.     if (iChildOffset < 0 || iChildOffset >= v_children.size()) {
  219.         return NULL;
  220.     }
  221.  
  222.     return v_children[iChildOffset];
  223. }
  224.  
  225. void CNodeDynamic::vPrintAllBelow() {
  226.  
  227.     vPrint();
  228.     for (int i = 0; i < v_children.size(); i++) {
  229.         this->pcGetChild(i)->vPrintAllBelow();
  230.     }
  231.  
  232. }
  233.  
  234. /**void CNodeDynamic::vPrintUp()
  235. {
  236.     //vPrint();
  237.  
  238.     if (parent != NULL) {
  239.         parent->vPrint();
  240.         parent->vPrintUp();
  241.     }
  242. }*/
  243.  
  244.  
  245. ///////////////////////////////////////////////////////
  246.  
  247. class CTreeDynamic
  248. {
  249. public:
  250.     CTreeDynamic();
  251.     ~CTreeDynamic() { delete c_root; };
  252.     CNodeDynamic* pcGetRoot() { return c_root; }
  253.     void vPrintTree();
  254.     bool bMoveSubtree(CNodeDynamic* pcParentNode, CNodeDynamic* pcNewChildNode, CNodeDynamic* pc2ParentNode);
  255. private:
  256.     CNodeDynamic* c_root;
  257. };
  258.  
  259. CTreeDynamic::CTreeDynamic() {
  260.     CNodeDynamic root = CNodeDynamic();
  261.     c_root = &root;
  262. }
  263.  
  264. void CTreeDynamic::vPrintTree() {
  265.     c_root->vPrintAllBelow();
  266. }
  267.  
  268. //6
  269.  
  270. bool CTreeDynamic::bMoveSubtree(CNodeDynamic* pcParentNode, CNodeDynamic* pcNewChildNode, CNodeDynamic* pc2ParentNode) {
  271.     pcParentNode->vAddNewChild(pcNewChildNode);
  272.     pc2ParentNode->removeChild(pcNewChildNode);
  273.  
  274.     return true;
  275. }
  276.  
  277.  
  278.  
  279. //////////////////////////////////////////////////
  280. void  v_tree_test() {
  281.     CNodeStatic  c_root;
  282.  
  283.     c_root.vAddNewChild();                                          //root:0
  284.     c_root.vAddNewChild();                                    //    1       2  
  285.                                                                    
  286.     c_root.pcGetChild(0)->vSetValue(1);                     // 11    12      21  22
  287.     c_root.pcGetChild(1)->vSetValue(2);
  288.  
  289.     c_root.pcGetChild(0)->vAddNewChild();
  290.     c_root.pcGetChild(0)->vAddNewChild();
  291.  
  292.     c_root.pcGetChild(0)->pcGetChild(0)->vSetValue(11);
  293.     c_root.pcGetChild(0)->pcGetChild(1)->vSetValue(12);
  294.  
  295.  
  296.     c_root.pcGetChild(1)->vAddNewChild();
  297.     c_root.pcGetChild(1)->vAddNewChild();
  298.  
  299.     c_root.pcGetChild(1)->pcGetChild(0)->vSetValue(21);
  300.     c_root.pcGetChild(1)->pcGetChild(1)->vSetValue(22);
  301.  
  302.     c_root.vPrintAllBelow();
  303.     cout << endl << "vPrint up";
  304.     c_root.pcGetChild(1)->pcGetChild(0)->vPrintUp();
  305.  
  306. }//void  v_tree_test()
  307. void testMovingStaticTree() {
  308.     cout << "\n\n~~~~Test Move Static Tree~~~~";
  309.     cout << "\n ~Allocating tree 1\n";
  310.     CTreeStatic c_tree;
  311.     CNodeStatic c_root = *c_tree.pcGetRoot();
  312.     c_root.vAddNewChild();
  313.     c_root.vAddNewChild();
  314.     c_root.pcGetChild(0)->vSetValue(1);
  315.     c_root.pcGetChild(1)->vSetValue(2);
  316.     c_root.pcGetChild(0)->vAddNewChild();
  317.     c_root.pcGetChild(0)->vAddNewChild();
  318.     c_root.pcGetChild(0)->pcGetChild(0)->vSetValue(11);
  319.     c_root.pcGetChild(0)->pcGetChild(1)->vSetValue(12);
  320.     c_root.pcGetChild(1)->vAddNewChild();
  321.     c_root.pcGetChild(1)->vAddNewChild();
  322.     c_root.pcGetChild(1)->pcGetChild(0)->vSetValue(21);
  323.     c_root.pcGetChild(1)->pcGetChild(1)->vSetValue(22);
  324.  
  325.     cout << "Print Below: ";
  326.     c_root.vPrintAllBelow();
  327.     cout << "\nPrint Up: ";
  328.     c_root.pcGetChild(0)->pcGetChild(1)->vPrintUp();
  329.  
  330.     cout << "\n\n   ~Allocating tree 3\n";
  331.     CTreeStatic c_tree3;
  332.     CNodeStatic c_root3 = *c_tree3.pcGetRoot();
  333.     c_root3.vAddNewChild();
  334.     c_root3.vAddNewChild();
  335.     c_root3.pcGetChild(0)->vSetValue(5);
  336.     c_root3.pcGetChild(1)->vSetValue(6);
  337.     c_root3.pcGetChild(0)->vAddNewChild();
  338.     c_root3.pcGetChild(0)->vAddNewChild();
  339.     c_root3.pcGetChild(0)->pcGetChild(0)->vSetValue(51);
  340.     c_root3.pcGetChild(0)->pcGetChild(1)->vSetValue(52);
  341.     c_root3.pcGetChild(1)->vAddNewChild();
  342.     c_root3.pcGetChild(1)->vAddNewChild();
  343.     c_root3.pcGetChild(1)->pcGetChild(0)->vSetValue(61);
  344.     c_root3.pcGetChild(1)->pcGetChild(1)->vSetValue(62);
  345.  
  346.     cout << "Print Below";
  347.     c_root3.vPrintAllBelow();
  348.  
  349.     cout << "\n\n   ~Moving tree 3 to tree 1 \n";
  350.     c_tree.bMoveSubtree(&c_root, c_root3.pcGetChild(0), &c_root3);
  351.  
  352.     cout << "\nPrint Below TREE1 after moving subtree: ";
  353.     c_root.vPrintAllBelow();
  354.     cout << "\nPrint Below TREE3 after moving subtree:";
  355.     c_root3.vPrintAllBelow();
  356.     cout << "\n";
  357. }
  358.  
  359.  
  360.  
  361. void testMovingDynamicTree() {
  362.     cout << "\n\n~~~~Test Move Dynamic Tree~~~~";
  363.     cout << "\n ~Allocating tree 1\n";
  364.     CTreeDynamic c_tree;
  365.     CNodeDynamic c_root = *c_tree.pcGetRoot();
  366.     c_root.vAddNewChild();
  367.     c_root.vAddNewChild();
  368.     c_root.pcGetChild(0)->vSetValue(1);
  369.     c_root.pcGetChild(1)->vSetValue(2);
  370.     c_root.pcGetChild(0)->vAddNewChild();
  371.     c_root.pcGetChild(0)->vAddNewChild();
  372.     c_root.pcGetChild(0)->pcGetChild(0)->vSetValue(11);
  373.     c_root.pcGetChild(0)->pcGetChild(1)->vSetValue(12);
  374.     c_root.pcGetChild(1)->vAddNewChild();
  375.     c_root.pcGetChild(1)->vAddNewChild();
  376.     c_root.pcGetChild(1)->pcGetChild(0)->vSetValue(21);
  377.     c_root.pcGetChild(1)->pcGetChild(1)->vSetValue(22);
  378.  
  379.     cout << "Print Below: ";
  380.     c_root.vPrintAllBelow();
  381.  
  382.     cout << "\n\n   ~Allocating tree 3\n";
  383.     CTreeDynamic c_tree3;
  384.     CNodeDynamic c_root3 = *c_tree3.pcGetRoot();
  385.     c_root3.vAddNewChild();
  386.     c_root3.vAddNewChild();
  387.     c_root3.pcGetChild(0)->vSetValue(5);
  388.     c_root3.pcGetChild(1)->vSetValue(6);
  389.     c_root3.pcGetChild(0)->vAddNewChild();
  390.     c_root3.pcGetChild(0)->vAddNewChild();
  391.     c_root3.pcGetChild(0)->pcGetChild(0)->vSetValue(51);
  392.     c_root3.pcGetChild(0)->pcGetChild(1)->vSetValue(52);
  393.     c_root3.pcGetChild(1)->vAddNewChild();
  394.     c_root3.pcGetChild(1)->vAddNewChild();
  395.     c_root3.pcGetChild(1)->pcGetChild(0)->vSetValue(61);
  396.     c_root3.pcGetChild(1)->pcGetChild(1)->vSetValue(62);
  397.  
  398.     cout << "Print Below";
  399.     c_root3.vPrintAllBelow();
  400.  
  401.     cout << "\n\n   ~Moving tree 3 to tree 1 \n";
  402.     c_tree.bMoveSubtree(&c_root, c_root3.pcGetChild(0), &c_root3);
  403.  
  404.     cout << "\nPrint Below TREE1 after moving subtree: ";
  405.     c_root.vPrintAllBelow();
  406.     cout << "\nPrint Below TREE3 after moving subtree:";
  407.     c_root3.vPrintAllBelow();
  408.     cout << "\n";
  409. }
  410. void testPrintUp()
  411. {
  412.     CNodeStatic c_root;
  413.     c_root.vAddNewChild();
  414.     c_root.vAddNewChild();
  415.     c_root.pcGetChild(0)->vSetValue(1);
  416.     c_root.pcGetChild(1)->vSetValue(2);
  417.     c_root.pcGetChild(0)->vAddNewChild();
  418.     c_root.pcGetChild(0)->vAddNewChild();
  419.     c_root.pcGetChild(0)->pcGetChild(0)->vSetValue(11);
  420.     c_root.pcGetChild(0)->pcGetChild(1)->vSetValue(12);
  421.     c_root.pcGetChild(1)->vAddNewChild();
  422.     c_root.pcGetChild(1)->vAddNewChild();
  423.     c_root.pcGetChild(1)->pcGetChild(0)->vSetValue(21);
  424.     c_root.pcGetChild(1)->pcGetChild(1)->vSetValue(22);
  425.  
  426.     cout << "\n\n~~~~Test Print up~~~~";
  427.     cout << "\nPrint Down:\n";
  428.     c_root.vPrintAllBelow();
  429.     cout << "\nPrint Up:\n";
  430.     c_root.pcGetChild(0)->pcGetChild(1)->vPrintUp();
  431. }
  432.  
  433.  
  434.  
  435.  
  436. int main()
  437. {
  438.     v_tree_test();
  439.     testMovingStaticTree();
  440.     testMovingDynamicTree();
  441. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement