二叉木の作成、先序遍歴、中序遍歴、後序遍歴、葉ノードの完全な関数を求めるc言語完全プログラム.


 
 
#include<stdlib.h>
#include<stdio.h>


typedef struct bitnode{
	int date;
	struct bitnode * lchild, *rchild;
}bitnode,*bitree;
int j=0;
//       
bitree *createbitree(bitree *T);
int Qtraversebitree(bitree T);
int Ztraversebitree(bitree T);
int Htraversebitree(bitree T);
int Ftraversebitree(bitree T);
/*******************   ****************************/


main()
{
	bitree Tree,*T ; int k;
     do                                 
	{
		printf("
╔-----------------------------------------------╗"); // printf("
┆ ----1 ------2 ┆"); printf("
┆ ----3 ------4 ┆"); printf("
┆ --- 5 ------6 ┆"); printf("
╚-----------------------------------------------╝
"); printf(" : "); scanf("%d",&k); // switch(k) // {case 1: printf(" 0 , :
"); T=createbitree(&Tree); break; case 2:Qtraversebitree(*T ); break; case 3:Ztraversebitree(*T ); break; case 4:Htraversebitree(*T ); break; case 5:printf("
: %d
",Ftraversebitree(*T )); break; case 6:break; default:printf(" !
");break; } }while(k!=6); // i 6 return 0; } /******************* ****************************/ bitree *createbitree(bitree *T) { char ch; scanf("%d",&ch); if(ch==0) (*T)=NULL; else{ if(!((*T) =(bitnode *)malloc(sizeof(bitnode)))) exit(0); (*T)->date = ch; // createbitree(&(*T)->lchild); createbitree(&(*T)->rchild); } return T; } /******************* ****************************/ Qtraversebitree(bitree T) { if(T){printf("%d ",T->date); if(Qtraversebitree(T->lchild)) if(Qtraversebitree(T->rchild)) return 1; return 0; } else return 1; } /******************* ****************************/ Ztraversebitree(bitree T) { if(T){if(Ztraversebitree(T->lchild)) printf("%d ",T->date); if(Ztraversebitree(T->rchild)) return 1; return 0; } else return 1; } /******************* ****************************/ Htraversebitree(bitree T) { if(T){if(Htraversebitree(T->lchild)) if(Htraversebitree(T->rchild)) printf("%d ",T->date); return 1; return 0; } else return 1; } /****************** *************/ Ftraversebitree(bitree T) { if(T){if(!((T->lchild)||(T->rchild))) j++; if(Ftraversebitree(T->lchild)) if(Ftraversebitree(T->rchild)) return j; return j; } else return j; }