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

📄 树的高度、叶子节点数.c

📁 数据结构学习用到的一些程序!!里面有二叉树相关的几个
💻 C
字号:
#include<stdlib.h>
#include<stdio.h>
static int NUM=0;
static int DEEP=0;
static int TEMP=0;
#define m0 100                    //定义节点最大个数
#define Len  sizeof (BinNode)     //定义存储单元空间


typedef struct BinNode            //定义节点类型
{ 
	char data;
    struct BinNode *fch,*nsb;
} BinNode,*Bintree;

struct chtp   
{
	int len;      
	char ch[m0];    
};


struct BinNode *bt,*m;
struct chtp s;
int top=0,i=0;

void  crt_pre(Bintree *t)           //递归建立树
{
	char c;      
	c=s.ch[i];     
	i=i+1;
    if (c=='.')  
		*t=NULL;
	else 
	{ 
		*t=(BinNode *)malloc(Len);     
		(*t)->data=c;
		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()
{
	char /*c[]={'a','b','d','.','.','.','c','e','.','.','f','.','.','!'};*/
      c[]={'a','b','.','c','d','.','.','e','.','.','.','!'};
    int i=0;
    for(i=0,s.len=0;c[i]!='!';i++,s.len++) 
		s.ch[i]=c[i];
	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 + -