Advertisement
Guest User

Untitled

a guest
Nov 16th, 2019
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.34 KB | None | 0 0
  1.  
  2. #ifndef UNTITLED_CTREESTATIC_H
  3. #define UNTITLED_CTREESTATIC_H
  4.  
  5. #include <iostream>
  6. #include <vector>
  7. #include <algorithm>
  8. class CNodeStatic {
  9. public:
  10.     CNodeStatic();
  11.     CNodeStatic(const CNodeStatic &pcOther);
  12.     explicit CNodeStatic(CNodeStatic *pcParent);
  13.  
  14.     CNodeStatic & operator=(CNodeStatic const &pcOther);
  15.     bool operator==(CNodeStatic const &pcOther);
  16.     bool operator !=(CNodeStatic const &pcOther);
  17.  
  18.     void vSetValue (int iNewVal) {i_val = iNewVal;}
  19.     int iGetChildrenNumber() {return v_children.size();}
  20.     void vAddNewChild();
  21.     CNodeStatic *pcGetChild(int iChildOffset);
  22.     void vPrint() {std::cout << " " << i_val;}
  23.     void vPrintAllBelow();
  24.     void vPrintUp();
  25.     void vPrintUpAddresses();
  26.     void vPrintAddressesAllBelow();
  27.     bool bHasParent() {return pc_parent != NULL;}
  28.     bool bUnpin();
  29.     void vSetMyChildrenParents();
  30.     void vSetParentsBelow();
  31.     CNodeStatic *pc_parent;
  32. private:
  33.     std::vector<CNodeStatic> v_children;
  34.     int i_val;
  35. };
  36.  
  37. class CTreeStatic {
  38. public:
  39.     CNodeStatic *pcGetRoot() {return &c_root;}
  40.     void vPrintTree();
  41.     bool bMoveSubtree(CNodeStatic *pcParentNode, CNodeStatic *pcNewChildNode);
  42. private:
  43.     CNodeStatic c_root;
  44. };
  45.  
  46.  
  47. #endif //UNTITLED_CTREESTATIC_H
  48.  
  49.  
  50.  
  51. #include "CTreeStatic.h"
  52. //Constructors
  53. CNodeStatic::CNodeStatic() {
  54.     i_val = 0;
  55.     pc_parent = NULL;
  56. }
  57. CNodeStatic::CNodeStatic(const CNodeStatic &pcOther) {
  58.     pc_parent = pcOther.pc_parent;
  59.     v_children = pcOther.v_children;
  60.     i_val = pcOther.i_val;
  61.     vSetMyChildrenParents();
  62. }
  63. CNodeStatic::CNodeStatic(CNodeStatic *pcParent){
  64.     i_val = 0;
  65.     pc_parent = pcParent;
  66. }
  67. //Operators
  68. CNodeStatic& CNodeStatic::operator=(CNodeStatic const &pcOther) {
  69.     pc_parent = pcOther.pc_parent;
  70.     v_children = pcOther.v_children;
  71.     i_val = pcOther.i_val;
  72.     vSetMyChildrenParents();
  73. }
  74.  
  75. bool CNodeStatic::operator==(CNodeStatic const &pcOther) {
  76.     if (i_val != pcOther.i_val) {
  77.         return false;
  78.     }
  79.     if (v_children.size() != pcOther.v_children.size()){
  80.         return false;
  81.     }
  82.     for (int i = 0; i < v_children.size(); i++) {
  83.         if (v_children[i] != pcOther.v_children[i]) {
  84.             return false;
  85.         }
  86.     }
  87.     return true;
  88. }
  89. bool CNodeStatic::operator!=(CNodeStatic const &pcOther) {
  90.     return !operator==(pcOther);
  91. }
  92. void CNodeStatic::vAddNewChild() {
  93.     v_children.push_back(CNodeStatic(this));
  94. }
  95.  
  96. CNodeStatic* CNodeStatic::pcGetChild(int iChildOffset) {
  97.     return (iChildOffset>=0 and iChildOffset < v_children.size()) ? &v_children[iChildOffset] : NULL;
  98. }
  99.  
  100. void CNodeStatic::vPrintAllBelow() {
  101.     vPrint();
  102.     for (int i = 0; i < v_children.size(); i++) {
  103.         v_children[i].vPrintAllBelow();
  104.     }
  105. }
  106. void CNodeStatic::vPrintUp() {
  107.     vPrint();
  108.     if (pc_parent != NULL) {
  109.         pc_parent->vPrintUp();
  110.     }
  111. }
  112. void CNodeStatic::vPrintUpAddresses() {
  113.     std::cout << this << " ";
  114.     if (pc_parent != NULL){
  115.         pc_parent->vPrintUpAddresses();
  116.     }
  117. }
  118. void CNodeStatic::vPrintAddressesAllBelow() {
  119.     std::cout << this << " ";
  120.     for (int i = 0; i < v_children.size(); i++){
  121.         v_children[i].vPrintAddressesAllBelow();
  122.     }
  123. }
  124. void CNodeStatic::vSetMyChildrenParents() {
  125.     for (int i = 0; i < v_children.size(); i++) {
  126.         v_children[i].pc_parent = this;
  127.     }
  128. }
  129. void CNodeStatic::vSetParentsBelow() {
  130.     vSetMyChildrenParents();
  131.     for (int i = 0; i < v_children.size(); i++){
  132.         v_children[i].vSetParentsBelow();
  133.     }
  134. }
  135. bool CNodeStatic::bUnpin() {
  136.     if (pc_parent == NULL){
  137.         return false;
  138.     }
  139.     std::vector <CNodeStatic> * pv_siblings = &(pc_parent->v_children);
  140.     CNodeStatic* pc_temp_parent = pc_parent;
  141.     pv_siblings->erase(std::remove(pv_siblings->begin(),pv_siblings->end(),*this),pv_siblings->end());
  142.     pc_temp_parent->vSetMyChildrenParents();
  143.     return true;
  144. }
  145. bool CTreeStatic::bMoveSubtree(CNodeStatic *pcParentNode, CNodeStatic *pcNewChildNode) {
  146.     if (!pcNewChildNode->bHasParent()) {
  147.         return false;
  148.     }
  149.     pcParentNode->vAddNewChild();
  150.     CNodeStatic *pc_place = pcParentNode->pcGetChild(pcParentNode->iGetChildrenNumber()-1);
  151.     *pc_place = *pcNewChildNode;
  152.     pcNewChildNode->bUnpin();
  153.     pcParentNode->vSetMyChildrenParents();
  154.     return true;
  155. }
  156. void CTreeStatic::vPrintTree() {
  157.     c_root.vPrintAllBelow();
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement