Posted on 1999-12-22
Medium Priority
Last Modified: 2011-10-03
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;

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??
Question by:tlounsbu
  • 3
  • 2

Accepted Solution

arikka earned 50 total points
ID: 2301046
You do not need TreePointer type
Do not use it in the elegant
InterChange(TreeNode *tN)as well.


Author Comment

ID: 2301154
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.

Expert Comment

ID: 2301168

Author Comment

ID: 2301238
Is the function InterChange correct then? Would it interchange the tree as it is coded above with the structure?

Author Comment

ID: 2338801
As it ends up, this code doesn't do what I was expecting it to at all.

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

607 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question