标签:分享图片 display clu 后序遍历 type 利用 col malloc scan
1 #include <stdio.h> 2 #include <malloc.h> 3 typedef struct node1{ 4 char data; 5 struct node1 *lchild,*rchild; 6 }node1,BTCHINALR; 7 8 BTCHINALR *createbt(){ 9 BTCHINALR *q; 10 struct node1 *s[30]; 11 int j,i,x; 12 printf("建立二叉树,输入结点对应的编号和值,编号和值之间用逗号隔开\n\n"); 13 printf("i,x="); 14 scanf("%d,%c",&i,&x); 15 while(i!=0 && x!=‘$‘){ 16 q=(BTCHINALR *)malloc(sizeof(BTCHINALR)); 17 q->data=x;q->lchild=NULL;q->rchild=NULL; 18 s[i]=q; 19 if(i!=1){ 20 j=i/2; 21 if(i%2==0){s[j]->lchild=q; 22 } 23 else{ 24 s[j]->rchild=q; 25 } 26 } 27 printf("i,x="); 28 scanf("%d,%c",&i,&x); 29 } 30 return s[1]; 31 } 32 33 34 void inorder (BTCHINALR *bt){ 35 if(bt!=NULL){ 36 37 inorder (bt->lchild); 38 printf("%c ",bt->data); 39 inorder(bt->rchild);} 40 } 41 void preorder (BTCHINALR *bt){ 42 if(bt!=NULL){ 43 printf("%c ",bt->data); 44 preorder (bt->lchild); 45 preorder (bt->rchild); 46 47 } 48 } 49 void laorder(BTCHINALR *bt){ 50 if(bt!=NULL){ 51 laorder (bt->lchild); 52 laorder (bt->rchild); 53 printf("%c ",bt->data); 54 } 55 } 56 main(){ 57 BTCHINALR *bt; 58 char ch; 59 int i; 60 bt=createbt(); 61 printf("\n中序遍历二叉树"); 62 inorder(bt); 63 printf("\n"); 64 printf("\n先序遍历二叉树"); 65 preorder(bt); 66 printf("\n"); 67 printf("\n后序遍历二叉树"); 68 laorder(bt); 69 printf("\n"); 70 }
测试结果:
2.利用非递归方法实现中序遍历:
1 #include <stdio.h> 2 #include <malloc.h> 3 typedef struct node1{ 4 char data; 5 struct node1 *lchild,*rchild; 6 }node1,BTCHINALR; 7 8 BTCHINALR *createbt(){ 9 BTCHINALR *q; 10 struct node1 *s[30]; 11 int j,i,x; 12 printf("建立二叉树,输入结点对应的编号和值,编号和值之间用逗号隔开\n\n"); 13 printf("i,x="); 14 scanf("%d,%c",&i,&x); 15 while(i!=0 && x!=‘$‘){ 16 q=(BTCHINALR *)malloc(sizeof(BTCHINALR)); 17 q->data=x;q->lchild=NULL;q->rchild=NULL; 18 s[i]=q; 19 if(i!=1){ 20 j=i/2; 21 if(i%2==0){s[j]->lchild=q; 22 } 23 else{ 24 s[j]->rchild=q; 25 } 26 } 27 printf("i,x="); 28 scanf("%d,%c",&i,&x); 29 } 30 return s[1]; 31 } 32 33 34 void inorder (BTCHINALR *bt){ 35 if(bt!=NULL){ 36 37 inorder (bt->lchild); 38 printf("%c ",bt->data); 39 inorder(bt->rchild);} 40 } 41 void preorder (BTCHINALR *bt){ 42 if(bt!=NULL){ 43 printf("%c ",bt->data); 44 preorder (bt->lchild); 45 preorder (bt->rchild); 46 47 } 48 } 49 void laorder(BTCHINALR *bt){ 50 if(bt!=NULL){ 51 laorder (bt->lchild); 52 laorder (bt->rchild); 53 printf("%c ",bt->data); 54 } 55 } 56 //非递归进行的中序遍历二叉树 57 void inorder_notrecursive(BTCHINALR *bt){ 58 BTCHINALR *q,*s[20]; 59 int top =0; 60 int bool=1; 61 q=bt; 62 do{ 63 while(q!=NULL){ 64 top ++;s[top]=q;q=q->lchild; 65 } 66 if(top==0) bool =0; 67 else{ 68 q=s[top]; 69 top--; 70 printf("%c ",q->data); 71 q=q->rchild;} 72 } 73 while(bool); 74 } 75 76 main(){ 77 BTCHINALR *bt; 78 char ch; 79 int i; 80 bt=createbt(); 81 printf("\n使用递归进行中序遍历二叉树"); 82 inorder(bt); 83 printf("\n"); 84 printf("\n使用非递归进行的中序遍历二叉树"); 85 inorder_notrecursive(bt); 86 printf("\n"); 87 printf("\n先序遍历二叉树"); 88 preorder(bt); 89 printf("\n"); 90 91 printf("\n后序遍历二叉树"); 92 laorder(bt); 93 printf("\n"); 94 }
测试结果:
标签:分享图片 display clu 后序遍历 type 利用 col malloc scan
原文地址:https://www.cnblogs.com/Catherinezhilin/p/9089801.html