【发布时间】:2016-04-23 12:52:31
【问题描述】:
我正在实现预先排序的二叉树遍历而不使用递归。 这是我的代码:
#include<iostream>
#include<stack>
using namespace std;
struct node{
int data;
node *left;
node *right;
};
node *getNewNode(int data){ //method for creating new node
node *newNode = new node();
newNode->data=data;
newNode->left=newNode->right = NULL;
return newNode;
}
node *Insert(node *root , int data){ //Method for insert new data in tree
if(root == NULL){
root = getNewNode(data);
}
else if(data>root->data){
root->right = Insert(root->right,data);
}
else{
root->left = Insert(root->left,data);
}
return root;
}
void Print(node *root){ //Method for preorder traversal with recursion
if(root == NULL){
return;
}
cout<<root->data<<" ";
Print(root->left);
Print(root->right);
}
void preOdr(node *root){ //Without recursion
stack<node*> stk;
cout<<root->data<<" ";
do{
a:
if(!(root->right==NULL&&root->left==NULL)){
if(root->right!=NULL){
stk.push(root->right);
}
if(root->left!=NULL){
stk.push(root->left);
}
}
cout<<stk.top()->data<<" ";
root=stk.top();
stk.pop();
goto a;
}
while(!stk.empty());
}
int main(){
node *root = NULL;
root = Insert(root,10);
root = Insert(root,6);
root = Insert(root,15);
root = Insert(root,3);
root = Insert(root,9);
root = Insert(root,11);
root = Insert(root,17);
root = Insert(root,11);
root = Insert(root,62);
root = Insert(root,135);
root = Insert(root,30);
root = Insert(root,98);
root = Insert(root,117);
root = Insert(root,176);
Print(root);
cout<<endl;
preOdr(root);
return 0;
}
在我的程序中,我还创建了使用递归的前序遍历方法来验证非递归方法给出的输出,即Print()。
在非递归方法中,首先我打印根,然后将该节点的左右子节点(如果有)分别推入堆栈。在此之后,我从堆栈中弹出项目并重复上述过程,直到堆栈不为空。
当我运行此代码时,它会正确输出,但之后会崩溃。我不明白名为preOrd() 的方法有什么问题。我附上了完整的代码以便更好地理解。
【问题讨论】:
-
去掉
goto中的preOdr它没有任何作用,你最终会在你的 do {} while() 中创建一个无限循环
标签: c++ data-structures tree binary-search-tree tree-traversal