📄 homework4.cpp
字号:
#include<stdio.h>
#include<stdlib.h>
typedef char DataType;
typedef struct node
{
DataType data;
struct node *lchild,*rchild;
}BinTNode;
typedef BinTNode *BinTree;
int count;
void CreateBinTree(BinTree *T);
void Leafnum(BinTree T);
main()
{BinTree T;
char ch1,ch2;
printf("welcome to you:\n");
ch1='y';
while(ch1=='y'||ch1=='Y')
{printf("\nA---------------二叉树建立");
printf("\nB---------------count the leaf");
printf("\nC---------------quit\n");
scanf("\n%c",&ch2);
switch(ch2)
{case 'a':
case 'A':printf("请输入按先序建立二叉树的结点序列:\n");
CreateBinTree(&T);break;
case'b':
case'B':count=0;Leafnum(T);
printf("该二叉树有%个叶子。\n",count);
case'c':
case'C':ch1='n';break;
default:ch1='n';
}
}return 0;
}
void CreateBinTree(BinTree *T)
{
char ch;
scanf("\n%c",&ch);
if(ch=='0') *T=NULL;
else{*T=(BinTNode*)malloc(sizeof(BinTNode));
(*T)->data=ch;
CreateBinTree(&(*T)->lchild);
CreateBinTree(&(*T)->rchild);
}
}
void Leafnum(BinTree T)
{
if(T)
{if(T->lchild==NULL&&T->rchild==NULL)
count++;
Leafnum(T->lchild);
Leafnum(T->rchild);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -