数据结构<C语言实现>二叉树链式结构

目录

1.二叉树链式结构及实现

1.1二叉树的遍历

1.1.1前序、中序以及后序遍历

1.1.2层序遍历

2.二叉树链式结构的实现


1.二叉树链式结构及实现

1.1二叉树的遍历

1.1.1前序、中序以及后序遍历

二叉树遍历(Traversal)是按照某种特定的规则,依次对二叉树中的节点进行相对应的操作,并且每个节点只操作一次。访问节点所做的操作依赖于具体的应用问题。遍历是二叉树上最重要的运算之一,也是二叉树上进行其它运算的基础。

按照规则,二叉树的遍历有:前序/中序/后序的递归结构遍历:

1.前序遍历:(Preorder Traversal亦称先序遍历)  访问根节点的操作发生在遍历其左右子树之前。

             根    左子    树右子树

2.中序遍历:(Inorder Traversal)  访问根节点的操作发生在遍历其左右子树中间。

   

         左子树    根    右子树

3.后序遍历:(Postorder Traversal)  访问根节点的操作发生在遍历其左右子树之后。

             左子树    右子树    根

由于被访问的节点必是某子树的根,所以N(Node)、L(Left subtree)和(Right subtree)又可解释为根、根的左子树和根的右子树。NLR、LNR和LRN又分别称为先根遍历、中根遍历和后根遍历。

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

typedef int BTDataType;

//二叉树结构
typedef struct BinaryTreeNode
{
	struct BinaryTreeNode* left;
	struct BinaryTreeNode* right;
	BTDataType data;
}BTNode;

//构建节点
BTNode* BuyBTNode(BTDataType x)
{
	BTNode* node = (BTNode*)malloc(sizeof(BTNode));
	if (node == NULL)
	{
		printf("malloc fail\n");
		exit(-1);
	}

	node->data = x;
	node->left = node->right = NULL;
	return node;
}

//建立二叉树
BTNode* CreatBinaryTree()

{
	BTNode* node1 = BuyBTNode(1);
	BTNode* node2 = BuyBTNode(2);
	BTNode* node3 = BuyBTNode(3);
	BTNode* node4 = BuyBTNode(4);
	BTNode* node5 = BuyBTNode(5);
	BTNode* node6 = BuyBTNode(6);

	node1->left = node2;
	node1->right = node4;
	node2->left = node3;
	node4->left = node5;
	node4->right = node6;

	return node1;
}

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

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

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

	InOrder(root->left);
	printf("%d ", root->data);
	InOrder(root->right);
}

//后序遍历
void PostOrder(BTNode* root)
{
	if (root == NULL)
	{
		printf("N ");
		return;
	}

	PostOrder(root->left);
	PostOrder(root->right);
	printf("%d ", root->data);
}

int main()
{
	BTNode* tree = CreatBinaryTree();
	PrevOrder(tree);
	printf("\n");
	InOrder(tree);
	printf("\n");
	PostOrder(tree);


	return 0;
}

1.1.2层序遍历

深度优先遍历(DFS):前序遍历、中序遍历、后序遍历

广度优先遍历(BFS):层序遍历

层序遍历:除了先序遍历、中序遍历、后序遍历外,还可以对二叉树进行层序遍历。设二叉树的根节点所在层数为1,层序遍历就是从所在二叉树的根节点出发,首先访问第一层的根节点,然后从左到右访问第二层上的节点,接着是第三层节点。以此类推,自上而下,自左至右逐层访问树的节点的过程就是层序遍历。

                                       

1.先把根入队列,借助队列先进先出的性质。

2.上一层的节点出的时候,带下一层的节点进去。

//Queue.h
//Queue.c

//层序遍历
void LevelOrder(BTNode* root)
{
    Queue q;
	QueueInit(&q);

	if (root)
	{
		QueuePush(&q, root);
	}

	while (!QueueEmpty(&q))
	{
		BTNode* front = QueueFront(&q);
		QueuePop(&q);

		printf("%d ", front->data);
		
		if (front->left)
		{
			QueuePush(&q, front->left);
		}
		
		if (front->right)
		{
			QueuePush(&q, front->right);
		}
	}
	printf("\n");

	QueueDestory;
}

2.二叉树链式结构的实现

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>
#include "Queue.h"

typedef int BTDataType;

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

//建立节点
BTNode* BuyBTNode(BTDataType x)
{
	BTNode* node = (BTNode*)malloc(sizeof(BTNode));
	if (node == NULL)
	{
		printf("malloc fail\n");
		exit(-1);
	}

	node->data = x;
	node->left = node->right = NULL;
	return node;
}

