why doesn't the code work on right part of tree?

This is a discussion on why doesn't the code work on right part of tree? within the C Programming forums, part of the General Programming Boards category; Idea is to swap Binary tree's element's with it's left node. Here's the code I came up with: Code: TREE ...

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    20

    why doesn't the code work on right part of tree?

    Idea is to swap Binary tree's element's with it's left node. Here's the code I came up with:

    Code:
    TREE swap_left (TREE T){
    element_type tmp;
               if(T!=NULL){
    	if(T->left){
    	tmp = T->element;
    	T->element = T->left->element;
    	T->left->element = tmp;	
    	T = T->left;
              }
               return (swap_left(T->right)&& swap_left(T->left));	
        }	
                  return T;
    }
    when I print elements, Elements left to node are swapped but not right....please help in fixing this.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Well . . . it seems like all your code does is swap the left. You never manipulate the right at all. I don't really understand how you could expect the right to be modified. If this is your intention:
    Idea is to swap Binary tree's element's with it's left node.
    then what does the right side of the tree have to do with it?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BST delete again, but this time I think I'm close
    By tms43 in forum C++ Programming
    Replies: 9
    Last Post: 11-05-2006, 05:24 PM
  2. Replies: 0
    Last Post: 11-04-2006, 10:07 AM
  3. Simple C++ code doesn't work
    By alex_dude_122 in forum C++ Programming
    Replies: 6
    Last Post: 10-18-2006, 12:53 PM
  4. Part of my C++ code
    By Maverick in forum C++ Programming
    Replies: 11
    Last Post: 02-15-2003, 08:02 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21