树的高度、叶子节点数(自己输数据).c

来自「数据结构学习用到的一些程序!!里面有二叉树相关的几个」· C语言 代码 · 共 54 行

C
54
字号
#include<stdlib.h>
#include<stdio.h>
#define null 0
static int NUM=0;
static int DEEP=0;
static int TEMP=0;
typedef struct BinNode
{ 
	char data;
    struct BinNode *fch,*nsb;
} BinNode,*Bintree;

void  crt_pre(Bintree *t)
{
	char ch;      
	scanf("%c",&ch);
    if (ch=='.')  
		*t=null;
	else
	{   
		*t=(BinNode *)malloc(sizeof(BinNode));     
		(*t)->data=ch;
		crt_pre(&(*t)->fch);	  
		crt_pre(&(*t)->nsb);
	}
}

void preorder( BinNode *t,int temp)
{   int top=temp;
	if(t!=null)
	{    
		TEMP=temp+1;
		if(t->fch==null)
		{ 
			NUM++;
			if(TEMP>DEEP)DEEP=TEMP;
		}
		printf("%c",t->data);
		preorder(t->fch,top+1);
		preorder(t->nsb,top);
	}
}
void main()
{
	Bintree bt;
	printf("输入数据建立树:");
	crt_pre(&bt);      
	preorder(bt,0);
	printf("\n");
	printf("高度为:%d\n",DEEP);
	printf("叶子数为:%d",NUM);
	printf("\n");
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?