如何使这个搜索功能非递归?

时间:2021-02-14 19:46:59

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);

}

2 个解决方案

#1


5  

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:

用更改状态替换尾端递归(返回recursive_call)的情况并继续:

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.

现在,如果有任何更多的递归调用没有返回recursive_call,添加一个手动状态堆栈并推送/弹出它而不是更改后面。在带有标签的代码中包含返回状态的位置作为void **。

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

这不是必需的,所以我不打算这样做。

#2


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

while!stack.is_empty()做stack.pop()这样的事情

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

因为这基本上是编译器会做的事情,因为递归不会发生在机器代码级别

#1


5  

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:

用更改状态替换尾端递归(返回recursive_call)的情况并继续:

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.

现在,如果有任何更多的递归调用没有返回recursive_call,添加一个手动状态堆栈并推送/弹出它而不是更改后面。在带有标签的代码中包含返回状态的位置作为void **。

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

这不是必需的,所以我不打算这样做。

#2


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

while!stack.is_empty()做stack.pop()这样的事情

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

因为这基本上是编译器会做的事情,因为递归不会发生在机器代码级别