二叉树的层次遍历(非递归)可否借助栈来实现?

来源:百度知道 编辑:UC知道 时间:2024/06/05 12:46:43
二叉树的层次遍历(非递归)可否借助栈来实现?
二叉树的中序遍历(非递归)可否借助队列来实现?
如果可以的话,请给出相应的算法!不用具体实现,谢谢!!!
请说的仔细点!给出相应的算法,不必用具体的语言去实现!谢谢

//层次遍历代码

template<class T>
void BinTree<T>::view()
{
if (IsNull()) return;
deque<TreeNode<T>*> q;

TreeNode<T>* temp;
q.push_back(root);
while(!q.empty())
{
temp = q.front();
q.pop_front();
cout<<temp->data;
if (temp->Left!=NULL)
q.push_back(temp->Left);
if (temp->Right!=NULL)
q.push_back(temp->Right);
}
cout<<endl;
}

1.先序遍历非递归算法
#define maxsize 100
typedef struct
{
Bitree Elem[maxsize];
int top;
}SqStack;

void PreOrderUnrec(Bitree t)
{
SqStack s;
StackInit(s);
p=t;

while (p!=null || !StackEmpty(s))
{
while (p!=null) //遍历左子树
{
visite(p->data);
push(s,p);
p=p->lchild;
}//endwhile

if (!StackEmpty(s)) //通过下一次循环中的内嵌whi