@Rahul : if you check the flow properly ,(lets concentrate on the root node , call other as left and right subtree) you will find that after done with reversing root->left->left subtree , it reaches root(backtrack to root) node and then swap root->left and root->right. now because it is inorder way of traversal , we are done with function(&(*node)->Left); now next recursion for root node is function(&(*node)->Right);. function(&(*node)->Right); ---> this will again do reversing steps in root->right subtree.(reverting back the old state). after backtracking it reaches root. but wait there is no swapping part after function(&(*node)->Right) , but it is after function(&(*node)->Left);
but we have as discussed before function(&(*node)->Right) is the only recursion root needs to complete in order move out of the function. hence root->left and root->right remain swapped. but program is similar to swapping root->left and root->right which can be done in constant time but taken O(n) to do the same. hope you have understood it :) On Fri, Feb 10, 2012 at 10:16 AM, Rahul Menon <menonrahul1...@gmail.com>wrote: > What about just the root being reversed? > Why is it different only in case of root? > > Rahul > > On Feb 9, 10:52 pm, Don <dondod...@gmail.com> wrote: > > Because it reverses one side twice and the other side not at all. > > It does a lot of work to accomplish nothing. > > Don > > > > On Feb 9, 9:06 am, Rahul Menon <menonrahul1...@gmail.com> wrote: > > > > > > > > > > > > > > > > > How come it just reversed the root? I still dont get it! > > > > > Rahul > > > > > On Feb 9, 7:57 pm, Don <dondod...@gmail.com> wrote: > > > > > > It appears to be an attempt to reverse the tree. However, there is a > > > > problem. It reverses the left sub-tree, then swaps the left and right > > > > sub-trees. Then it reverses the right sub-tree. But wait! The > original > > > > left sub-tree which we reversed is now the right sub-tree, so we > > > > actually unreversed it. And the left sub-tree has never been reversed > > > > at all. So I don't think that it will work. The actual result will be > > > > that left and right will be swapped in the root node. Beyond that, > > > > there will be no change. > > > > > > To make it work as intended, either do the two recursive calls one > > > > after the other, or change the second one from Right to Left. > > > > > > Don > > > > > > On Feb 9, 8:38 am, Rahul Menon <menonrahul1...@gmail.com> wrote: > > > > > > > What does this function do? > > > > > > > void function(Node **node){ > > > > > if(*node!=NULL){ > > > > > function(&(*node)->Left); > > > > > Node *temp; > > > > > temp = (*node)->Left; > > > > > (*node)->Left= (*node)->Right; > > > > > (*node)->Right = temp; > > > > > function(&(*node)->Right); > > > > > } > > > > > > > }- Hide quoted text - > > > > > > > - Show quoted text -- Hide quoted text - > > > > > - Show quoted text - > > -- > You received this message because you are subscribed to the Google Groups > "Algorithm Geeks" group. > To post to this group, send email to algogeeks@googlegroups.com. > To unsubscribe from this group, send email to > algogeeks+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/algogeeks?hl=en. > > -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.