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

📄 6.42.cpp

📁 二叉树算法skjdgfjkfgdksfdgdfghfglh
💻 CPP
字号:
//从键盘读入一组数据,根据输入的数据创建一个二叉树,数据的插入按
//照每个节点的左节点小于等于该节点,右节点大于该节点的规则。
#include <stdio.h>
#include <stdlib.h>
#define Maxsize 10
struct TNode
{
		int data;
		struct TNode * left;
		struct TNode * right;
};

typedef struct TNode *Tree;
int n,c=0,k;
int count;
void Insert(Tree *t, int value)
{
	struct TNode *tmp;
	Tree T =*t;
	if(T == NULL)
	{
		tmp =(struct TNode *)malloc(sizeof(struct TNode));
		tmp->data = value;
		tmp->left = NULL;
		tmp->right = NULL;
		*t = tmp;
	}
	else
	{
		if( value <= T->data)
		{
			if(T->left != NULL)
				Insert(&(T->left), value);
			else
			{
				tmp =(struct TNode *)malloc(sizeof(struct TNode));
				tmp->data = value;
				tmp->left = NULL;
				tmp->right = NULL;
				(*t)->left = tmp;
			}
		}
		else
		{
			if(T->right != NULL)
				Insert(&(T->right), value);
			else
			{
				tmp =(struct TNode *)malloc(sizeof(struct TNode));
				tmp->data = value;
				tmp->left = NULL;
				tmp->right = NULL;
				(*t)->right = tmp;
			}
		}
	}
}




void CountTrees(Tree T)
{
	if(T == NULL)
		return;
	else
	{  if(T->left==NULL && T->right==NULL)
	      count++;
	   else
	   {
		CountTrees(T->left);
		CountTrees(T->right);
	   }
	}
}

void main()
{   
	Tree T;
	int i,a;
	T=NULL;
	printf("How many nodes?\n");
	scanf("%d",&n);
	for(i=0; i<n; i++)
	{
		scanf("%d",&a);
		Insert(&T,a);
	}
    printf("\n");

	CountTrees(T);
	printf("树叶的数目为:%d",count);
	printf("\n");
}

⌨️ 快捷键说明

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