- 8
- 0
- 约3.08千字
- 约 18页
- 2019-03-17 发布于江苏
- 举报
#includemalloc.h
#define NULL 0
#includestdio.h
typedef struct node
{
char data;
struct node *lchild,*rchild;
}NODE;
int count;
NODE *crt_bt_pre()/*二叉树先序创建算法*/
{
NODE * bt;
char ch;
printf(\n\t\t\t);
scanf(%c,ch);
getchar();
if(ch== ) bt=NULL;
else
{
bt=(NODE*)malloc(sizeof(NODE));
bt-data=ch;
printf(\n\t\t\t请输入%c结点的左孩子:,bt-data);
bt-lchild=crt_bt_pre();
printf(\n\t\t\t请输入%c结点的右孩子:,bt-data);
bt-rchild=crt_bt_pre();
}
return(bt);
}
void Preorder(NODE* bt)/*二叉树先序递归遍历算法*/
{
if(bt!=NULL)
{
printf(\n\t\t\t %c,bt-data);
Preorder(bt-lchild);
Preorder(bt-rchild);
}
}
void Inorder(NODE* bt)
{
if(bt!=NULL)
{
Inorder(bt-lchild);
printf(\n\t\t\t %c,bt-data);
Inorder(bt-rchild);
}
}
void Postorder(NODE* bt)
{
if(bt!=NULL)
{
Postorder(bt-lchild);
Postorder(bt-rchild);
printf(\n\t\t\t %c,bt-data);
}
}
int CountLeaf(NODE *bt)/*求二叉树叶子结点数的递归遍历算法*/
{
if(bt==NULL)
return 0;
if(bt-lchild==NULLbt-rchild==NULL)
count++;
CountLeaf(bt-lchild);
CountLeaf(bt-rchild);
return(count);
}
int CountNode (NODE* bt)/*求二叉树结点数的递归遍历算法*/
{
if(bt==NULL)
return 0;
else
count++;
CountNode(bt-lchild);
CountNode(bt-rchild);
return(count);
}
int TreeDepth(NODE* bt)/*求二叉树深度的递归遍历算法*/
{
int x,y;
if(bt==NULL)
return 0;
else
x=TreeDepth(bt-lchild);
y=TreeDepth(bt-rchild);
if(xy)
return(x+1);
else
return(y+1);
}
void main()
{
NODE *bt;
char choice;
int j=1;
int x;
while(j)
{
printf(\n\n\n);
printf(\t\t\t-二叉树的基本运算--\n);
printf(\n\t\t\t************************************);
printf(\n\t\t\t* 1-------建二 差树 *);
printf(\n\t\t\t* 2-------先序 遍历 *);
printf(\n\t\t\t* 3-------中序 遍历 *);
printf(\n\t\t\t* 4-------后序 遍历 *);
printf(\n\t\t\t* 5-------统计 叶子数 *);
printf(\n\t\t\t* 6-------统计 结点数 *);
printf(\n\t\t\t* 7-------求二叉树深度 *);
printf(\n\t\t\t*
您可能关注的文档
最近下载
- 水平二 田径 大单元教学设计(18课时,表格式)(第三版).docx VIP
- RISN-TG016-2014 生活垃圾流化床焚烧工程技术导则(2014年4月).doc
- 2025年上海市崇明区高三下学期高考&等级考二模地理试卷含详解.docx VIP
- 全科医学科学研究.pptx VIP
- 三菱MAXIEZ调整称量装置-文档资料.docx VIP
- 2026春季新苏教版一年级数学下册全册教案.pdf
- 一种垃圾压缩机构.pdf VIP
- 基于PLC-S7-200交通信号灯的控制系统设计.pdf VIP
- 外研版(三起)(2024)三年级下册英语Unit 4《What’s your hobby?》教案(共4课时).docx VIP
- 护士求职简历word模板(清新).docx VIP
原创力文档

文档评论(0)