Interchange

Initially I asked this Question:

How would I write a function in c++ that will interchange all left and right subtrees in a linked binary tree.
        1                                                            1
       /  \                                                          /  \
      2   3                becomes -->                    3     2
     /    / \                                                    /  \     \
   4    5   6                                                 6    5     4
        / \                                                         /  \
       7  8                                                       8   7

I don't need the entire program, just the required function segment of code.

the definition of the tree node I have is:

typedef struct treenode
{
      int data;
      struct treenode *left;
      struct treenode *right;
}TreeNode;

typedef TreeNode *TreePointer;

And I was given this answer:
void InterChange(TreeNode *tN)
{
    TreeNode *left =  tN->left; //get left node
    TreeNode *right =  tN->right; //grt right node
    if (left || right) //not last?
    {
         tN->left = right; //left = write
         tN->right = left; //righ =left
         if (tN->left) //exist left?
                InterChange(tN->left); //make interchange inside                                                  // left(recursion)
         if (tN->right) //exist right?
                 InterChange(tN->right); //make interchange inside                                                    //right(recursion)
     }
}

So the typedef TreeNode *TreePointer; after the structure is not needed or used in the InterChange function??
tlounsbuAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

arikkaCommented:
You do not need TreePointer type
Do not use it in the elegant
InterChange(TreeNode *tN)as well.

 
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
tlounsbuAuthor Commented:
So, the typedef TreeNode *TreePointer; line of code would not be needed in order for the function InterChange to work correctly in order to interchange the tree.
0
arikkaCommented:
Absolutely
0
tlounsbuAuthor Commented:
Is the function InterChange correct then? Would it interchange the tree as it is coded above with the structure?
0
tlounsbuAuthor Commented:
As it ends up, this code doesn't do what I was expecting it to at all.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.