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

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 小时前
数据结构---时空复杂度
数据结构
立志成为大牛的小牛1 小时前
数据结构——四十、折半查找(王道408)
数据结构·学习·程序人生·考研·算法
程序员东岸3 小时前
数据结构精讲:从栈的定义到链式实现,再到LeetCode实战
c语言·数据结构·leetcode
laocooon5238578863 小时前
大数的阶乘 C语言
java·数据结构·算法
WBluuue12 小时前
数据结构与算法:树上倍增与LCA
数据结构·c++·算法
lkbhua莱克瓦2413 小时前
Java基础——集合进阶用到的数据结构知识点1
java·数据结构·笔记·github
杨福瑞13 小时前
数据结构:单链表(2)
c语言·开发语言·数据结构
王璐WL14 小时前
【数据结构】单链表及单链表的实现
数据结构
z1874610300315 小时前
list(带头双向循环链表)
数据结构·c++·链表
T.Ree.16 小时前
cpp_list
开发语言·数据结构·c++·list