BTNode* CreatBinaryTree()
{
	BTNode* node1 = BuyBTNode(1);
	BTNode* node2 = BuyBTNode(2);
	BTNode* node3 = BuyBTNode(3);
	BTNode* node4 = BuyBTNode(4);
	BTNode* node5 = BuyBTNode(5);
	BTNode* node6 = BuyBTNode(6);

	node1->left = node2;
	node1->right = node4;
	node2->left = node3;
	node4->left = node5;
	node4->right = node6;

	return node1;
}

int BTreeDepth(BTNode* root)
{
	if (root == NULL)
		return 0;

	return BTreeDepth(root->left) > BTreeDepth(root->right) ? BTreeDepth(root->left) + 1 : BTreeDepth(root->right) + 1;
}

// 通过前序遍历的数组"ABD##E#H##CF##G##"构建二叉树
BTNode* BinaryTreeCreate(BTDataType* a, int* pi)
{
	if (a[*pi] == '#')
	{
		(*pi)++;
		return NULL;
	}

	BTNode* root = (BTNode*)malloc(sizeof(BTNode));
	root->data = a[(*pi)++];

	root->left = CreatBTree(a, pi);
	root->right = CreatBTree(a, pi);

	return root;
}

//二叉树销毁
void BTreeDestory(BTNode* root)
{
	if (root == NULL)
		return;

	BTreeDestory(root->left);
	BTreeDestory(root->right);

	free(root);
}

//二叉树节点个数
int BTreeSize(BTNode* root)
{
	if (root == NULL)
		return 0;

	return BTreeSize(root->left) + BTreeSize(root->right) + 1;
	
	//return root == NULL ? 0 : BTreeSize(root->left) + BTreeSize(root->right) + 1;
}

//二叉树叶子节点个数
int BTreeLeafSize(BTNode* root)
{
	//遍历 + 技术
	/*if (root == NULL)
		return 0;

	return BTreeLeafSize(root->left) + BTreeLeafSize(root->right) == 0 ?
		BTreeLeafSize(root->left) + BTreeLeafSize(root->right) + 1 :
		BTreeLeafSize(root->left) + BTreeLeafSize(root->right);*/

	//分治
	if (root == NULL)
		return 0;

	if (root->left == NULL && root->right == NULL)
		return 1;

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

//二叉树第k层节点个数
int BTreeKLevelSize(BTNode* root, int k)
{
	assert(k >= 1);

	if (root == NULL)
		return 0;

	if (k == 1)
		return 1;

	return BTreeKLevelSize(root->left, k - 1) + BTreeKLevelSize(root->right, k - 1);
}

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

	if (root->data == x)
		return root;

	BTNode* ret1 = BinaryTreeFind(root->left, x);
	if (ret1)
		return ret1;

	BTNode* ret2 = BinaryTreeFind(root->right, x);
	if (ret2)
		return ret2;

	return NULL;
}

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

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

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

	InOrder(root->left);
	printf("%d ", root->data);
	InOrder(root->right);
}

//后序遍历
void PostOrder(BTNode* root)
{
	if (root == NULL)
	{
		printf("N ");
		return;
	}

	PostOrder(root->left);
	PostOrder(root->right);
	printf("%d ", root->data);
}

//层序遍历
void LevelOrder(BTNode* root)
{
	Queue q;
	QueueInit(&q);

	if (root)
	{
		QueuePush(&q, root);
	}

	while (!QueueEmpty(&q))
	{
		BTNode* front = QueueFront(&q);
		QueuePop(&q);

		printf("%d ", front->data);
		
		if (front->left)
		{
			QueuePush(&q, front->left);
		}
		
		if (front->right)
		{
			QueuePush(&q, front->right);
		}
	}
	printf("\n");

	QueueDestory;
}

//判断二叉树是否是完全二叉树
int BTreeComplete(BTNode* root)
{
	Queue q;
	QueueInit(&q);

	if (root)
	{
		QueuePush(&q, root);
	}

	while (!QueueEmpty(&q))
	{
		BTNode* front = QueueFront(&q);
		QueuePop(&q);
		if (front == NULL)
			break;

		QueuePush(&q, front->left);
		QueuePush(&q, front->right);
	}
	int ret = true;
	while (!QueueEmpty(&q))
	{
		if (QueueFront(&q))
		{
			ret = false;
			break;
		}
		QueuePop(&q);
	}

	QueueDestory(&q);
	return ret;
}

int main()
{
	BTNode* tree = CreatBinaryTree();
	printf("%d\n", BTreeSize(tree));
	printf("%d\n", BTreeLeafSize(tree));
	printf("%d\n", BTreeKLevelSize(tree, 100));
	printf("%d\n", BTreeDepth(tree));

	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值