Advertisement
Guest User

Untitled

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