Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- template <typename T>
- class Forest {
- private:
- T data;
- Tree<T> *leftChild;
- Tree<T> *rightSibling;
- Tree<T> *rootNode;
- //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?
- int count = 0;
- public:
- Forest(){
- rootNode = 0;
- leftChild = 0;
- rightSibling = 0;
- }
- Forest(const Forest& otherForest){
- data = otherForest.data;
- if(*rootNode == 0){
- rootNode = data;
- }
- if(*otherForest.leftChild == 0) {
- leftChild = new Forest(*otherForest.leftChild);
- } else {
- leftChild = 0;
- }
- if(otherForest.rightSibling == 0) {
- rightSibling = new Forest(*otherForest.rightSibling);
- } else {
- right = 0;
- }
- }
- ~Forest(){
- delete *rootNode;
- delete data;
- delete *leftChild;
- delete *rightSibling;
- }
- void nodes(int&) const{
- if(Forest.data!=0){
- count++;
- Forest.leftChild.nodes(count);
- Forest.rightSibling.nodes(count);
- }else{
- return count;
- }
- }
- //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.
- friend Forest& operator+(Forest&, Forest&){
- while(rightSibling!=0){
- rightSibling++;
- }
- rightSibling = *Forest.data;
- if(*Forest.leftChild == 0) {
- leftChild = new Forest(*otherForest.leftChild);
- } else {
- leftChild = 0;
- }
- if(otherForest.rightSibling == 0) {
- rightSibling = new Forest(*otherForest.rightSibling);
- } else {
- right = 0;
- }
- };
- //methods I haven't worked on yet, copied as is from the assignment. They also run compile errors though.
- friend ostream& operator<<(ostream&, const Forest&);
- friend istream& operator>>(istream&, Forest&);
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement