简体   繁体   English

while循环无限执行,即使在执行流程到达包含while的函数结束后也没有任何递归调用

[英]while loop executing infinitely without any recursive call even after execution flow reaches end of function containing while

below is a function i am using for inorder traversal of a binary search tree . 下面是我使用的功能inorder一的遍历binary search tree Everything works well till the loop while(!st.empty()) gets terminated but after that the execution flow again goes to while(!st.empty()) hence loop turns into an infinite loop 一切顺利,直到循环, while(!st.empty())被终止但在此之后执行流程再次进入while(!st.empty())因此循环变成无限循环

structure of node 节点结构

class bst{ 
private:
    bst *lLink;
    int info;
    bst *rLink;

friend void inorder(bst &);
 };
void inorder(bst &);

calling part 呼叫部分

string c;
cout<<"\na, b or c ?";
cin>>c;

 if(c == "a")
 {
  inorder(nodeVec[0]);  //nodeVec is vector containing all nodes (objects) of tree with first element as root
 }

 //......

function 功能

void inorder(bst &item)
{
stack<bst> st;
st.push(item);


while((st.top()).getLeftChild()!=NULL)
{
    st.push(*((st.top()).getLeftChild()));
}

while(!st.empty())
{
    if(st.top().getInfo()==item.getInfo()) //true if traversal is done with all
                                           //left subtree of root and only oneitem remained in stack i.e. only root remained
    {                                     
        cout<<st.top().getInfo()<<endl;

        if(st.top().getRightChild()!=NULL)
            inorder(*(item.getRightChild()));

        else
            st.pop();
    }

    else{
    cout<<st.top().getInfo()<<endl;
    if(st.top().getRightChild()!=NULL)
    {
        bst curr=*(st.top().getRightChild());
        st.pop();
        st.push(curr);
    }
    else{
        st.pop();
    }
    }
}
 cout<<st.empty();  //for testing, this prints 1
} //execution goes here and then again at while(!st.empty())

suppose the tree is like this: 假设树是这样的:

      69
     /  \
    43  89
   /   /
  24  73
 /
14
 \
  21

it gives output 它给出了输出

14
21
24
43
69
73
89
69   //loop starts again
73
89
69
73
89
...

You should be removing the element from the stack when it's printed out. 您应该在打印出来时从堆栈中删除该元素。

You're leaving it on the stack in the first block of the if() that checks that the left side of the tree has been completed. 您将它留在if()的第一个块中的堆栈上,该块检查树的左侧是否已完成。

if(st.top().getRightChild()!=NULL)
    inorder(*(item.getRightChild()));

// else  // <--- don't use else here.. Always pop
    st.pop();

This seems way more complicated than it needs to be, Here is a untested version of what I think would work better. 这似乎比它需要的更复杂,这是我认为可以更好地工作的未经测试的版本。

struct bst{ 
    int info;
    bst *right;
    bst *left;
};

void inorder(bst & item) {
    std::stack<bst*> st;
    bst* current = &item;
    while(current || !st.empty()) {
        if(current) {
            st.push(current);
            current = current->left;
        } else {
            current = st.top();
            st.pop();
            std::cout << current->info << std::endl;
            current = current->right;
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么做while循环无限执行呢? - Why is this do while loop infinitely executing? 为什么即使满足条件,While 循环在 C++ 中仍会无限执行? - Why does the While Loop keeps executing infinitely in C++ even when the condition is met? 收到警告:控制在 while_loop function 中到达非空 function [-Wreturn-type] 的末尾 - Getting the warning: control reaches end of non-void function [-Wreturn-type] in the while_loop function 使用while循环控制达到非void函数(c ++)的结尾 - Control reaches end of non void function (c++) using while loop 有什么功能可以代替while或while循环在c ++编程中传递流程? - Is there any function available to transfer the flow in c++ programming alternative to while or do while loop? 递归函数中的for循环在递归结束后继续 - For loop in recursive function continues after end of recursion 执行while循环后程序崩溃 - The program crashes after executing while loop 编译器警告:执行到达返回值 function 的末尾而不返回值 - Compiler Warning: Execution reaches the end of a value-returning function without returning a value 为什么while循环无限重复? - Why does this do while loop repeat infinitely? 警告:控制到达递归函数中非空函数的结尾 - warning: control reaches end of non-void function in recursive function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM