【数据结构】二叉树概念及代码

1.概念

2.遍历

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

中序遍历(Inorder Traversal)------访问根结点的操作发生在遍历其左右子树之中(间)。

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

3.代码

复制代码
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>

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

BTNode* BuyNode(BTDataType* x)
{
	BTNode* node = (BTNode*)malloc(sizeof(BTNode));
	if (node==NULL)
	{
		perror("malloc fail");
		return;
	}

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

	return node;
}

int TreeSize(BTNode* root)
{
	return root == NULL ? 0 :
		TreeSize(root->right) +
		TreeSize(root->left) +
		1;//加1是加自己
}

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

	int leftHeight = TreeHeight(root->left);
	int rightHeight = TreeHeight(root->right);
	return leftHeight>rightHeight ?
		leftHeight + 1 : rightHeight + 1;
}

// 计算第k层的节点个数
int TreeKLevel(BTNode* root, int k)
{
	if (root == NULL)
	{
		return 0;
	}
	if (k == 1)
	{
		return 1;
	}
	int leftK = TreeKLevel(root->left, k - 1);
	int rightK = TreeKLevel(root->right, k - 1);

	return leftK + rightK;
}

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

}

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

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

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

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

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

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

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

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


int main()
{
	BTNode* root = CreatBinaryTree();
	PreOrder(root);
	printf("\n");

	InOrder(root);
	printf("\n");

	PostOrder(root);
	printf("\n");

    printf("TreeSize: %d\n", TreeSize(root));
    printf("TreeHeight: %d\n", TreeHeight(root));
    printf("TreeKLever: %d\n", TreeKLevel(root,3));

	return 0;
}
相关推荐
DONG9132 小时前
Python 中的可迭代、迭代器与生成器——从协议到实现再到最佳实践
开发语言·汇编·数据结构·python·算法·青少年编程·排序算法
YLCHUP3 小时前
题解:P4447 [AHOI2018初中组] 分组
开发语言·数据结构·c++·经验分享·算法·贪心算法·抽象代数
John.Lewis3 小时前
C语言数据结构(7)贪吃蛇项目2.贪吃蛇项目实现
c语言·数据结构·算法
小指纹4 小时前
河南萌新联赛2025第(三)场:河南理工大学【补题】
数据结构·c++·算法·macos·ios·objective-c·cocoa
UP_Continue4 小时前
算法讲解--最大连续1的个数
数据结构·算法·leetcode
柒柒的代码学习日记8 小时前
二叉树链式结构的遍历实现
数据结构·二叉树·链式结构
码破苍穹ovo11 小时前
堆----1.数组中的第K个最大元素
java·数据结构·算法·排序算法
lifallen13 小时前
深入解析RocksDB的MVCC和LSM Tree level
大数据·数据结构·数据库·c++·lsm-tree·lsm tree
kokunka15 小时前
【数据结构】栈的顺序存储(整型栈、字符栈)
数据结构