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

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;
}
相关推荐
仟千意1 小时前
数据结构:二叉树
数据结构·算法
代码欢乐豆4 小时前
编译原理机测客观题(7)优化和代码生成练习题
数据结构·算法·编译原理
祁同伟.4 小时前
【C++】二叉搜索树(图码详解)
开发语言·数据结构·c++·容器·stl
Joy T5 小时前
Solidity智能合约存储与数据结构精要
数据结构·区块链·密码学·智能合约·solidity·合约function
海绵宝宝的好伙伴6 小时前
【数据结构】哈希表的理论与实现
数据结构·哈希算法·散列表
Aqua Cheng.6 小时前
代码随想录第七天|哈希表part02--454.四数相加II、383. 赎金信、15. 三数之和、18. 四数之和
java·数据结构·算法·散列表
zym大哥大6 小时前
哈希表封装myunordered_map以及set
数据结构·散列表
怀揣小梦想6 小时前
跟着Carl学算法--哈希表
数据结构·c++·笔记·算法·哈希算法·散列表
Kent_J_Truman6 小时前
【模拟散列表】
数据结构·算法·蓝桥杯·散列表·常识类
努力努力再努力wz6 小时前
【C++进阶系列】:万字详解unordered_set和unordered_map,带你手搓一个哈希表!(附模拟实现unordered_set和unordered_map的源码)
java·linux·开发语言·数据结构·数据库·c++·散列表