Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // EPT5_2017.cpp : This file contains the 'main' function. Program execution begins and ends there.
- //
- // EPT5_CM.cpp : This file contains the 'main' function. Program execution begins and ends there.
- //
- //#include "pch.h"
- #include <iostream>
- #include <vector>
- #include <string>
- using namespace std;
- class CNodeStatic {
- public:
- CNodeStatic() { i_val = 0; parent = NULL; };
- ~CNodeStatic();
- void vSetValue(int iNewVal) { cout << "changing node id: " << i_val<< " to "<< iNewVal<<endl; i_val = iNewVal; };
- int iGetChildrenNumber() { return(v_children.size()); };
- void vAddNewChild();
- CNodeStatic* pcGetChild(int iChildOffset);
- CNodeStatic* getParent() { return parent; };
- void vPrint() { cout << " " << i_val; };
- void vPrintAllBelow();
- void vPrintUp();
- void vAddNewChild(CNodeStatic* newChild);
- void removeChild(CNodeStatic* oldChild);
- private:
- vector<CNodeStatic> v_children;
- int i_val;
- CNodeStatic* parent;
- //void setParent();
- };//class CNodeStatic
- CNodeStatic::~CNodeStatic()
- {
- }
- void CNodeStatic::vAddNewChild()
- {
- CNodeStatic toAdd = CNodeStatic();
- toAdd.parent = this;
- v_children.push_back(toAdd);
- cout << "Node Added to node " << i_val << endl;
- }
- CNodeStatic* CNodeStatic::pcGetChild(int iChildOffset)
- {
- if (iChildOffset < 0 || iChildOffset >= v_children.size()) {
- return NULL;
- }
- return &v_children[iChildOffset];
- }
- void CNodeStatic::vPrintAllBelow() {
- vPrint();
- for (int i = 0; i < v_children.size(); i++) {
- this->pcGetChild(i)->vPrintAllBelow();
- }
- }
- void CNodeStatic::vPrintUp()
- {
- //vPrint();
- if (parent != NULL) {
- parent->vPrint();
- parent->vPrintUp();
- }
- }
- //6
- void CNodeStatic::vAddNewChild(CNodeStatic* newChild) {
- newChild->parent=this;
- v_children.push_back(*newChild);
- }
- void CNodeStatic::removeChild(CNodeStatic* oldChild) {
- for (int i = 0; i < iGetChildrenNumber(); i++) {
- if (v_children[i].i_val == oldChild->i_val)
- v_children.erase(v_children.begin() + 1);
- }
- }
- /////////////////////////////////////////////////////////////////////////////////////////
- class CTreeStatic {
- public:
- CTreeStatic();
- ~CTreeStatic() {};
- CNodeStatic* pcGetRoot() { return(&c_root); }
- void vPrintTree();
- bool bMoveSubtree(CNodeStatic* pcParentNode, CNodeStatic* pcNewChildNode, CNodeStatic* pc2ParentNode);
- private:
- CNodeStatic c_root;
- };//class CTreeStatic
- CTreeStatic::CTreeStatic() {
- c_root = CNodeStatic();
- }
- void CTreeStatic::vPrintTree() {
- c_root.vPrintAllBelow();
- }
- bool CTreeStatic::bMoveSubtree(CNodeStatic* pcParentNode, CNodeStatic* pcNewChildNode, CNodeStatic* pc2ParentNode) {
- pcParentNode->vAddNewChild(pcNewChildNode);
- pc2ParentNode->removeChild(pcNewChildNode);
- return true;
- }
- ////////////////////////////////////////////////////////////////////////////////////
- //DYNAMIC CLASSES//
- class CNodeDynamic
- {
- public:
- CNodeDynamic() { i_val = 0; parent = NULL;};
- ~CNodeDynamic();
- void vSetValue(int iNewVal) { cout << "changing node id: " << i_val << " to " << iNewVal << endl; i_val = iNewVal; };
- int iGetChildrenNumber() { return v_children.size(); };
- void vAddNewChild(); //
- CNodeDynamic* pcGetChild(int iChildOffset); //
- void vPrint() { cout << " " << i_val; };
- void vPrintAllBelow(); //
- //void vPrintUp();
- void vAddNewChild(CNodeDynamic* newChild);
- void removeChild(CNodeDynamic* oldChild);
- private:
- vector<CNodeDynamic*> v_children;
- int i_val;
- CNodeDynamic* parent;
- };
- void CNodeDynamic::vAddNewChild(CNodeDynamic* newChild) {
- v_children.push_back(newChild);
- }
- void CNodeDynamic::removeChild(CNodeDynamic* oldChild) {
- for (int i = 0; i < iGetChildrenNumber(); i++) {
- if ((*v_children[i]).i_val == oldChild->i_val)
- v_children.erase(v_children.begin() + 1);
- }
- }
- //CNodeDynamic::~CNodeDynamic()
- //{
- // if (!v_children.empty()) {
- // for (int i = 0; i < v_children.size(); i++) {
- // delete this->pcGetChild(i);
- // }
- // }
- // delete[] this;
- //}
- CNodeDynamic::~CNodeDynamic() {
- //if (iGetChildrenNumber() == 0) {
- //delete[] this;
- //}
- // else {
- //}
- if (iGetChildrenNumber != 0) {
- for (int i = 0; i < iGetChildrenNumber(); i++) {
- v_children[i]->~CNodeDynamic();
- }
- }
- else {
- delete[] this;
- }
- }
- void CNodeDynamic::vAddNewChild()
- {
- CNodeDynamic * toAdd = &CNodeDynamic();
- toAdd->parent = this;
- v_children.push_back(toAdd);
- cout << "Node Added to node " << i_val << endl;
- }
- CNodeDynamic* CNodeDynamic::pcGetChild(int iChildOffset)
- {
- if (iChildOffset < 0 || iChildOffset >= v_children.size()) {
- return NULL;
- }
- return v_children[iChildOffset];
- }
- void CNodeDynamic::vPrintAllBelow() {
- vPrint();
- for (int i = 0; i < v_children.size(); i++) {
- this->pcGetChild(i)->vPrintAllBelow();
- }
- }
- /**void CNodeDynamic::vPrintUp()
- {
- //vPrint();
- if (parent != NULL) {
- parent->vPrint();
- parent->vPrintUp();
- }
- }*/
- ///////////////////////////////////////////////////////
- class CTreeDynamic
- {
- public:
- CTreeDynamic();
- ~CTreeDynamic() { delete c_root; };
- CNodeDynamic* pcGetRoot() { return c_root; }
- void vPrintTree();
- bool bMoveSubtree(CNodeDynamic* pcParentNode, CNodeDynamic* pcNewChildNode, CNodeDynamic* pc2ParentNode);
- private:
- CNodeDynamic* c_root;
- };
- CTreeDynamic::CTreeDynamic() {
- CNodeDynamic root = CNodeDynamic();
- c_root = &root;
- }
- void CTreeDynamic::vPrintTree() {
- c_root->vPrintAllBelow();
- }
- //6
- bool CTreeDynamic::bMoveSubtree(CNodeDynamic* pcParentNode, CNodeDynamic* pcNewChildNode, CNodeDynamic* pc2ParentNode) {
- pcParentNode->vAddNewChild(pcNewChildNode);
- pc2ParentNode->removeChild(pcNewChildNode);
- return true;
- }
- //////////////////////////////////////////////////
- void v_tree_test() {
- CNodeStatic c_root;
- c_root.vAddNewChild(); //root:0
- c_root.vAddNewChild(); // 1 2
- c_root.pcGetChild(0)->vSetValue(1); // 11 12 21 22
- c_root.pcGetChild(1)->vSetValue(2);
- c_root.pcGetChild(0)->vAddNewChild();
- c_root.pcGetChild(0)->vAddNewChild();
- c_root.pcGetChild(0)->pcGetChild(0)->vSetValue(11);
- c_root.pcGetChild(0)->pcGetChild(1)->vSetValue(12);
- c_root.pcGetChild(1)->vAddNewChild();
- c_root.pcGetChild(1)->vAddNewChild();
- c_root.pcGetChild(1)->pcGetChild(0)->vSetValue(21);
- c_root.pcGetChild(1)->pcGetChild(1)->vSetValue(22);
- c_root.vPrintAllBelow();
- cout << endl << "vPrint up";
- c_root.pcGetChild(1)->pcGetChild(0)->vPrintUp();
- }//void v_tree_test()
- void testMovingStaticTree() {
- cout << "\n\n~~~~Test Move Static Tree~~~~";
- cout << "\n ~Allocating tree 1\n";
- CTreeStatic c_tree;
- CNodeStatic c_root = *c_tree.pcGetRoot();
- c_root.vAddNewChild();
- c_root.vAddNewChild();
- c_root.pcGetChild(0)->vSetValue(1);
- c_root.pcGetChild(1)->vSetValue(2);
- c_root.pcGetChild(0)->vAddNewChild();
- c_root.pcGetChild(0)->vAddNewChild();
- c_root.pcGetChild(0)->pcGetChild(0)->vSetValue(11);
- c_root.pcGetChild(0)->pcGetChild(1)->vSetValue(12);
- c_root.pcGetChild(1)->vAddNewChild();
- c_root.pcGetChild(1)->vAddNewChild();
- c_root.pcGetChild(1)->pcGetChild(0)->vSetValue(21);
- c_root.pcGetChild(1)->pcGetChild(1)->vSetValue(22);
- cout << "Print Below: ";
- c_root.vPrintAllBelow();
- cout << "\nPrint Up: ";
- c_root.pcGetChild(0)->pcGetChild(1)->vPrintUp();
- cout << "\n\n ~Allocating tree 3\n";
- CTreeStatic c_tree3;
- CNodeStatic c_root3 = *c_tree3.pcGetRoot();
- c_root3.vAddNewChild();
- c_root3.vAddNewChild();
- c_root3.pcGetChild(0)->vSetValue(5);
- c_root3.pcGetChild(1)->vSetValue(6);
- c_root3.pcGetChild(0)->vAddNewChild();
- c_root3.pcGetChild(0)->vAddNewChild();
- c_root3.pcGetChild(0)->pcGetChild(0)->vSetValue(51);
- c_root3.pcGetChild(0)->pcGetChild(1)->vSetValue(52);
- c_root3.pcGetChild(1)->vAddNewChild();
- c_root3.pcGetChild(1)->vAddNewChild();
- c_root3.pcGetChild(1)->pcGetChild(0)->vSetValue(61);
- c_root3.pcGetChild(1)->pcGetChild(1)->vSetValue(62);
- cout << "Print Below";
- c_root3.vPrintAllBelow();
- cout << "\n\n ~Moving tree 3 to tree 1 \n";
- c_tree.bMoveSubtree(&c_root, c_root3.pcGetChild(0), &c_root3);
- cout << "\nPrint Below TREE1 after moving subtree: ";
- c_root.vPrintAllBelow();
- cout << "\nPrint Below TREE3 after moving subtree:";
- c_root3.vPrintAllBelow();
- cout << "\n";
- }
- void testMovingDynamicTree() {
- cout << "\n\n~~~~Test Move Dynamic Tree~~~~";
- cout << "\n ~Allocating tree 1\n";
- CTreeDynamic c_tree;
- CNodeDynamic c_root = *c_tree.pcGetRoot();
- c_root.vAddNewChild();
- c_root.vAddNewChild();
- c_root.pcGetChild(0)->vSetValue(1);
- c_root.pcGetChild(1)->vSetValue(2);
- c_root.pcGetChild(0)->vAddNewChild();
- c_root.pcGetChild(0)->vAddNewChild();
- c_root.pcGetChild(0)->pcGetChild(0)->vSetValue(11);
- c_root.pcGetChild(0)->pcGetChild(1)->vSetValue(12);
- c_root.pcGetChild(1)->vAddNewChild();
- c_root.pcGetChild(1)->vAddNewChild();
- c_root.pcGetChild(1)->pcGetChild(0)->vSetValue(21);
- c_root.pcGetChild(1)->pcGetChild(1)->vSetValue(22);
- cout << "Print Below: ";
- c_root.vPrintAllBelow();
- cout << "\n\n ~Allocating tree 3\n";
- CTreeDynamic c_tree3;
- CNodeDynamic c_root3 = *c_tree3.pcGetRoot();
- c_root3.vAddNewChild();
- c_root3.vAddNewChild();
- c_root3.pcGetChild(0)->vSetValue(5);
- c_root3.pcGetChild(1)->vSetValue(6);
- c_root3.pcGetChild(0)->vAddNewChild();
- c_root3.pcGetChild(0)->vAddNewChild();
- c_root3.pcGetChild(0)->pcGetChild(0)->vSetValue(51);
- c_root3.pcGetChild(0)->pcGetChild(1)->vSetValue(52);
- c_root3.pcGetChild(1)->vAddNewChild();
- c_root3.pcGetChild(1)->vAddNewChild();
- c_root3.pcGetChild(1)->pcGetChild(0)->vSetValue(61);
- c_root3.pcGetChild(1)->pcGetChild(1)->vSetValue(62);
- cout << "Print Below";
- c_root3.vPrintAllBelow();
- cout << "\n\n ~Moving tree 3 to tree 1 \n";
- c_tree.bMoveSubtree(&c_root, c_root3.pcGetChild(0), &c_root3);
- cout << "\nPrint Below TREE1 after moving subtree: ";
- c_root.vPrintAllBelow();
- cout << "\nPrint Below TREE3 after moving subtree:";
- c_root3.vPrintAllBelow();
- cout << "\n";
- }
- void testPrintUp()
- {
- CNodeStatic c_root;
- c_root.vAddNewChild();
- c_root.vAddNewChild();
- c_root.pcGetChild(0)->vSetValue(1);
- c_root.pcGetChild(1)->vSetValue(2);
- c_root.pcGetChild(0)->vAddNewChild();
- c_root.pcGetChild(0)->vAddNewChild();
- c_root.pcGetChild(0)->pcGetChild(0)->vSetValue(11);
- c_root.pcGetChild(0)->pcGetChild(1)->vSetValue(12);
- c_root.pcGetChild(1)->vAddNewChild();
- c_root.pcGetChild(1)->vAddNewChild();
- c_root.pcGetChild(1)->pcGetChild(0)->vSetValue(21);
- c_root.pcGetChild(1)->pcGetChild(1)->vSetValue(22);
- cout << "\n\n~~~~Test Print up~~~~";
- cout << "\nPrint Down:\n";
- c_root.vPrintAllBelow();
- cout << "\nPrint Up:\n";
- c_root.pcGetChild(0)->pcGetChild(1)->vPrintUp();
- }
- int main()
- {
- v_tree_test();
- testMovingStaticTree();
- testMovingDynamicTree();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement