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

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;
}
相关推荐
papership7 小时前
【入门级-算法-6、排序算法: 计数排序】
数据结构·算法·排序算法
2401_840105208 小时前
GESP C++5级 2025年6月编程2题解:最大公因数
数据结构·c++·算法
myw07120510 小时前
Leetcode94.二叉数的中序遍历练习
c语言·数据结构·笔记·算法
songx_9910 小时前
leetcode(填充每个节点的下一个右侧节点指针 II)
java·数据结构·算法·leetcode
chenyuhao202410 小时前
vector深度求索(上)实用篇
开发语言·数据结构·c++·后端·算法·类和对象
@Zeker10 小时前
并查集(Union-Find)数据结构详解
数据结构
西望云天13 小时前
基础组合计数(三道例题)
数据结构·算法·icpc
hn小菜鸡15 小时前
LeetCode 2540.最小公共值
数据结构·算法·leetcode
Z_z在努力16 小时前
【数据结构】List 详解
数据结构·list
神龙斗士24018 小时前
Java 数组的定义与使用
java·开发语言·数据结构·算法