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

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;
}
相关推荐
WolfGang0073211 天前
代码随想录算法训练营Day50 | 拓扑排序、dijkstra(朴素版)
数据结构·算法
一直都在5721 天前
数据结构入门:二叉排序树的删除算法
数据结构·算法
hweiyu001 天前
排序算法简介及分类
数据结构
oscar9991 天前
CSP-J教程——第二阶段第十二、十三课:排序与查找算法
数据结构·算法·排序算法
fei_sun1 天前
【总结】【OS】成组链接法
jvm·数据结构
月明长歌1 天前
【码道初阶】牛客TSINGK110:二叉树遍历(较难)如何根据“扩展先序遍历”构建二叉树?
java·数据结构·算法
保持低旋律节奏1 天前
数据结构——链表自实现
数据结构·链表
zmzb01031 天前
C++课后习题训练记录Day53
数据结构·c++·算法
LYFlied1 天前
【每日算法】131. 分割回文串
前端·数据结构·算法·leetcode·面试·职场和发展
夏乌_Wx1 天前
练题100天——DAY30:下一个更大的元素+键盘行
数据结构·算法