Facebook
From Colorant Tamarin, 6 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 278
  1. #include <iostream>
  2. #include<string>
  3. #include <vector>
  4.  
  5. using namespace std;
  6. class CNode{
  7. public:
  8.     CNode(int val){
  9.         pi_val=new int(val);
  10.     }
  11.     CNode(CNode &other){
  12.         pi_val=other.pi_val;
  13.         for(int i=0; i<other.v_kids.size(); i++){
  14.             v_kids.push_back(other.v_kids[i]);
  15.         }
  16.     }
  17.     ~CNode(){
  18.         delete pi_val;
  19.         for(int i=0; i<v_kids.size(); i++){
  20.             delete v_kids[i];
  21.         }
  22.     }
  23.     void vSetVal(int iVal){
  24.         *pi_val=iVal;
  25.     }
  26.     void vAdd(CNode *pcKid){
  27.         v_kids.push_back(pcKid);
  28.     }
  29.     void vPrint(){
  30.         cout<<*pi_val;
  31.         for(int i=0; i<v_kids.size(); i++){
  32.             v_kids[i]->vPrint();
  33.         }
  34.     }
  35.  
  36. private:
  37.     int *pi_val;
  38.     vector<CNode*> v_kids;
  39. };
  40.  
  41. class CTree{
  42. public:
  43.     CTree(){
  44.         pc_root=new CNode(0);
  45.     }
  46.     CTree(CTree &other){
  47.         ???
  48.     }
  49.     ~CTree(){
  50.         delete pc_root;
  51.     }
  52.     CNode *pcGetRt(){
  53.         return pc_root;
  54.     }
  55.     void vPrint(){
  56.         pc_root->vPrint();
  57.     }
  58. private:
  59.     CNode *pc_root;
  60. };
  61.  
  62. int main() {
  63.     CTree c_t0;
  64.     c_t0.pcGetRt()->vSetVal(1);
  65.     c_t0.pcGetRt()->vAdd(new CNode(2));
  66.  
  67.     CNode *pc_buf;
  68.     pc_buf = new CNode(3);
  69.     pc_buf->vAdd(new CNode(4));
  70.     c_t0.pcGetRt()->vAdd(pc_buf);
  71.  
  72.     CTree *pc_copy = new CTree(c_t0);
  73.     c_t0.vPrint();
  74.     //pc_copy->vPrint();
  75.     delete pc_copy;
  76. }