Flipping a binary tree using recursion

• 03-03-2002
Jim0669
Flipping a binary tree using recursion
can anyone give me some ideas on how i could flip a binary tree around using a recursive function..for example.

struct BinaryTreeNode
{
int data;
BinaryTreeNode *left;
BinaryTreeNode *right;
};

Example original tree: Example new tree:
1 1
/ \ / \
2 3 3 2
/ \ / \
4 5 5 4
• 03-03-2002
Unregistered
old tree
1
/\
2 3
/\
4 5

new tree
1
/\
3 2
/\
5 4
• 03-04-2002
Unregistered
is it a binary tree? binary tree should be
1
\
2
\
3
\
4
\
5

• 03-04-2002
hk_mp5kpdw
Unregistered said:

Quote:

is it a binary tree? binary tree should be
1
\
2
\
3
\
4
\
5
The way a binary tree looks depends on the order the items are added to a tree. If the numbers were added in order (least to greatest), then yes the tree would look like you describe.
• 03-04-2002
Sorensen
Copy the tree, then either do a pre-order traversal on the source copying to the destination by means of a post-order traversal, or do a post-order traversal of the source and a pre-order traversal of the destination.
• 03-04-2002
samGwilliam
Quote:

Originally posted by hk_mp5kpdw
Unregistered said:

The way a binary tree looks depends on the order the items are added to a tree. If the numbers were added in order (least to greatest), then yes the tree would look like you describe.

...which is where the height balancing comes in.
• 03-04-2002
Sorensen
Using a binary tree you can insert elements any way you like. A binary search tree orders the elements on insertion in some pre-defined manner.
• 03-04-2002
Unregistered
I have some code that could help. Let's see your code first.

When I think height balancing I think AVL trees not binary trees.!!!

Mr. c.