二叉树 前序 中序 后序 递归 和非递归

来源:互联网 发布:淘宝店注册 编辑:程序博客网 时间:2024/06/06 08:25
#include "stdafx.h"#include "iostream"#include<queue>  #include<stack>#include<stdlib.h>using namespace std;//BiTree   #include <stdio.h>  #include <malloc.h>    //define  #define  OK 1  #define  ERROR 0  //#define OVERFLOW -1    //typedef  typedef int Status;  typedef char TElemType;    //struct  typedef struct BiTNode{  TElemType data;  struct BiTNode *lchild, *rchild;  }BiTNode, *BiTree;  Status CreateBiTree(BiTree &T)  {  char ch;  scanf_s("%c", &ch);    if (ch == ' ')   T = NULL;  else  {  if (!(T = (BiTNode *)malloc(sizeof(BiTNode))))  return OVERFLOW;  T->data = ch;  //Visit(T->data);  CreateBiTree(T->lchild);  CreateBiTree(T->rchild);  }  return OK;  }  void preOrder1(BiTNode *root)     //非递归前序遍历 {stack<BiTNode*> s;BiTNode *p=root;while(p!=NULL||!s.empty()){while(p!=NULL){cout<<p->data<<" ";s.push(p);p=p->lchild;}if(!s.empty()){p=s.top();s.pop();p=p->rchild;}}}void preOrder2(BiTNode *root) {stack<BiTNode *>s;BiTNode *p=root;while (p||!s.empty()){if (p){s.push(p);cout<<p->data<<" ";p=p->lchild;}else{p=s.top();s.pop();p=p->rchild;}}}Status preOrder3(BiTree T, Status (* Visit)(TElemType e)){stack<BiTNode*>S;BiTNode *p;S.push(T);while (!S.empty()){p=S.top();//此时的p具有T的所有元素while (p){Visit(p->data);p=p->lchild;S.push(p);}S.pop();if(!S.empty()){p=S.top();S.pop();S.push(p->rchild);}}return OK;}  void inOrder1(BiTNode *root){stack<BiTNode *>s;BiTNode *p=root;while (p||!s.empty()){while (p){s.push(p);p=p->lchild;}if (!s.empty()){p=s.top();cout<<p->data<<" ";s.pop();p=p->rchild;}}}void inOrder2(BiTNode *root){stack<BiTNode *>s;BiTNode *p=root;while(p||!s.empty()){if (p){s.push(p);p=p->lchild;}else{p=s.top();cout<<p->data<<" ";s.pop();p=p->rchild;}}}void inOrder3(BiTNode *root){if(root){inOrder3(root->lchild);cout<<root->data<<" ";inOrder3(root->rchild);}}void postOrder(BiTNode *root){stack<BiTNode *>s;BiTNode *cur;//当前结点BiTNode *pre=NULL;//前一次访问的结点s.push(root);while (!s.empty()){cur=s.top();if((cur->lchild==NULL&&cur->rchild==NULL)||(pre!=NULL&&(pre==cur->lchild||pre ==cur->rchild))){cout<<cur->data<<" ";s.pop();pre=cur;}else{if (cur->rchild!=NULL)s.push(cur->rchild);if (cur->lchild!=NULL)s.push(cur->lchild);}}}Status LeverOrderTraverse(BiTree T, Status (* Visit)(TElemType e)){if (T==0)return ERROR;queue <BiTree> Q;Q.push(T);while (Q.empty()==0){BiTree t=Q.front();Q.pop();Visit(t->data);if(t->lchild)Q.push(t->lchild);if(t->rchild)Q.push(t->rchild);}return OK;}Status Visit(TElemType e)  {  cout<<e<<" ";return OK;  }  //---------------------------------////叶子个数int leafcount1(BiTNode *root){if(root==NULL)return 0;else if (root->lchild==NULL&&root->rchild==NULL)return 1;else return leafcount1(root->lchild)+leafcount1(root->rchild);}int leafcount2(BiTNode *root){static int LeafNum=0;//叶子最初的数目if (root){if (root->lchild==NULL&&root->rchild==NULL){LeafNum++;}else{leafcount2(root->lchild);leafcount2(root->rchild);}}return LeafNum;}int main(){ BiTree T;  CreateBiTree(T);cout<<"preOrder1:";preOrder1(T);cout<<endl;cout<<"preOrder2:";preOrder2(T);cout<<endl;cout<<"preOrder3:";preOrder3(T,Visit);cout<<endl;cout<<"-----------------------"<<endl;cout<<"inOrder1:";inOrder1(T);cout<<endl;cout<<"inOrder2:";inOrder2(T);cout<<endl;cout<<"inOrder3:";inOrder3(T);cout<<"-----------------------"<<endl;cout<<"(递归1)叶子结点数目:";cout<<leafcount1(T);cout<<endl;cout<<"(递归2)叶子结点数目:";cout<<leafcount2(T);cout<<endl;system("pause");return 0;}

0 0