Thread: how to make a non-recursive function from recursive

  1. #1
    Registered User
    Join Date
    Dec 2010

    how to make a non-recursive function from recursive

    Hi, could you help me with my problem? How to make a non-recusrsive function from a recursive one? This is my recursive function (function locates the node with a specified key in binary search tree)
    /* Key x - searched key in the tree                                    */
    /* Bvs t - pointer to the root of the tree                             */
    /* return value - pointer to found node, NULL if the key was not found */
    Position Member ( Key x, Bvs t )
                     Position pos = NULL;
                     if (t == NULL) return NULL;
                     if (t -> key == x) return (Position)t;
                     if (t -> key < x) pos = Member(x, t -> right);
                     if (t -> key > x) pos = Member(x, t -> left);
                     return pos;
    I think I should use loop and wrap all if conditions to loop and of course change bodies of conditions where function member is called, but I dont know how to implement it in C... Could you help me? Thanks....

  2. #2
    Registered User
    Join Date
    Jun 2005
    Try mapping out on paper what would happen for a couple of (non-trivial) trees and search keys. That will give you an insight as to what needs to be converted into loops, and what the end conditions are.

    I'm not going to give a more specific answer than that, as this strikes me as homework. Regardless of whether it's homework, you will benefit more if you work out the solution for yourself.

    This is one of those problems that initially looks hard but, once you have solved it, you realise it's easy and you can use the approach to solve other problems.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM