Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.74 KB | None | 0 0
  1. template <typename T>
  2. class Forest {
  3.     private:
  4.     T data;
  5.     Tree<T> *leftChild;
  6.     Tree<T> *rightSibling;
  7.     Tree<T> *rootNode;
  8.     //Compiler says I can't initialize int count here which doesn't make sense. Should I be initializing the variable outside of the class or something?
  9.     int count = 0;
  10.  
  11.     public:
  12.     Forest(){
  13.         rootNode = 0;
  14.         leftChild = 0;
  15.         rightSibling = 0;
  16.     }
  17.  
  18.     Forest(const Forest& otherForest){
  19.         data = otherForest.data;
  20.         if(*rootNode == 0){
  21.             rootNode = data;
  22.         }
  23.         if(*otherForest.leftChild == 0) {
  24.             leftChild = new Forest(*otherForest.leftChild);
  25.         } else {
  26.             leftChild = 0;
  27.         }
  28.         if(otherForest.rightSibling == 0) {
  29.             rightSibling = new Forest(*otherForest.rightSibling);
  30.         } else {
  31.             right = 0;
  32.         }
  33.  
  34.     }
  35.  
  36.  
  37.     ~Forest(){
  38.         delete *rootNode;
  39.         delete data;
  40.         delete *leftChild;
  41.         delete *rightSibling;
  42.     }
  43.  
  44.     void nodes(int&) const{
  45.         if(Forest.data!=0){
  46.             count++;
  47.             Forest.leftChild.nodes(count);
  48.             Forest.rightSibling.nodes(count);
  49.         }else{
  50.             return count;
  51.         }
  52.     }
  53.  
  54.  
  55.     //Trying to go o the right-most node of the first forest and then copy the second forest onto the first as was done in the copy method above.
  56.     friend Forest& operator+(Forest&, Forest&){
  57.         while(rightSibling!=0){
  58.             rightSibling++;
  59.         }
  60.         rightSibling = *Forest.data;
  61.         if(*Forest.leftChild == 0) {
  62.             leftChild = new Forest(*otherForest.leftChild);
  63.         } else {
  64.             leftChild = 0;
  65.         }
  66.         if(otherForest.rightSibling == 0) {
  67.             rightSibling = new Forest(*otherForest.rightSibling);
  68.         } else {
  69.             right = 0;
  70.         }
  71.     };
  72.  
  73.     //methods I haven't worked on yet, copied as is from the assignment. They also run compile errors though.
  74.     friend ostream& operator<<(ostream&, const Forest&);
  75.     friend istream& operator>>(istream&, Forest&);
  76.    
  77. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement