二叉树非递归遍历(前序、中序、后序)

来源:互联网 发布:淘宝金钻买家有什么用 编辑:程序博客网 时间:2024/05/16 23:52
  #include <iostream>  using namespace std; typedef struct node {//定义二叉树节点         int data;         struct node *lchild;         struct node *rchild; node(int data) {this->data=data;lchild=NULL;rchild=NULL; } void SetChild(struct node *lchild,struct node *rchild) {this->lchild=lchild;this->rchild=rchild; }         }BiNode,*BiTree; typedef struct node1{//定义堆栈,用于保存二叉树节点         BiTree data[30];     //默认30个元素 ,这里需要一个辅助堆栈!!!         int top;         }Stack;          void createTree(BiTree &T)   //先序递归创建树,这里注意参数的类型,T的类型是 "*&" ,如果是 "**" 代码稍加改动就OK... { /*      char ch;      cin.get(ch).get();      //过滤输入流中每次回车产生的回车符      if (ch==' ') T=NULL;    //这里首先判断是不是空格,如果是,则为该节点赋NULL      else{             T=(BiTree)malloc(sizeof(BiNode));             T->data=ch;             createTree(T->lchild);             createTree(T->rchild);           }   */    BiTree A=new BiNode(1);      BiTree B1=new BiNode(21);BiTree B2=new BiNode(22);      BiTree C1=new BiNode(31);BiTree C2=new BiNode(32);BiTree C3=new BiNode(33);BiTree C4=new BiNode(34);      BiTree D1=new BiNode(41);BiTree D2=new BiNode(42);BiTree D3=new BiNode(43);BiTree D4=new BiNode(44);      BiTree D5=new BiNode(45);BiTree D6=new BiNode(46);BiTree D7=new BiNode(47);BiTree D8=new BiNode(48);         A->SetChild(B1,B2);      B1->SetChild(C1,C2); B2->SetChild(C3,C4);      C1->SetChild(D1,D2);C2->SetChild(D3,D4);C3->SetChild(D5,D6);C4->SetChild(D7,D8); T=A; } void initstack(Stack *&st) {      st=(Stack *)malloc(sizeof(Stack));      st->top=-1; } bool isempty(Stack *st) {     return st->top==-1; } bool isfull(Stack *st) {     return st->top==19; } void push(Stack *st,BiTree T) {      if (!isfull(st))      st->data[++st->top]=T;     //栈顶指针始终指向堆栈最上面可用的一个元素,  //因此入栈时候,先要将指针加1,然后再执行入栈操作!      else cout<<"已满"<<endl; } BiTree pop(Stack *st) {      if (!isempty(st)) return st->data[st->top--];//出栈时,先取出栈顶指针指向的元素,  //然后再将指针减1,使其指向栈中下一个可用元素!  elsereturn NULL; } BiTree gettop(Stack *st) {        if (!isempty(st)) return st->data[st->top];  elsereturn NULL; } void preOrderNoRe(BiTree T)          // 前序遍历 {    Stack *st;    initstack(st);    BiTree p;    p=T;      while (p!=NULL||!isempty(st))      {            while (p!=NULL)            {                  cout<<p->data<<"  ";                  push(st,p);                  p=p->lchild;            }            if (!isempty(st))            {                        p=pop(st);                        p=p->rchild;            }                  } } void inOrderNoRe(BiTree T)       //中序遍历 {    Stack *st;    initstack(st);    BiTree p;    p=T;      while (p!=NULL||!isempty(st))      {            while (p!=NULL)            {                  push(st,p);                  p=p->lchild;            }            if (!isempty(st))            {                        p=pop(st);                        cout<<p->data<<"  ";                        p=p->rchild;            }                  } } void postOrderNoRe(BiTree T)          //后序遍历 {      BiTree p;      Stack *st;      initstack(st);      p=T;      int Tag[20];      //栈,用于标识从左(0)或右(1)返回       while (p!=NULL || !isempty(st))      {            while (p!=NULL)            {                  push(st,p);                  Tag[st->top]=0;                  p=p->lchild;            }            while (!isempty(st)&&Tag[st->top]==1)            {                  p=pop(st);                  cout<<p->data<<"  ";            }            if (!isempty(st))            {                             Tag[st->top]=1;   //设置标记右子树已经访问                              p=gettop(st);                             p=p->rchild;            }            else break;      } } int main() {     //cout<<"Enter char one by one                      hicjiajia"<<endl;     BiNode *T;     createTree(T);     cout<<endl;          cout<<"preOrderNoRe:  ";preOrderNoRe(T);cout<<endl;     cout<<"inOrderNoRe:   ";inOrderNoRe(T);cout<<endl;     cout<<"postOrderNoRe: ";postOrderNoRe(T);cout<<endl;      //system("pause");     return 0; }