karol_dziachan

Dynamic Tree implementation (C++)

Nov 20th, 2020 (edited)
1,052
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.97 KB | None | 0 0
  1. class CNodeDynamic
  2. {
  3. public:
  4.     CNodeDynamic() { i_val = 0; pc_parent_node = NULL; i_level = 0; };
  5.     CNodeDynamic(const CNodeDynamic* pcOther);
  6.     ~CNodeDynamic();
  7.     void vSetValue(int iNewVal) { i_val = iNewVal; };
  8.     int iGetChildrenNumber() { return(v_children.size()); };
  9.     void vAddNewChild();
  10.     void vAddNewChild(CNodeDynamic* node);
  11.     CNodeDynamic* pcGetChild(int iChildOffSet);
  12.     CNodeDynamic* pcGetParent() { return pc_parent_node;  }
  13.     int indexOfChild(CNodeDynamic* node);
  14.     bool childDelete(int iIndex);
  15.     void vPrint() { cout << " " << i_val; };
  16.     void vPrintParent() { cout << " " << pcGetParent()->i_val << endl; }
  17.     void vPrintAllBelow();
  18.     void vPrintUp();
  19.     CNodeDynamic* pcGoToRoot();
  20.     int iHowMany(int iVal);
  21.     int iHowDeepNode();
  22.     vector<int> vecBFS();
  23.     void vBFSPrint();
  24.     vector<int>  vecSortTree(vector<int> vecToSort);
  25.     vector<int> vecSortTree(vector<int> vecToSort, int n);
  26. private:
  27.     vector<CNodeDynamic*> v_children;
  28.     CNodeDynamic* pc_parent_node;
  29.     int iHowDeepNodeHelper(CNodeDynamic* node, int iCounterDeep);
  30.     void swap(int* xp, int* yp);
  31.     int i_level;
  32.     int i_val;
  33. };//class CNodeDynamic
  34.  
  35.  
  36. class CTreeDynamic
  37. {
  38. public:
  39.     CTreeDynamic() { pc_root = new CNodeDynamic;  };
  40.     CTreeDynamic(const CTreeDynamic* pcOther);
  41.     ~CTreeDynamic();
  42.     CNodeDynamic* pcGetRoot() { return(pc_root); }
  43.     void vPrintTree();
  44.     bool bMoveSubtree(CNodeDynamic* pcParentNode, CNodeDynamic* pcNewChildNode);
  45.  
  46. private:
  47.     CNodeDynamic* pc_root;
  48.    
  49. };//class CTreeDynamieDynami
  50.  
  51.  
  52.  
  53. //CNodeDynamic (down)
  54.  
  55. CNodeDynamic::CNodeDynamic(const CNodeDynamic* pcOther)
  56. {
  57.     if(DEBUG)   cout << COPY_PRINT << pcOther->i_val << endl;
  58.     if(pcOther->pc_parent_node!= NULL && DEBUG)  cout << COPY_PRINT << pcOther->pc_parent_node << endl;
  59.    
  60.     vector<CNodeDynamic*> v_children;
  61.     pc_parent_node = new CNodeDynamic;
  62.     i_level = pcOther->i_level;
  63.  
  64.     for (int ii = 0; ii < pcOther->v_children.size(); ii++)
  65.     {
  66.         if(DEBUG)  cout << COPY_PRINT << pcOther->v_children[ii]->i_val << endl;
  67.         v_children.push_back(pcOther->v_children[ii]);
  68.     }
  69.  
  70.     i_val = pcOther->i_val;
  71.     pc_parent_node = pcOther->pc_parent_node;
  72. }//copy constructor
  73.  
  74. CNodeDynamic :: ~CNodeDynamic()
  75. {
  76.     for (int ii = 0; ii < v_children.size(); ii++)
  77.     {
  78.         if(DEBUG) cout << DELETE << (*v_children[ii]).i_val << endl;    //debug mesage
  79.         delete v_children[ii];    //free up memory
  80.     }
  81. }//destructor
  82.  
  83. bool CNodeDynamic :: childDelete(int iIndex)
  84. {
  85.     if (iIndex > LESS_THAN_ZERO_INDEX && iIndex < v_children.size())
  86.     {
  87.         v_children.erase(v_children.begin() + iIndex);
  88.         return true;
  89.     }
  90.  
  91.     cout << CANT_DELETE << endl;
  92.     return false;
  93. }//delete child
  94.  
  95. void CNodeDynamic::vAddNewChild()
  96. {
  97.  
  98.     CNodeDynamic* new_child = new CNodeDynamic;     //dynamic alocate new node
  99.     new_child->pc_parent_node = this;  //if user wanna add new child, this will be parent
  100.     new_child->i_level = this->i_level + 1;
  101.    
  102.     v_children.push_back(new_child);       //add new child
  103. }//add new child
  104.  
  105. CNodeDynamic* CNodeDynamic::pcGetChild(int iChildOffSet)
  106. {
  107.     //check index, maybe its < 0 or more than node have children
  108.     if (iChildOffSet < v_children.size() && iChildOffSet >= LESS_THAN_ZERO_INDEX)
  109.         return v_children[iChildOffSet];
  110.     else
  111.         return NULL;
  112. }// return pointer to child from index
  113.  
  114. void CNodeDynamic::vPrintAllBelow()
  115. {
  116.     vPrint();
  117.    
  118.     //print all children and recursion go down
  119.     for (int ii = 0; ii < v_children.size(); ii++)
  120.         (*v_children[ii]).vPrintAllBelow();
  121. }//print all tree (down from node)
  122.  
  123. void CNodeDynamic::vPrintUp()
  124. {
  125.     vPrint();
  126.     cout << " -> ";
  127.  
  128.     if (pc_parent_node != NULL)
  129.         this->pcGetParent()->vPrintUp();  //go to parent and print him
  130.  
  131. }//print to up from here
  132.  
  133. CNodeDynamic* CNodeDynamic::pcGoToRoot()
  134. {
  135.     if(this != NULL)
  136.     if (pc_parent_node != NULL)
  137.         pc_parent_node->pcGoToRoot();
  138.     else
  139.         return this;
  140. }
  141.  
  142. int CNodeDynamic::iHowMany(int iVal)
  143. {
  144.     int iCounter = 0;  //how many iVal
  145.  
  146.     if (v_children.size() > 0)  //if the node has no children and we go to children the app will do BOOM
  147.     {
  148.         for (int ii = 0; ii < v_children.size(); ii++)
  149.             iCounter += v_children[ii]->iHowMany(iVal);  //recursion to children
  150.  
  151.  
  152.         if (i_val == iVal)   //one more node
  153.             return 1 + iCounter;
  154.         else   //dont change counter
  155.             return iCounter;
  156.     }
  157.    
  158.     if (this->i_val == iVal)  //if the node dont have a children yet check only node
  159.         return 1 + iCounter;
  160.     else
  161.         return iCounter;
  162. }
  163.  
  164. int CNodeDynamic::iHowDeepNode()
  165. {
  166.     int iCounter = 0;
  167.     iCounter += iHowDeepNodeHelper(this, 0);
  168.  
  169.     return iCounter;
  170. }
  171.  
  172. int CNodeDynamic ::  iHowDeepNodeHelper(CNodeDynamic* node, int iCounterDeep)
  173. {
  174.     if (node->pc_parent_node != NULL)
  175.         iHowDeepNodeHelper(node->pc_parent_node, ++iCounterDeep);
  176.     else
  177.         return iCounterDeep+1; //add 1, because if the parent node is null the recursion was stopped, but alg dont add number from root
  178. }
  179.  
  180. vector<int> CNodeDynamic::vecBFS()
  181. {
  182.     queue<CNodeDynamic*> qToVisit;
  183.     vector<int> vAllNode;
  184.  
  185.     if(this != NULL)
  186.     qToVisit.push(this);
  187.    
  188.     while (!qToVisit.empty())
  189.     {
  190.         CNodeDynamic* tmp = qToVisit.front();
  191.         qToVisit.pop();
  192.         vAllNode.push_back(tmp-> i_val);
  193.  
  194.         for (int ii = 0; ii < tmp->v_children.size(); ii++)
  195.             qToVisit.push(tmp->pcGetChild(ii));
  196.        
  197.     }
  198.  
  199.     return vAllNode;
  200. }//return tree according to bfs
  201.  
  202.  
  203. void CNodeDynamic :: swap(int* xp, int* yp)
  204. {
  205.     int temp = *xp;
  206.     *xp = *yp;
  207.     *yp = temp;
  208. }//bubble sort helper
  209.  
  210. vector<int> CNodeDynamic::vecSortTree(vector<int> vecToSort, int iN)
  211. {
  212.     int iI, iJ;
  213.    
  214.     for (iI = 0; iI < iN - 1; iI++)                         //bubble sort
  215.         for (iJ = 0; iJ < iN - iI - 1; iJ++)
  216.             if (vecToSort[iJ] > vecToSort[iJ + 1])
  217.                 swap(&vecToSort[iJ], &vecToSort[iJ + 1]);
  218.  
  219.     return vecToSort;
  220. }
  221.  
  222.  
  223. void CNodeDynamic::vBFSPrint()
  224. {
  225.     queue<CNodeDynamic*> qToVisit;  //BFS uses queue
  226.     int iLastLevel = 0;     //helper var to print
  227.  
  228.     if(this != NULL)     //error control
  229.     qToVisit.push(this);   //first elem to visit
  230.  
  231.     while (!qToVisit.empty())
  232.     {
  233.         CNodeDynamic* tmp = qToVisit.front();   //tmp (first from queue)
  234.         qToVisit.pop();
  235.  
  236.         if (iLastLevel < tmp->i_level)    //print helper
  237.         {
  238.             cout << endl;
  239.             iLastLevel++;
  240.         }
  241.  
  242.         tmp->vPrint();  
  243.  
  244.         for (int ii = 0; ii < tmp -> v_children.size(); ii++)   //add children to visit
  245.             qToVisit.push(tmp -> pcGetChild(ii));
  246.     } // repeat
  247.  
  248. }//print tree with use BFS
  249.  
  250.  
  251. //CTreeDynamic (down)
  252.  
  253.  
  254. CTreeDynamic::CTreeDynamic(const CTreeDynamic* pcOther)
  255. {
  256.     if (DEBUG)   cout << COPY_PRINT << pcOther->pc_root << endl;
  257.  
  258.     pc_root = new CNodeDynamic();
  259.     pc_root = pcOther->pc_root;
  260. }//tree copy constructor
  261.  
  262. CTreeDynamic :: ~CTreeDynamic()
  263. {
  264.     //debug messages
  265.     if(DEBUG)   cout << DELETE;
  266.     if(DEBUG)   (*pc_root).vPrint();
  267.     if(DEBUG)   cout << endl;
  268.  
  269.     delete pc_root;     //free up memory
  270. }//destructor
  271.  
  272. void CTreeDynamic::vPrintTree()
  273. {
  274.     (*pc_root).vPrintAllBelow();    //print (use Node's method)
  275.  
  276.     cout << endl;
  277. }//print all tree
  278.  
  279. void CNodeDynamic::vAddNewChild(CNodeDynamic* node)
  280. {
  281.  
  282.     (*node).pc_parent_node = this; //new child, but parent is this
  283.     node->i_level = this->i_level + 1;
  284.     v_children.push_back(node);        // add new child
  285. }//add new child, but this method is overloaded
  286.  
  287.  
  288. int CNodeDynamic::indexOfChild(CNodeDynamic* node)
  289. {
  290.     //check all children and search the same
  291.     for (int ii = 0; ii < node->pcGetParent()->v_children.size(); ii++)
  292.         if (node->pcGetParent()->pcGetChild(ii) == node)
  293.             return ii;
  294.  
  295.     //if parent dont have this child
  296.     return UNBOUND_VALUE;
  297. }//go to parent and tell me index of this children (pointer points to the same area)
  298.  
  299. bool CTreeDynamic::bMoveSubtree(CNodeDynamic* pcParentNode, CNodeDynamic* pcNewChildNode)
  300. {
  301.     if (pcParentNode != NULL && pcNewChildNode != NULL)
  302.     {
  303.         //go to parent, first check index of flip child and tell to father, that he don't have a this child now
  304.         pcNewChildNode->pcGetParent()->childDelete(pcNewChildNode->pcGetParent()->indexOfChild(pcNewChildNode));
  305.         pcParentNode->vAddNewChild(pcNewChildNode);
  306.  
  307.          return true; //if success return true
  308.     }
  309.  
  310.     return false; //if fail return false
  311.  
  312. }//flip the node to another tree
  313.  
  314.  
Advertisement
Add Comment
Please, Sign In to add comment