首先是二叉树结点类型定义
typedef struct node {
ElemType data;
struct node* lchild;
struct node* rchild;
}*BST,BSTree;
此为一开始的广度遍历代码:
void Level(BST root)
{
queue<BSTree> Q;
BST temp;
if (!root)
return;
Q.push(*root);
while (Q.size()>0)
{
temp = &(Q.front());
Q.pop();
cout << temp->data;
if (temp->lchild)
Q.push(*(temp->lchild));
if (temp->rchild)
Q.push(*(temp->rchild));
}
}
遍历结果总是缺少子树或者重复包含,后改为以下代码:
正常打印
void Level(BST root)
{
queue<BSTree> Q;
BST temp;
BSTree P;
if (!root)
return;
Q.push(*root);
while (Q.size()>0)
{
P = Q.front();
temp = &P;
Q.pop();
cout << temp->data;
if (temp->lchild)
Q.push(*(temp->lchild));
if (temp->rchild)
Q.push(*(temp->rchild));
}
}
怀疑问题出在#include中的pop()函数,求解答,Orz。