【数据结构】二叉树的实现--需要时ctrl+c


#include <stdio.h>
#include <assert.h>
#include <string.h>
#include <stdlib.h>

typedef char BTDataType;

typedef struct BinaryTree
{
	struct BinaryTree* left;
	struct BinaryTree* right;
	BTDataType data;
}BTNode;

//初始化
BTNode* BTCreate(BTDataType x)
{
	BTNode* node = (BTNode*)malloc(sizeof(BTNode));
	if (node == NULL)
	{
		perror("malloc false");
		exit(-1);
	}
	BTNode* root = node;
	root->left = NULL;
	root->right = NULL;
	root->data = x;

	return root;
}

//前序
void PrevOrder(BTNode* root)
{
	if (root == NULL)
	{
		printf("NULL ");
		return;
	}

	printf("%c ", root->data);
	PrevOrder(root->left);
	PrevOrder(root->right);

}

//中序
void InOrder(BTNode* root)
{
	if (root == NULL)
	{
		printf("NULL ");
		return;
	}

	PrevOrder(root->left);
	printf("%c ", root->data);
	PrevOrder(root->right);

}

//后序
void PoseOrder(BTNode* root)
{
	if (root == NULL)
	{
		printf("NULL ");
		return;
	}

	PrevOrder(root->left);
	PrevOrder(root->right);
	printf("%c ", root->data);

}

//1、树的结点
//void TreeSize(BTNode* root, int* sz)
//{
//	if (root == NULL)
//	{
//		return 0;
//	}
//	(*sz)++;
//	TreeSize(root->left, sz);
//	TreeSize(root->right, sz);
//
//}

//2、树的结点
int TreeSize(BTNode* root)
{
	return root == NULL ? 0 : TreeSize(root->left) + TreeSize(root->right) + 1;
}

//树的左右有几个结点
int TreeLfRt(BTNode* root)
{
	if (root == NULL)
	{
		return 0;
	}
	
	return 1 + TreeLfRt(root->right) + TreeLfRt(root->left);
}

//树的层数
int TreeLayer(BTNode* root)
{
	if (root == NULL)
	{
		return 0;
	}
	int left = TreeLayer(root->left);
	int right = TreeLayer(root->right);
	
	return left > right ? left + 1 : right + 1;
}

//每层有几个结点
int TreeLayerNode(BTNode* root, int k, int count)
{
	if (k == 1)
	{
		return 1;
	}
	while (count > k-1)
	{
		if (root->left == NULL && root->right == NULL)
		{
			return 0;
		}
		else if (root->left != NULL && root->right != NULL)
		{
			return 2;
		}
		else if (root->left != NULL || root->right != NULL)
		{
			return 1;
		}

	}
	count += 1;
	return TreeLayerNode(root->left, k - 1,count) + TreeLayerNode(root->right, k - 1,count);
}

//书有几个叶子
int TreeLeafSize(BTNode* root)
{
	if (root == NULL)
		return 0;
	if (root->left == NULL && root->right == NULL)
		return 1;

	return TreeLeafSize(root->left) + TreeLeafSize(root->right);
}

int main()
{
	BTNode* A = BTCreate('A');
	BTNode* B = BTCreate('B');
	BTNode* C = BTCreate('C');
	BTNode* D = BTCreate('D');
	BTNode* E = BTCreate('E');
	BTNode* F = BTCreate('F');
	BTNode* G = BTCreate('G');
	BTNode* Q = BTCreate('Q');

	A->left = B;
	A->right = C;
	B->left = D;
	B->right = Q;
	C->left = E;
	C->right = F;
	F->right = G;

	//PrevOrder(A);
	//printf("\n");
	//int sz = 0;
	//sz = TreeLeafSize(A);
	//printf("%d ", sz);

	printf("%d ", TreeLayerNode(A, 3, 1));

	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值