-2

There's tons of info on how to mirror it, but those assume that node->left and node->right can be modified, such as this function (not mine, copied from another website).

void mirror(struct Node* node) {
    if (node==NULL) return;
    else { 
        struct Node* temp; 

        /* do the subtrees */
        mirror(node->left); 
        mirror(node->right); 

        /* swap the pointers in this node */
        temp = node->left; 
        node->left  = node->right; 
        node->right = temp;
    }
}  

I know there has to be recursion and a base case (basically the "do the subtrees" part) but I have no idea how to do the actual swapping since Node is a class with the left and right subtrees being private (can't change that).

For reference, this is the class constructor (there's no default constructor). I can provide more functions if they're important (there are accessor functions but no mutators). It also uses templates, hence the T.

Node(const T &x, Node *L = 0, Node *R = 0) : data(x), left(L), right(R) {}

I also made two other functions (treeHeight and countNodes), don't know if that's relevant. And I have to make a new tree to return, not modify the original tree.



Note - I did not want to provide this as an answer but wanted to inform the OP about C++ syntax

You originally had this for your function:

void mirror(struct Node* node) {
    if (node==NULL) return;
    else { 
        struct Node* temp; 

        /* do the subtrees */
        mirror(node->left); 
        mirror(node->right); 

        /* swap the pointers in this node */
        temp = node->left; 
        node->left  = node->right; 
        node->right = temp;
    }
}

In C++ you do not need to use the keyword struct when declaring it as a function-parameter nor as a declaration of a variable. You only need it when you are writing the declaration to the struct or class itself. You can simply do this:

void mirror(Node* node) {
    if (node==NULL) return;
    else { 
        Node* temp; 

        /* do the subtrees */
        mirror(node->left); 
        mirror(node->right); 

        /* swap the pointers in this node */
        temp = node->left; 
        node->left  = node->right; 
        node->right = temp;
    }
}  
Francis Cugler
  • 7,788
  • 2
  • 28
  • 59
Karan Bijani
  • 65
  • 1
  • 8
  • 1
    I made an edit and appended it at the bottom of your original question to help you with the syntax for C++ structs and classes. – Francis Cugler Nov 26 '18 at 08:44

1 Answers1

1

I would suggest you write your own swap member function:

void swapChildren(){ 
temp = node->left; 
node->left = node->right; 
node->right = temp; 
}

This swaps the left and right elements.

M. Denninger
  • 151
  • 9
  • This is homework, so I can't modify the Node header file. And I forgot to mention that I have to make a new tree, not modify the original tree (if that changes anything). – Karan Bijani Nov 26 '18 at 07:12
  • You could replace each Node with a new Node, in which you switch the left and right Node – M. Denninger Nov 26 '18 at 07:14
  • I assume you mean like this: Node* left = node->get_right(); Node* right = node->get_left(); How would I link this back to the new mirrored tree (let's say the name is mirrorTree) though? – Karan Bijani Nov 26 '18 at 07:17
  • Not like that, more like: node = new Node(node->data, node->RIGHT(), node->LEFT()); Oh, but I see that you can not modify the pointer of Node*, you would have to use Node** – M. Denninger Nov 26 '18 at 07:20
  • I figured it out: Node* mirrorTree = new Node(node->get_data(), mirror(node->get_right()), mirror(node->get_left())); seemed to work for me. Thanks for the help. – Karan Bijani Nov 26 '18 at 07:23
  • No problem, by the way. The code above is not modern C++, I suggest reading up on how to write modern C++ Code. There awesome books for that: https://stackoverflow.com/a/388282/9654834 – M. Denninger Nov 26 '18 at 07:26