⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

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

📁 数据结构学习用到的一些程序!!里面有二叉树相关的几个
💻 C
字号:
#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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -