二叉树相关函数实现

二叉树实现

二叉树结构定义

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include"Queue.h"
typedef char BTDataType;
typedef struct BinaryTreeNode
{
	BTDataType data;
	struct BinaryTreeNode* leftChild;
	struct BinaryTreeNode* rightChild;
}BTNode;

创建结点

BTNode* BuyNode(BTDataType x)
{
	BTNode* newnode = (BTNode*)malloc(sizeof(BTNode));
	if (newnode == NULL)
		exit(-1);
	newnode->data = x;
	newnode->leftChild = newnode->rightChild = NULL;
	return newnode;
}

模拟创建二叉树,用于测试

BTNode* CreatBinaryTree()
{
	BTNode* nodeA = BuyNode('A');
	BTNode* nodeB = BuyNode('B');
	BTNode* nodeC = BuyNode('C');
	BTNode* nodeD = BuyNode('D');
	BTNode* nodeE = BuyNode('E');
	BTNode* nodeF = BuyNode('F');
	BTNode* nodeG = BuyNode('G');
	nodeA->leftChild = nodeB;
	nodeA->rightChild = nodeC;

	nodeB->leftChild = nodeD;
	nodeB->rightChild = nodeE;

	nodeC->rightChild = nodeF;
	nodeC->leftChild = nodeG;
	return nodeA;
}

二叉树前序遍历

// 二叉树前序遍历
void PreOrder(BTNode* root)
{
	if (root == NULL)
	{
		printf("NULL ");
		return;
	}

	printf("%c ", root->data);
	PreOrder(root->leftChild);
	PreOrder(root->rightChild);
}

二叉树中序遍历

// 二叉树中序遍历
void InOrder(BTNode* root)
{
	if (root == NULL)
	{
		printf("NULL ");
		return;
	}
	InOrder(root->leftChild);
	printf("%c ", root->data);
	InOrder(root->rightChild);
}

二叉树后序遍历

// 二叉树后序遍历
void PostOrder(BTNode* root)
{
	if (root == NULL)
	{
		printf("NULL ");
		return;
	}
	PostOrder(root->leftChild);
	PostOrder(root->rightChild);
	printf("%c ", root->data);
}

二叉树节点个数

// 二叉树节点个数
int BinaryTreeSize(BTNode* root)
{
	//	return root == NULL ? 0 : BinaryTreeSize(root->left)+ BinaryTreeSize(root->right) + 1;
	if (root == NULL)
		return 0;
	return BinaryTreeSize(root->leftChild) + BinaryTreeSize(root->rightChild) + 1;
}

二叉树叶子节点个数

// 二叉树叶子节点个数
int BinaryTreeLeafSize(BTNode* root)
{
	if (root == NULL)
		return 0;
	if (root->leftChild == NULL && root->rightChild == NULL)
		return 1;
	return BinaryTreeLeafSize(root->leftChild) + BinaryTreeLeafSize(root->rightChild);
}

二叉树深度/高度

// 二叉树深度/高度
int BinaryTreeDepth(BTNode* root)
{
	if (root == NULL)
		return 0;
	int leftDepth = BinaryTreeDepth(root->leftChild)+ 1;
	int rightDepth = BinaryTreeDepth(root->rightChild)+ 1;
	if (leftDepth > rightDepth)
		return leftDepth;
	else
		return rightDepth;
	//int leftDepth = BinaryTreeDepth(root->left);
	//int rightDepth = BinaryTreeDepth(root->right);

	//return leftDepth > rightDepth ? leftDepth + 1 : rightDepth + 1;
}

二叉树第k层节点个数

// 二叉树第k层节点个数
int BinaryTreeLevelKSize(BTNode* root, int k)
{
	if (root == NULL||k<1)
		return 0;
	if (k == 1)
		return 1;
	return BinaryTreeLevelKSize(root->leftChild, k - 1) + BinaryTreeLevelKSize(root->rightChild, k - 1);
}

二叉树查找值为x的节点

// 二叉树查找值为x的节点
BTNode* BinaryTreeFind(BTNode* root, BTDataType x)
{
	if (root == NULL)
		return NULL;
	if (root->data == x)
		return root;
	BTNode* left = BinaryTreeFind(root->leftChild, x);
	if (left)
		return left;

	BTNode* right = BinaryTreeFind(root->rightChild, x);
	return right;
}

二叉树层次遍历

//二叉树层次遍历
void BinaryTreeLevelOrder(BTNode* root)
{
	if (root == NULL)
		return;
	Queue q;
	QueueInit(&q);
	//先入根结点
	QueuePush(&q, root);
	while (!QueueEmpty(&q))
	{
		BTNode* front = QueueFront(&q);
		printf("%c ", front->data);
		QueuePop(&q);
		if (front->leftChild != NULL)
		{
			//入队列
			QueuePush(&q, front->leftChild);
		}
		if (front->rightChild != NULL)
		{
			//入队列
			QueuePush(&q, front->rightChild);
		}
	}

	printf("\n");

	QueueDestroy(&q);
}

二叉树销毁

// 二叉树销毁
void BinaryTreeDestroy(BTNode* root)
{
	if (root == NULL)
	{
		return;
	}
	BinaryTreeDestroy(root->leftChild);	
	BinaryTreeDestroy(root->rightChild);
	free(root);
}

判断二叉树是否是完全二叉树

// 判断二叉树是否是完全二叉树
bool BinaryTreeComplete(BTNode* root)
{
	if (root == NULL)
		return true;
	Queue q;
	QueueInit(&q);
	//先入根结点
	QueuePush(&q, root);
	BTNode* front = NULL;
	//遇到空就退出
	while (front = QueueFront(&q))
	{
		QueuePop(&q);

		//不管是否为空都入队列
		QueuePush(&q, front->leftChild);
		QueuePush(&q, front->rightChild);

	}
	//检查后面的是否都为空,存在一个不为空的,则不是完全二叉树
	while (!QueueEmpty(&q))
	{
		front = QueueFront(&q);
		if (front != NULL)
		{
			QueueDestroy(&q);
			return false;
		}
		QueuePop(&q);//出队列
	}
	//没有返回,那么全为空

	QueueDestroy(&q);
	return true;
}

二叉树测试

void testTree()
{
	BTNode* root = CreatBinaryTree();
	PreOrder(root);
	printf("\n");
	InOrder(root);
	printf("\n");
	PostOrder(root);
	printf("\n");
	BinaryTreeLevelOrder(root);
	printf("\n");
	printf("BinaryTreeSize:%d\n", BinaryTreeSize(root));
	printf("BinaryTreeLeafSize:%d\n", BinaryTreeLeafSize(root));
	printf("BinaryTreeLevelKSize:%d\n", BinaryTreeLevelKSize(root,2));
	BTNode* find = BinaryTreeFind(root, 'C');
	printf("BinaryTreeFind:%c\n",find->data );
	printf("BinaryTreeDepth:%d\n", BinaryTreeDepth(root));
	BinaryTreeDestroy(root);
}
void testTree2()
{
	BTNode* root = CreatBinaryTree();

	printf("BinaryTreeComplete:%d ", BinaryTreeComplete(root));
	BinaryTreeDestroy(root);
}
int main()
{
	testTree();
	testTree2();
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

s_persist

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值