二叉树的链式结构
对于非完全二叉树和满二叉树,使用链式存储比较好,用链表来表示一棵二叉树。
typedef int BTDataType;
typedef struct BinaryTreeNode
{
BTDataType x;
// 记录左右节点
struct BinaryTreeNode* left;
struct BinaryTreeNode* right;
}BTNode;
本篇博客使用的二叉树都是这个图,可以自行修改节点连接顺序,测试函数功能是否正常。
二叉树的链式结构遍历有:前序、中序、后序的递归结构遍历。
前序遍历(Preorder Traversal 亦称先序遍历):访问根结点的操作发生在遍历其左右子树之前。前序遍历顺序:根、左子树、右子树;先访问根,再访问左子树,最后访问右子树。左子树和右子树也分为根、左子树、右子树。上图二叉树的前序遍历,#表示空树:1 2 4 # # # 3 6 # # #。
中序遍历(Inorder Traversal):访问根结点的操作发生在遍历其左右子树之间。中序遍历顺序:左子树、根、右子树。上图二叉树的中序遍历:# 4 # 2 # 1 # 6 # 3 # 。
后序遍历(Postorder Traversal)——访问根结点的操作发生在遍历其左右子树之后。后续遍历顺序:左子树、右子树、根。上图二叉树的后序遍历:# # 4 # 2 # # 6 # 3 1。
构建上图二叉树
#include <stdio.h>
typedef int BTDataType;
typedef struct BinaryTreeNode
{
BTDataType x;
struct BinaryTreeNode* left;
struct BinaryTreeNode* right;
}BTNode;
BTNode* BuyBTNode(BTDataType x)
{
BTNode* node = (BTNode*)malloc(sizeof(BTNode));
if (node == NULL)
{
printf("malloc fail\n");
exit(-1);
}
node->x = x;
node->left = node->right = NULL;
return node;
}
void test()
{
BTNode* node1 = BuyBTNode(1);
BTNode* node2 = BuyBTNode(2);
BTNode* node3 = BuyBTNode(3);
BTNode* node4 = BuyBTNode(4);
BTNode* node6 = BuyBTNode(6);
node1->left = node2;
node1->right = node3;
node2->left = node4;
node3->left = node6;
}
int main()
{
test();
return 0;
}
前序遍历
void PreOrder(BTNode* root)
{
if (root == NULL)
{
printf("# ");
return;
}
printf("%d ", root->x);
PreOrder(root->left);
PreOrder(root->right);
}
递归的代码不理解的话,建议画递归展开图理解程序。
中序遍历
void InOrder(BTNode* root)
{
if (root == NULL)
{
printf("# ");
return;
}
InOrder(root->left);
printf("%d ", root->x);
InOrder(root->right);
}
后续遍历
void PostOrder(BTNode* root)
{
if (root == NULL)
{
printf("# ");
return;
}
PostOrder(root->left);
PostOrder(root->right);
printf("%d ", root->x);
}
节点个数
int TreeSize(BTNode* root)
{
if (root == NULL)
return 0;
return 1 + TreeSize(root->left) + TreeSize(root->right);
}
叶子节点个数
int TreeLeafSize(BTNode* root)
{
if (root == NULL)
return 0;
// 叶子节点的特点
if (root->left == NULL && root->right == NULL)
return 1;
// 找左子树和右子树的叶子节点
return TreeLeafSize(root->left) + TreeLeafSize(root->right);
}
查找数据
BTNode* TreeSearch(BTNode* root, BTDataType x)
{
if (root == NULL)
return NULL;
if (root->x == x)
return root;
// 找左子树是否存在,接收返回值
BTNode* leftNode = TreeSearch(root->left, x);
// 如果不为空,证明左子树中找到了,直接返回,不再查找。
if (leftNode != NULL)
return leftNode;
// 找右子树是否存在,接收返回值
BTNode* rightNode = TreeSearch(root->right, x);
// 如果不为空,证明右子树中找到了,直接返回,不再查找。
if (rightNode != NULL)
return rightNode;
return NULL;
}
深度
// 深度
int TreeDepth(BTNode* root)
{
if (root == NULL)
return 0;
// 找左子树的深度和右子树的深度
int leftDepth = TreeDepth(root->left);
int rightDepth = TreeDepth(root->right);
// 返回深度深的,+1是返回当前节点的层次
return leftDepth > rightDepth ? leftDepth + 1 : rightDepth + 1;
}
层序遍历
使用队列辅助二叉树层序遍历。队列中先存储二叉树的根节点,取队头节点,出队列,如果该节点左孩子不为空,入队列,如果该节点右孩子不为空入队列,重复这些步骤,直到队列为空,层序遍历结束。
将之前写好的队列的源文件和头文件添加到项目中,建议复制一份到当前项目的路径,不要影响到之前的队列。在Queue.h头文件中声明二叉树的结构体,重命名队列节点的存储类型。
二叉树源文件中添加Queue.h头文件。
void test()
{
BTNode* node1 = BuyBTNode(1);
BTNode* node2 = BuyBTNode(2);
BTNode* node3 = BuyBTNode(3);
BTNode* node4 = BuyBTNode(4);
BTNode* node6 = BuyBTNode(6);
node1->left = node2;
node1->right = node3;
node2->left = node4;
node3->left = node6;
Queue q;
QueueInit(&q);
// 根不为空,入根节点
if (node1)
QueuePush(&q, node1);
// 结束条件,队列为空
while (!QueueEmpty(&q))
{
BTNode* cur = QueueFront(&q);
printf("%d ", cur->x);
QueuePop(&q);
// 左孩子不为空,入队列
if (cur->left)
QueuePush(&q, cur->left);
// 右孩子不为空,入队列
if (cur->right)
QueuePush(&q, cur->right);
}
// 销毁队列
QueueDestroy(&q);
// 销毁二叉树
TreeDestroy(node1);
}