此部分是对于数据结构的边缘部分的理解
#include <iostream>
#include<stack>
using namespace std;
struct node
{
char data;
node *leftchild;
node *rightchild;
};
void Create(node *&p)//扩展先序遍历建立二叉树
{
char ch;
cin>>ch;
if (ch=='#')
p=NULL;
else
{
p=new node;
p->data=ch;
Create(p->leftchild);
Create(p->rightchild);
}
}
void PreOrder(node *root)
{
stack<node*>s;
node *p;
s.push(NULL);
p=root;
while(!s.empty())
{
if (p==NULL)
break;
cout<<p->data<<" ";
if (p->rightchild!=NULL)//如果右子树不为空,那么将右子树入栈
s.push(p->rightchild);
if (p->leftchild!=NULL)//如果左子树不为空,那么将当前节点迭代为它的左孩子
p=p->leftchild;
else//如果左子树为空,那么就从栈中取出一个节点
{
p=s.top();