2

I am trying to turn this recursive function into a non-recursive one.This is a search function from a binary search tree. I am aware it is natural to make it recursive, but for learning purposes I would like to make it non-recursive. How could I do this? Thanks in advance!

    bool Search(BstNode* root, string data) {

    if (root == NULL) return false;
    else if (root->data == data) return true;
    else if (data <= root->data) return Search(root->left, data);
    else return Search(root->right, data);

}
1
  • 1
    Set up a variable at the top for the root. Turn it into a loop. Update the root to be root->left or root->right before the next iteration. Commented Oct 11, 2016 at 20:29

2 Answers 2

6

Here is mechanical way to make a recursive algorithm non-recursive.

bool Search(BstNode* root, string data) {
  if (root == NULL) return false;
  else if (root->data == data) return true;
  else if (data <= root->data) return Search(root->left, data);
  else return Search(root->right, data);
}

Bundle up the state (arguments and local variables):

bool Search(BstNode* root, string data) {
  struct State {
    BstNode* root;
    string data;
  };
  State state{root, data};

  if (state.root == NULL) return false;
  else if (state.root->data == state.data) return true;
  else if (data <= state.root->data) return Search(state.root->left, state.data);
  else return Search(state.root->right, state.data);
}

wrap body in a loop:

bool Search(BstNode* root, string data) {
  struct State {
    BstNode* root;
    string data;
  };
  State state{root, data};

  while(true) {
    if (state.root == NULL) return false;
    else if (state.root->data == state.data) return true;
    else if (data <= state.root->data) return Search(state.root->left, data);
    else return Search(state.root->right, data);
  }
}

Replace case where you tail-end recurse (return recursive_call) with changing state and continue:

bool Search(BstNode* root, string data) {
  struct State {
    BstNode* root;
    string data;
  };
  State state{root, data};

  while(true) {
    if (state.root == NULL) return false;
    else if (state.root->data == state.data) return true;
    else if (data <= state.root->data) {
      state = {state.root->left, state.data};
      continue;
    } else  {
      state = {state.root->right, state.data};
      continue;
    }
  }
}

Now, if there are any more recursive calls that are not return recursive_call, add a manual stack of state and push/pop it instead of changing the back. Include the location of the return state as a void** in the code with labels.

This isn't required here, so I won't bother doing it.

Sign up to request clarification or add additional context in comments.

1 Comment

This really breaks it down and makes it simple for me. Thank you!
1

You can usually make a recursive function in general iterative by essentially 'putting' the recursive calls onto a stack, and then using

while !stack.is_empty() do stack.pop() kind of thing

as this is essentially what a compiler will do given that recursion doesn't happen at the machine-code level

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.