栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

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

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

数据结构<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 
#include 
#include 

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 failn");
		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 
#include 
#include 
#include 
#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 failn");
		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;

	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("%dn", BTreeSize(tree));
	printf("%dn", BTreeLeafSize(tree));
	printf("%dn", BTreeKLevelSize(tree, 100));
	printf("%dn", BTreeDepth(tree));

	return 0;
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/854201.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号