Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class CNodeDynamic
- {
- public:
- CNodeDynamic() { i_val = 0; pc_parent_node = NULL; i_level = 0; };
- CNodeDynamic(const CNodeDynamic* pcOther);
- ~CNodeDynamic();
- void vSetValue(int iNewVal) { i_val = iNewVal; };
- int iGetChildrenNumber() { return(v_children.size()); };
- void vAddNewChild();
- void vAddNewChild(CNodeDynamic* node);
- CNodeDynamic* pcGetChild(int iChildOffSet);
- CNodeDynamic* pcGetParent() { return pc_parent_node; }
- int indexOfChild(CNodeDynamic* node);
- bool childDelete(int iIndex);
- void vPrint() { cout << " " << i_val; };
- void vPrintParent() { cout << " " << pcGetParent()->i_val << endl; }
- void vPrintAllBelow();
- void vPrintUp();
- CNodeDynamic* pcGoToRoot();
- int iHowMany(int iVal);
- int iHowDeepNode();
- vector<int> vecBFS();
- void vBFSPrint();
- vector<int> vecSortTree(vector<int> vecToSort);
- vector<int> vecSortTree(vector<int> vecToSort, int n);
- private:
- vector<CNodeDynamic*> v_children;
- CNodeDynamic* pc_parent_node;
- int iHowDeepNodeHelper(CNodeDynamic* node, int iCounterDeep);
- void swap(int* xp, int* yp);
- int i_level;
- int i_val;
- };//class CNodeDynamic
- class CTreeDynamic
- {
- public:
- CTreeDynamic() { pc_root = new CNodeDynamic; };
- CTreeDynamic(const CTreeDynamic* pcOther);
- ~CTreeDynamic();
- CNodeDynamic* pcGetRoot() { return(pc_root); }
- void vPrintTree();
- bool bMoveSubtree(CNodeDynamic* pcParentNode, CNodeDynamic* pcNewChildNode);
- private:
- CNodeDynamic* pc_root;
- };//class CTreeDynamieDynami
- //CNodeDynamic (down)
- CNodeDynamic::CNodeDynamic(const CNodeDynamic* pcOther)
- {
- if(DEBUG) cout << COPY_PRINT << pcOther->i_val << endl;
- if(pcOther->pc_parent_node!= NULL && DEBUG) cout << COPY_PRINT << pcOther->pc_parent_node << endl;
- vector<CNodeDynamic*> v_children;
- pc_parent_node = new CNodeDynamic;
- i_level = pcOther->i_level;
- for (int ii = 0; ii < pcOther->v_children.size(); ii++)
- {
- if(DEBUG) cout << COPY_PRINT << pcOther->v_children[ii]->i_val << endl;
- v_children.push_back(pcOther->v_children[ii]);
- }
- i_val = pcOther->i_val;
- pc_parent_node = pcOther->pc_parent_node;
- }//copy constructor
- CNodeDynamic :: ~CNodeDynamic()
- {
- for (int ii = 0; ii < v_children.size(); ii++)
- {
- if(DEBUG) cout << DELETE << (*v_children[ii]).i_val << endl; //debug mesage
- delete v_children[ii]; //free up memory
- }
- }//destructor
- bool CNodeDynamic :: childDelete(int iIndex)
- {
- if (iIndex > LESS_THAN_ZERO_INDEX && iIndex < v_children.size())
- {
- v_children.erase(v_children.begin() + iIndex);
- return true;
- }
- cout << CANT_DELETE << endl;
- return false;
- }//delete child
- void CNodeDynamic::vAddNewChild()
- {
- CNodeDynamic* new_child = new CNodeDynamic; //dynamic alocate new node
- new_child->pc_parent_node = this; //if user wanna add new child, this will be parent
- new_child->i_level = this->i_level + 1;
- v_children.push_back(new_child); //add new child
- }//add new child
- CNodeDynamic* CNodeDynamic::pcGetChild(int iChildOffSet)
- {
- //check index, maybe its < 0 or more than node have children
- if (iChildOffSet < v_children.size() && iChildOffSet >= LESS_THAN_ZERO_INDEX)
- return v_children[iChildOffSet];
- else
- return NULL;
- }// return pointer to child from index
- void CNodeDynamic::vPrintAllBelow()
- {
- vPrint();
- //print all children and recursion go down
- for (int ii = 0; ii < v_children.size(); ii++)
- (*v_children[ii]).vPrintAllBelow();
- }//print all tree (down from node)
- void CNodeDynamic::vPrintUp()
- {
- vPrint();
- cout << " -> ";
- if (pc_parent_node != NULL)
- this->pcGetParent()->vPrintUp(); //go to parent and print him
- }//print to up from here
- CNodeDynamic* CNodeDynamic::pcGoToRoot()
- {
- if(this != NULL)
- if (pc_parent_node != NULL)
- pc_parent_node->pcGoToRoot();
- else
- return this;
- }
- int CNodeDynamic::iHowMany(int iVal)
- {
- int iCounter = 0; //how many iVal
- if (v_children.size() > 0) //if the node has no children and we go to children the app will do BOOM
- {
- for (int ii = 0; ii < v_children.size(); ii++)
- iCounter += v_children[ii]->iHowMany(iVal); //recursion to children
- if (i_val == iVal) //one more node
- return 1 + iCounter;
- else //dont change counter
- return iCounter;
- }
- if (this->i_val == iVal) //if the node dont have a children yet check only node
- return 1 + iCounter;
- else
- return iCounter;
- }
- int CNodeDynamic::iHowDeepNode()
- {
- int iCounter = 0;
- iCounter += iHowDeepNodeHelper(this, 0);
- return iCounter;
- }
- int CNodeDynamic :: iHowDeepNodeHelper(CNodeDynamic* node, int iCounterDeep)
- {
- if (node->pc_parent_node != NULL)
- iHowDeepNodeHelper(node->pc_parent_node, ++iCounterDeep);
- else
- return iCounterDeep+1; //add 1, because if the parent node is null the recursion was stopped, but alg dont add number from root
- }
- vector<int> CNodeDynamic::vecBFS()
- {
- queue<CNodeDynamic*> qToVisit;
- vector<int> vAllNode;
- if(this != NULL)
- qToVisit.push(this);
- while (!qToVisit.empty())
- {
- CNodeDynamic* tmp = qToVisit.front();
- qToVisit.pop();
- vAllNode.push_back(tmp-> i_val);
- for (int ii = 0; ii < tmp->v_children.size(); ii++)
- qToVisit.push(tmp->pcGetChild(ii));
- }
- return vAllNode;
- }//return tree according to bfs
- void CNodeDynamic :: swap(int* xp, int* yp)
- {
- int temp = *xp;
- *xp = *yp;
- *yp = temp;
- }//bubble sort helper
- vector<int> CNodeDynamic::vecSortTree(vector<int> vecToSort, int iN)
- {
- int iI, iJ;
- for (iI = 0; iI < iN - 1; iI++) //bubble sort
- for (iJ = 0; iJ < iN - iI - 1; iJ++)
- if (vecToSort[iJ] > vecToSort[iJ + 1])
- swap(&vecToSort[iJ], &vecToSort[iJ + 1]);
- return vecToSort;
- }
- void CNodeDynamic::vBFSPrint()
- {
- queue<CNodeDynamic*> qToVisit; //BFS uses queue
- int iLastLevel = 0; //helper var to print
- if(this != NULL) //error control
- qToVisit.push(this); //first elem to visit
- while (!qToVisit.empty())
- {
- CNodeDynamic* tmp = qToVisit.front(); //tmp (first from queue)
- qToVisit.pop();
- if (iLastLevel < tmp->i_level) //print helper
- {
- cout << endl;
- iLastLevel++;
- }
- tmp->vPrint();
- for (int ii = 0; ii < tmp -> v_children.size(); ii++) //add children to visit
- qToVisit.push(tmp -> pcGetChild(ii));
- } // repeat
- }//print tree with use BFS
- //CTreeDynamic (down)
- CTreeDynamic::CTreeDynamic(const CTreeDynamic* pcOther)
- {
- if (DEBUG) cout << COPY_PRINT << pcOther->pc_root << endl;
- pc_root = new CNodeDynamic();
- pc_root = pcOther->pc_root;
- }//tree copy constructor
- CTreeDynamic :: ~CTreeDynamic()
- {
- //debug messages
- if(DEBUG) cout << DELETE;
- if(DEBUG) (*pc_root).vPrint();
- if(DEBUG) cout << endl;
- delete pc_root; //free up memory
- }//destructor
- void CTreeDynamic::vPrintTree()
- {
- (*pc_root).vPrintAllBelow(); //print (use Node's method)
- cout << endl;
- }//print all tree
- void CNodeDynamic::vAddNewChild(CNodeDynamic* node)
- {
- (*node).pc_parent_node = this; //new child, but parent is this
- node->i_level = this->i_level + 1;
- v_children.push_back(node); // add new child
- }//add new child, but this method is overloaded
- int CNodeDynamic::indexOfChild(CNodeDynamic* node)
- {
- //check all children and search the same
- for (int ii = 0; ii < node->pcGetParent()->v_children.size(); ii++)
- if (node->pcGetParent()->pcGetChild(ii) == node)
- return ii;
- //if parent dont have this child
- return UNBOUND_VALUE;
- }//go to parent and tell me index of this children (pointer points to the same area)
- bool CTreeDynamic::bMoveSubtree(CNodeDynamic* pcParentNode, CNodeDynamic* pcNewChildNode)
- {
- if (pcParentNode != NULL && pcNewChildNode != NULL)
- {
- //go to parent, first check index of flip child and tell to father, that he don't have a this child now
- pcNewChildNode->pcGetParent()->childDelete(pcNewChildNode->pcGetParent()->indexOfChild(pcNewChildNode));
- pcParentNode->vAddNewChild(pcNewChildNode);
- return true; //if success return true
- }
- return false; //if fail return false
- }//flip the node to another tree
Advertisement
Add Comment
Please, Sign In to add comment