user-0 提问: 二叉树非递归存储类型

回复:
user: user-1
content: 代码不全，怎么看，各有各的用法

user: user-1
content: 没有看到什么问题

user: user-0
content: 引用 1 楼 user-1的回复:代码不全，怎么看，各有各的用法 非递归遍历那部分。

user: user-0
content: 引用 1 楼 user-1的回复:代码不全，怎么看，各有各的用法 #include<stdio.h> #include<stdlib.h> #define maxsize 20  //－－－－－－－树的操作－－－－－－－－ //声明 typedef int elemtype; typedef struct Tnode {     elemtype data;     struct Tnode* left;     struct Tnode* right; }Bnode,*Btree;  //2.创建 Btree Btree_create() {     Btree T;     elemtype m;     scanf("%d",&m);     if(!m)     {         T=NULL;         return;     }     T=(Btree)malloc(sizeof(Bnode));     T->data=m;     T->left=Btree_create();     T->right=Btree_create(); }  //3.打印 void Btree_print(Btree T) {     if(!T)         return;     printf("%d ",T->data);     Btree_print(T->left);     Btree_print(T->right); }  //4.清空 void Btree_clear(Btree* T) {     if(!(*T))         return;     Btree_clear(&(*T)->left);     Btree_clear(&(*T)->right);     free(*T);     *T=NULL; }  //5.查找 Btree Btree_search(Btree T,elemtype e) {     if(!T)         return NULL;     if(T->data==e)         return T;     if(!Btree_search(T->left,e))         return Btree_search(T->right,e); }  //6.深度 int Btree_depth(Btree T) {     int m,n;     if(!T)         return 0;     m=Btree_depth(T->left);     n=Btree_depth(T->right);     return (m>n? m:n)+1; }  //7.叶结点 int Btree_leaf(Btree T) {     int m,n;     if(!T)         return 0;     if(!T->left&&!T->right)         return 1;     m=Btree_leaf(T->left);     n=Btree_leaf(T->right);     return m+n; } //8.总结点 int Btree_node(Btree T) {     int m,n;     if(!T)         return 0;     m=Btree_node(T->left);     n=Btree_node(T->right);     return m+n+1; }  //9.插入 void Btree_insert(Btree* T,elemtype e) {     Btree p;     if(!(*T))     {         p=(Btree)malloc(sizeof(Bnode));         p->data=e;         *T=p;         return;     }     if(e>(*T)->data)         Btree_insert(&(*T)->right,e);     else         Btree_insert(&(*T)->left,e); }  //－－－－－－非递归遍历－－－－－－－ //1.声明 typedef struct node {     Btree elem[maxsize];     int top; }sq_stack;  //3.进栈 void stack_push(sq_stack* s,Btree T) {     if(s->top==maxsize)         return;     s->elem[s->top]=T;     s->top++; } //4.出栈 Btree stack_pop(sq_stack* s) {     if(s->top==0)         return NULL;     s->top--;     return s->elem[s->top]; } //2.遍历 void Btree_travel2(Btree T) {     sq_stack s;     elemtype p[maxsize],i;     s.top=0;i=0;     if(!T)         return;     while(T||s.top)     {         while(T)         {             p[++i]=T->data;             stack_push(&s,T);             T=T->right;         }         T=stack_pop(&s);         T=T->left;     }     while(i)         printf("%d ",p[i--]); } //1.遍历 void Btree_travel1(Btree T) {     sq_stack s;     s.top=0;     if(!T)         return;     while(T||s.top)     {         while(T)         {             printf("%d ",T->data);             stack_push(&s,T);             T=T->left;         }         T=stack_pop(&s);         T=T->right;     } } //3.遍历 void Btree_travel3(Btree T) {     sq_stack s;     s.top=0;     Btree Bbool[maxsize];     if(!T)         return;     while(T||s.top)     {         while(T)         {             stack_push(&s,T);             Bbool[s.top-1]=0;             T=T->left;         }         if(!Bbool[s.top-1])         {             Bbool[s.top-1]=1;             T=s.elem[s.top-1]->right;         }         else          {             T=stack_pop(&s);             printf("%d ",T->data);             T=NULL;         }     } }  //－－－－－－－层序遍历－－－－－－－－  //1.声明 typedef struct sq_queue {     Btree base[maxsize];     int front;     int rear; }sq_queue;  //2.入队 void queue_in(sq_queue* Q,Btree T) {     if(Q->rear-Q->front==maxsize)         return;     Q->base[Q->rear++]=T; }  //3.出队 Btree queue_out(sq_queue* Q) {     if(Q->rear==Q->front)         return NULL;     return Q->base[Q->front++]; } //4.判空 int queue_isempty(sq_queue Q) {     if(Q.rear==Q.front)         return 1;     return 0; } //1.遍历 void Btree_travel4(Btree T) {     sq_queue Q;     Q.rear=Q.front=0;     if(!T)         return;     queue_in(&Q,T);      while(!queue_isempty(Q))     {         T=queue_out(&Q);         printf("%d ",T->data);         if(T->left)             queue_in(&Q,T->left);         if(T->right)             queue_in(&Q,T->right);     } } int main() {     Btree T,p;     T=Btree_create();     printf("先序递归");     Btree_print(T);     printf("\n先序非递归a ");     Btree_travel1(T);     printf("\n后序非递归b ");     Btree_travel2(T);     printf("\n后序非递归c ");     Btree_travel3(T);     printf("\n层序非递归d ");     Btree_travel4(T);     p=Btree_search(T,5);     if(p)         printf("查找成功 ");     printf("%d\n",p->data);     printf("\n深度%d\n",Btree_depth(T));     printf("叶结点%d\n",Btree_leaf(T));     printf("总结点%d\n",Btree_node(T));     Btree_insert(&T,5);     printf("插入元素5后");     Btree_print(T); }