//法一
TreeNode* Creat()
{
//开辟树的空间
TreeNode* node1 = (TreeNode*)malloc(sizeof(TreeNode));
assert(node1);
TreeNode* node2 = (TreeNode*)malloc(sizeof(TreeNode));
assert(node2);
TreeNode* node3 = (TreeNode*)malloc(sizeof(TreeNode));
assert(node3);
TreeNode* node4 = (TreeNode*)malloc(sizeof(TreeNode));
assert(node4);
TreeNode* node5 = (TreeNode*)malloc(sizeof(TreeNode));
assert(node5);
TreeNode* node6 = (TreeNode*)malloc(sizeof(TreeNode));
assert(node6);
//确定指向
node1->left = node2;
node1->right = node4;
node2->left = node3;
node4->left = node5;
node4->right = node6;
//对每个进行赋值
node1->date = 1;
node2->date = 2;
node3->date = 3;
node4->date = 4;
node5->date = 5;
node6->date = 6;
return node1;
}
但是你会发现,这是一个明显可以简化的代码
简化结果如下:
//法二
TreeNode* BuyTreeNode(int x)
{
//开辟树的空间
TreeNode* node = (TreeNode*)malloc(sizeof(TreeNode));
assert(node);
node->date = x;
node->left = NULL;
node->right = NULL;
return node;
}
TreeNode* Creat()
{
//开辟树并且对每个进行赋值
TreeNode* node1 = BuyTreeNode(1);
TreeNode* node2 = BuyTreeNode(2);
TreeNode* node3 = BuyTreeNode(3);
TreeNode* node4 = BuyTreeNode(4);
TreeNode* node5 = BuyTreeNode(5);
TreeNode* node6 = BuyTreeNode(6);
//确定指向
node1->left = node2;
node1->right = node4;
node2->left = node3;
node4->left = node5;
node4->right = node6;
return node1;
}
定义:
// 二叉树前序遍历
void PrevOrder(TreeNode* root)
{
if (root == NULL)
{
printf("NULL ");
return;
}
else
{
printf("%d ", root->date);
PrevOrder(root->left);
PrevOrder(root->right);
}
}
结合前面我们建的二叉树,输出结果:
//法二
TreeNode* BuyTreeNode(int x)
{
//开辟树的空间
TreeNode* node = (TreeNode*)malloc(sizeof(TreeNode));
assert(node);
node->date = x;
node->left = NULL;
node->right = NULL;
return node;
}
TreeNode* CreateTree()
{
//开辟树并且对每个进行赋值
TreeNode* node1 = BuyTreeNode(1);
TreeNode* node2 = BuyTreeNode(2);
TreeNode* node3 = BuyTreeNode(3);
TreeNode* node4 = BuyTreeNode(4);
TreeNode* node5 = BuyTreeNode(5);
TreeNode* node6 = BuyTreeNode(6);
//确定指向
node1->left = node2;
node1->right = node4;
node2->left = node3;
node4->left = node5;
node4->right = node6;
return node1;
}
//
// 二叉树前序遍历
void PrevOrder(TreeNode* root)
{
if (root == NULL)
{
printf("NULL ");
return;
}
else
{
printf("%d ", root->date);
PrevOrder(root->left);
PrevOrder(root->right);
}
}
int main()
{
TreeNode* root = CreateTree();
PrevOrder(root);
return 0;
}
定义:
即:左子树-》树根-》右子树
同上,回到上面题目:
结果:NULL->3->NULL->2->NULL->1->NULL->5->NULL->4->NULL->6->NULL
代码如下:
// 二叉树中序遍历
void InOrder(TreeNode* root)
{
if (root == NULL)
{
printf("NULL ");
}
else
{
InOrder(root->left);
printf("%d ", root->date);
InOrder(root->right);
}
}
还是检验下:
TreeNode* BuyTreeNode(int x)
{
//开辟树的空间
TreeNode* node = (TreeNode*)malloc(sizeof(TreeNode));
assert(node);
node->date = x;
node->left = NULL;
node->right = NULL;
return node;
}
TreeNode* CreateTree()
{
//开辟树并且对每个进行赋值
TreeNode* node1 = BuyTreeNode(1);
TreeNode* node2 = BuyTreeNode(2);
TreeNode* node3 = BuyTreeNode(3);
TreeNode* node4 = BuyTreeNode(4);
TreeNode* node5 = BuyTreeNode(5);
TreeNode* node6 = BuyTreeNode(6);
//确定指向
node1->left = node2;
node1->right = node4;
node2->left = node3;
node4->left = node5;
node4->right = node6;
return node1;
}
// 二叉树中序遍历
void InOrder(TreeNode* root)
{
if (root == NULL)
{
printf("NULL ");
}
else
{
InOrder(root->left);
printf("%d ", root->date);
InOrder(root->right);
}
}
int main()
{
TreeNode* root = CreateTree();
InOrder(root);
printf("\n");
return 0;
}
定义:
后序遍历(Postorder Traversal)——访问根结点的操作发生在遍历其左右子树之后
即:左子树-》右子树-》树根
上面的树结果:NULL->NULL->3->NULL->2->NULL->NULL->5->NULL->NULL->6->4->1
代码如下:
// 二叉树后序遍历
void PostOrder(TreeNode* root)
{
if (root == NULL)
{
printf("NULL ");
}
else
{
PostOrder(root->left);
PostOrder(root->right);
printf("%d ", root->date);
}
}
验证:
TreeNode* BuyTreeNode(int x)
{
//开辟树的空间
TreeNode* node = (TreeNode*)malloc(sizeof(TreeNode));
assert(node);
node->date = x;
node->left = NULL;
node->right = NULL;
return node;
}
TreeNode* CreateTree()
{
//开辟树并且对每个进行赋值
TreeNode* node1 = BuyTreeNode(1);
TreeNode* node2 = BuyTreeNode(2);
TreeNode* node3 = BuyTreeNode(3);
TreeNode* node4 = BuyTreeNode(4);
TreeNode* node5 = BuyTreeNode(5);
TreeNode* node6 = BuyTreeNode(6);
//确定指向
node1->left = node2;
node1->right = node4;
node2->left = node3;
node4->left = node5;
node4->right = node6;
return node1;
}
// 二叉树后序遍历
void PostOrder(TreeNode* root)
{
if (root == NULL)
{
printf("NULL ");
}
else
{
PostOrder(root->left);
PostOrder(root->right);
printf("%d ", root->date);
}
}
int main()
{
TreeNode* root = CreateTree();
PostOrder(root);
printf("\n");
return 0;
}
// 二叉树节点个数
int BinaryTreeSize(TreeNode* root)
{
if (root == NULL)
{
return 0;
}
else
{
return BinaryTreeSize(root->left) + BinaryTreeSize(root->right) + 1;
}
}
有煤油恍然大悟的感觉!
检验:
int main()
{
TreeNode* root = CreateTree();
int ret = BinaryTreeSize(root);
printf("%d\n", ret);
return 0;
}
(我省略了建树的代码,需要可以去前面找,后面我都会适当省略)
结果
// 二叉树叶子节点个数
int BinaryTreeLeafSize(TreeNode* root)
{
if (root == NULL)
{
return 0;
}
else if ((root->left == NULL) && (root->right == NULL))
{
return 1;
}
return BinaryTreeLeafSize(root->left) + BinaryTreeLeafSize(root->right);
}
验证:
int main()
{
TreeNode* root = CreateTree();
int ret2=BinaryTreeLeafSize(root);
printf("%d\n", ret2);
return 0;
}
//法一
//二叉树的高度
int BinaryTreeHeight(TreeNode* root)
{
if (root == NULL)
{
return 0;
}
else
{
int left = BinaryTreeHeight(root->left);
int right = BinaryTreeHeight(root->right);
return fmax(left, right) + 1;
}
}
//法二
//二叉树的高度
int BinaryTreeHeight(TreeNode* root)
{
if (root == NULL)
{
return 0;
}
else
{
return fmax(BinaryTreeHeight(root->left), BinaryTreeHeight(root->right)) + 1;
}
}
验证:
int main()
{
TreeNode* root = CreateTree();
int ret3 = BinaryTreeHeight(root);
printf("%d\n", ret3);
return 0;
}
求第K层节点个数,这是不是也是一个递归的题目,当K==1时,是不是就是一个节点,当k>1时,是不是可以依次递减。
代码实现过程:
// 二叉树第k层节点个数
int BinaryTreeLevelKSize(TreeNode* root, int k)
{
if (root == NULL)
{
return 0;
}
if (k == 1)
{
return 1;
}
else
{
return BinaryTreeLevelKSize(root->left, k - 1) + BinaryTreeLevelKSize(root->right, k - 1);
}
}
int main()
{
TreeNode* root = CreateTree();
int ret4=BinaryTreeLevelKSize(root, 2);
printf("%d", ret4);
int ret5 = BinaryTreeLevelKSize(root, 3);
printf("%d", ret5);
return 0;
}
//法一
// 二叉树查找值为x的节点定义
TreeNode* BinaryTreeFind(TreeNode* root, BTDateType x)
{
if (root == NULL)
{
return NULL;
}
if (root->date == x)
{
return root;
}
TreeNode* ret1 = BinaryTreeFind(root->left, x);
if (ret1 != NULL)
{
return ret1;
}
TreeNode* ret2 = BinaryTreeFind(root->right, x);
if (ret2 != NULL)
{
return ret2;
}
return NULL;
}
检查:
int main()
{
TreeNode* root = CreateTree();
PrevOrder(root);
printf("\n");
TreeNode* ps = BinaryTreeFind(root, 4);
if (ps == NULL)
{
printf("没找到\n");
}
else
{
ps->date = 5;
PrevOrder(root);
printf("\n");
}
return 0;
}
如果找到的话结果4会变成5,看结果:
//法二
TreeNode* BinaryTreeFind(TreeNode* root, BTDateType x)
{
if (root == NULL)
{
return NULL;
}
if (root->date == x)
{
return root;
}
TreeNode* ret1 = BinaryTreeFind(root->left, x);
if (ret1 != NULL)
{
return ret1;
}
return BinaryTreeFind(root->right, x);;
}
// 通过前序遍历的数组"ABD##E#H##CF##G##"构建二叉树
TreeNode* BinaryTreeCreate(char* arr, int* pi)//注意:char
{
if (arr[(*pi)] == '#')
{
(*pi)++;
return NULL;
}
TreeNode* root = (TreeNode*)malloc(sizeof(TreeNode));
//判断开辟情况
if (root == NULL)
{
perror(root);
exit(-1);
}
root->date = arr[(*pi)++];
root->left = BinaryTreeCreate(arr, pi);
root->right = BinaryTreeCreate(arr, pi);
return root;
}
任何程序都要在不使用时进行销毁,所以我们下面来实现销毁接口。
// 二叉树销毁(一级指针法)//需要外面手动置空
void BinaryTreeDestory1(TreeNode* root)
{
if (root == NULL)
return;
//后序销毁法
BinaryTreeDestory1(root->left);
BinaryTreeDestory1(root->right);
free(root);
}
// 二叉树销毁(二级指针法)//不需要外面手动置空
void BinaryTreeDestory2(TreeNode** root)
{
if (root == NULL)
return;
//后序销毁法
BinaryTreeDestory2((*root)->left);
BinaryTreeDestory2((*root)->right);
free(root);
root = NULL;
}
#pragma once
#include
#include
#include
#include
//定义结构体
typedef int BTDateType;
typedef struct BinaryTreeNode
{
BTDateType date;
struct BinaryTreeNode* left;
struct BinaryTreeNode* right;
}TreeNode;
//动态开辟空间声明
TreeNode* BuyTreeNode(int x);
//建立二叉树声明
TreeNode* CreateTree();
// 二叉树前序遍历声明
void PrevOrder(TreeNode* root);
// 二叉树中序遍历声明
void InOrder(TreeNode* root);
// 二叉树后序遍历声明
void PostOrder(TreeNode* root);
// 二叉树节点个数声明
int BinaryTreeSize(TreeNode* root);
// 二叉树叶子节点个数声明
int BinaryTreeLeafSize(TreeNode* root);
//二叉树的高度声明
int BinaryTreeHeight(TreeNode* root);
// 二叉树第k层节点个数声明
int BinaryTreeLevelKSize(TreeNode* root, int k);
// 二叉树查找值为x的节点声明(前序)
TreeNode* BinaryTreeFindPreamble(TreeNode* root, BTDateType x);
//二叉树查找值为x的节点声明(中序)
TreeNode* BinaryTreeFindmedium(TreeNode* root, BTDateType x);
//二叉树查找值为x的节点声明(后序)
TreeNode* BinaryTreeFindpostorder(TreeNode* root, BTDateType x);
BinaryTreeNode.c:
#include "BinaryTreeNode.h"
//法一
//TreeNode* Creat()
//{
// //开辟树的空间
// TreeNode* node1 = (TreeNode*)malloc(sizeof(TreeNode));
// assert(node1);
// TreeNode* node2 = (TreeNode*)malloc(sizeof(TreeNode));
// assert(node2);
// TreeNode* node3 = (TreeNode*)malloc(sizeof(TreeNode));
// assert(node3);
// TreeNode* node4 = (TreeNode*)malloc(sizeof(TreeNode));
// assert(node4);
// TreeNode* node5 = (TreeNode*)malloc(sizeof(TreeNode));
// assert(node5);
// TreeNode* node6 = (TreeNode*)malloc(sizeof(TreeNode));
// assert(node6);
//
// //确定指向
// node1->left = node2;
// node1->right = node4;
// node2->left = node3;
// node4->left = node5;
// node4->right = node6;
//
// //对每个进行赋值
// node1->date = 1;
// node2->date = 2;
// node3->date = 3;
// node4->date = 4;
// node5->date = 5;
// node6->date = 6;
//}
//法二
//动态开辟空间定义
TreeNode* BuyTreeNode(int x)
{
//开辟树的空间
TreeNode* node = (TreeNode*)malloc(sizeof(TreeNode));
assert(node);
node->date = x;
node->left = NULL;
node->right = NULL;
return node;
}
//建立二叉树定义
TreeNode* CreateTree()
{
//开辟树并且对每个进行赋值
TreeNode* node1 = BuyTreeNode(1);
TreeNode* node2 = BuyTreeNode(2);
TreeNode* node3 = BuyTreeNode(3);
TreeNode* node4 = BuyTreeNode(4);
TreeNode* node5 = BuyTreeNode(5);
TreeNode* node6 = BuyTreeNode(6);
//确定指向
node1->left = node2;
node1->right = node4;
node2->left = node3;
node4->left = node5;
node4->right = node6;
return node1;
}
// 二叉树前序遍历定义
void PrevOrder(TreeNode* root)
{
if (root == NULL)
{
printf("NULL ");
return;
}
else
{
printf("%d ", root->date);
PrevOrder(root->left);
PrevOrder(root->right);
}
}
// 二叉树中序遍历定义
void InOrder(TreeNode* root)
{
if (root == NULL)
{
printf("NULL ");
}
else
{
InOrder(root->left);
printf("%d ", root->date);
InOrder(root->right);
}
}
// 二叉树后序遍历定义
void PostOrder(TreeNode* root)
{
if (root == NULL)
{
printf("NULL ");
}
else
{
PostOrder(root->left);
PostOrder(root->right);
printf("%d ", root->date);
}
}
// 层序遍历定义(一:一起遍历)
void LevelOrder(TreeNode* root)
{
;
}
// 二叉树节点个数定义
int BinaryTreeSize(TreeNode* root)
{
if (root == NULL)
{
return 0;
}
else
{
return BinaryTreeSize(root->left) + BinaryTreeSize(root->right) + 1;
}
}
// 二叉树叶子节点个数定义
int BinaryTreeLeafSize(TreeNode* root)
{
if (root == NULL)
{
return 0;
}
else if ((root->left == NULL) && (root->right == NULL))
{
return 1;
}
return BinaryTreeLeafSize(root->left) + BinaryTreeLeafSize(root->right);
}
法一
二叉树的高度定义
//int BinaryTreeHeight(TreeNode* root)
//{
// if (root == NULL)
// {
// return 0;
// }
// else
// {
// int left = BinaryTreeHeight(root->left);
// int right = BinaryTreeHeight(root->right);
// return fmax(left, right) + 1;
// }
//}
//法二
//二叉树的高度定义
int BinaryTreeHeight(TreeNode* root)
{
if (root == NULL)
{
return 0;
}
else
{
return fmax(BinaryTreeHeight(root->left), BinaryTreeHeight(root->right)) + 1;
}
}
// 二叉树第k层节点个数定义
int BinaryTreeLevelKSize(TreeNode* root, int k)
{
if (root == NULL)
{
return 0;
}
if (k == 1)
{
return 1;
}
else
{
return BinaryTreeLevelKSize(root->left, k - 1) + BinaryTreeLevelKSize(root->right, k - 1);
}
}
//法一
// 二叉树查找值为x的节点定义
//TreeNode* BinaryTreeFind(TreeNode* root, BTDateType x)
//{
// if (root == NULL)
// {
// return NULL;
// }
// if (root->date == x)
// {
// return root;
// }
// TreeNode* ret1 = BinaryTreeFind(root->left, x);
// if (ret1 != NULL)
// {
// return ret1;
// }
// TreeNode* ret2 = BinaryTreeFind(root->right, x);
// if (ret2 != NULL)
// {
// return ret2;
// }
// return NULL;
//}
//法二
TreeNode* BinaryTreeFindPreamble(TreeNode* root, BTDateType x)
{
if (root == NULL)
{
return NULL;
}
if (root->date == x)
{
return root;
}
TreeNode* ret1 = BinaryTreeFindPreamble(root->left, x);
if (ret1 != NULL)
{
return ret1;
}
return BinaryTreeFindPreamble(root->right, x);;
}
//二叉树查找值为x的节点定义(中序)
TreeNode* BinaryTreeFindmedium(TreeNode* root, BTDateType x)
{
if (root == NULL)
{
return NULL;
}
TreeNode* ret1 = BinaryTreeFindmedium(root->left, x);
if (ret1 != NULL)
{
return ret1;
}
if (root->date == x)
{
return root;
}
TreeNode* ret2 = BinaryTreeFindmedium(root->right, x);
if (ret2 != NULL)
{
return ret2;
}
return NULL;
}
//二叉树查找值为x的节点定义(后序)
TreeNode* BinaryTreeFindpostorder(TreeNode* root, BTDateType x)
{
if (root == NULL)
{
return NULL;
}
TreeNode* ret1 = BinaryTreeFindpostorder(root->left, x);
if (ret1 != NULL)
{
return ret1;
}
TreeNode* ret2 = BinaryTreeFindpostorder(root->right, x);
if (ret2 != NULL)
{
return ret2;
}
if (root->date == x)
{
return root;
}
return NULL;
}
// 通过前序遍历的数组"ABD##E#H##CF##G##"构建二叉树
TreeNode* BinaryTreeCreate(char* arr, int* pi)//注意:char
{
if (arr[(*pi)] == '#')
{
(*pi)++;
return NULL;
}
TreeNode* root = (TreeNode*)malloc(sizeof(TreeNode));
//判断开辟情况
if (root == NULL)
{
perror(root);
exit(-1);
}
root->date = arr[(*pi)++];
root->left = BinaryTreeCreate(arr, pi);
root->right = BinaryTreeCreate(arr, pi);
return root;
}
// 二叉树销毁(一级指针法)//需要外面手动置空
void BinaryTreeDestory1(TreeNode* root)
{
if (root == NULL)
return;
//后序销毁法
BinaryTreeDestory1(root->left);
BinaryTreeDestory1(root->right);
free(root);
}
// 二叉树销毁(二级指针法)//不需要外面手动置空
void BinaryTreeDestory2(TreeNode** root)
{
if (root == NULL)
return;
//后序销毁法
BinaryTreeDestory2((*root)->left);
BinaryTreeDestory2((*root)->right);
free(root);
root = NULL;
}