Thread: Move binary tree elements to stack

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    58

    Move binary tree elements to stack

    Hello. I am trying to move binary tree elements to stack. I tried to move like this but i am getting empty stack still/.
    Code:
       void move(TreePtr current)
    {
         Stack start;
         Stack end;
         int D;
         if (current !=NULL)
         {
            move(current->left);
            D=current->element;
                    G=(Stack)malloc(sizeof(stack));
            end->number=D;
            end->next=start;
            start=end;
            move(current->right);
         }  
    }
    Last edited by krakatao; 04-09-2012 at 10:49 AM.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Several things:

    • Define "move binary tree elements to stack". There is more than one way to do it.
    • You don't provide us with crucial information like how TreePtr and Stack are declared or what G is.
    • Use descriptive variable names, not bizarro one-letter crap like D and G. Better yet, get rid of them. You don't need either of them as far as I can tell.
    • Don't use globals. Read this: Global Variables Are Bad.
    • Don't cast malloc. Read this: FAQ > Casting malloc - Cprogramming.com
    • If start and end are local variables, how do you plan to get them out of the function? You need to return them somehow.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    58
    "G" is "end" forgot to change when translated code.

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    It's hard to tell without seeing implementation details, but can you traverse your entire tree and print all node values or destroying the tree freeing all nodes? Can you build a stack by pushing values to it? If you know how to do these in isolation you can easily replace print with push as you traverse the tree.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Hint: You appear to be writing a "tree to rope" function.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Registered User
    Join Date
    Nov 2011
    Posts
    58
    I can print tree values and delete them. Also i can insert elements into stack but when i do like this it still does not work.
    Code:
    void move(TreePtr current)
    {
         Stack start;
         Stack end;
         int D;
         if (current !=NULL)
         {
            move(current->left);
            intoStack(start, current->element);
    
            move(current->right);
         }  
    }

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Like I said earlier:
    Quote Originally Posted by anduril462 View Post
    • If start and end are local variables, how do you plan to get them out of the function? You need to return them somehow.
    As you have it, each call creates a separate start and end that exist only in that function, and disappear as soon as the function returns. Not only that, since they contain the result of a malloc, which you never free, you have a memory leak. You need to declare start and end outside your move function, and pass them in.

  8. #8
    Registered User
    Join Date
    Nov 2011
    Posts
    58
    Now I understood. But how to pass them in?

  9. #9
    Registered User
    Join Date
    Nov 2011
    Posts
    58
    Thanks already solved.
    Last edited by krakatao; 04-10-2012 at 01:38 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary Tree doesn't print it's elements
    By cr33p in forum C Programming
    Replies: 2
    Last Post: 08-17-2011, 10:27 AM
  2. Stack operations from switch statement elements
    By mlsrar in forum C Programming
    Replies: 15
    Last Post: 10-02-2008, 01:12 PM
  3. Replies: 2
    Last Post: 08-01-2007, 01:08 PM
  4. Insert elements in binary tree
    By lastrial in forum C Programming
    Replies: 3
    Last Post: 05-23-2007, 10:22 AM
  5. display tree data in binary tree format
    By xddxogm3 in forum C++ Programming
    Replies: 4
    Last Post: 12-11-2003, 12:47 AM