🖊作者 : D. Star.
📘专栏 : 数据结构
😆今日分享 : 为什么阴干的衣服会有味道?
衣服上超过三分之二的污垢来自我们身体外部的分泌物,即汗水和皮脂。如果在洗衣服的过程中皮脂没有洗干净的话,它就可以成为衣服(没有正确晒干)臭味来源的罪魁祸首。因为这些天然的油没有洗掉就会沉到布料的纤维中,并且它们非常粘稠,容易吸引污垢。这就为细菌真菌的繁殖提供了一个绝佳的“培养皿”。如果天气阴冷潮湿,无法让衣服晒干,细菌会跟衣服上残留的汗液、皮肤细胞和其他各种腺体分泌物发生反应,产生挥发性有机化合物,就会产生臭味。
文章目录
🌻头文件
#define _CRT_SECURE_NO_WARNINGS
#pragma once
#include<stdio.h>
#include<stdlib.h>
#define DataType int
typedef struct tree
{
DataType a;
struct tree* left;
struct tree* right;
}tree;
//申请一个节点
tree* BuyNode(DataType x);
//二叉树:
//前序遍历
void PreOrder(tree* t);
//中序遍历
void InOrder(tree* t);
//后序遍历
void PostOrder(tree* t);
//求二叉树的节点数1
int TreeSize1(tree* t);
//求二叉树的节点数2
int TreeSize2(tree* t);
//叶子节点数
int TreeLeafSize(tree* t);
//求二叉树的高度
int TreeHeight(tree* t);
//求k层上的节点个数 k >=1
int TreeLevelSize(tree* t,int k);
🌻功能实现(详细版)
🔎申请结点
//申请一个节点
tree* BuyNode(DataType x)
{
tree* d = (tree*)malloc(sizeof(tree));
if (d == NULL)
{
perror("malloc fail");
exit(-1);
}
d->a = x;
d->left = d->right = NULL;
return d;
}
🔎递归算法:
✔前序遍历
//二叉树的前序遍历
void PreOrder(tree* t)
{
if (t == NULL)
{
printf("# ");
return;
}
else
{
printf("%d ",t->a);
PreOrder(t->left);
PreOrder(t->right);
}
}
✔中序遍历
//中序遍历
void InOrder(tree* t)
{
if (t == NULL)
{
printf("\n");
return;
}
else
{
PreOrder(t->left);
printf("%d ", t->a);
PreOrder(t->right);
}
}
✔后序遍历
//后序遍历
void PostOrder(tree* t)
{
if (t == NULL)
{
printf("\n");
return;
}
else
{
PreOrder(t->left);
PreOrder(t->right);
printf("%d ", t->a);
}
}
🔎求二叉树的总结点数(法一/法二)
int size = 0;
//求二叉树的节点数1
int TreeSize1(tree* t)
{
if (t == NULL)
return;
size++;
TreeSize1(t->left);
TreeSize1(t->right);
return size;
}
//求二叉树的节点数2
int TreeSize2(tree* t)
{
return t == NULL ? 0 :
TreeSize2(t->left) + TreeSize2(t->right) + 1;
}
🔎二叉树的叶子节点数
//叶子节点数
int TreeLeafSize(tree* t)
{
if (t == NULL)
{
return 0;
}
if (t->left == NULL && t->right == NULL)
{
return 1;
}
return TreeLeafSize(t->left) + TreeLeafSize(t->right);
}
🔎求二叉树的高度
//求二叉树的高度
int TreeHeight(tree* t)
{
if (t == NULL)
return 0;
int leftT = TreeHeight(t->left);
int rightT = TreeHeight(t->right);
return (leftT > rightT ? leftT : rightT) + 1;
}
🔎求k层上的结点个数(k>=1)
//求k层上的节点个数 k >=1
int TreeLevelSize(tree* t, int k)
{
if (t == NULL)
return 0;
if (k == 1)
{
return 1;
}
return TreeLevelSize(t->left, k - 1) + TreeLevelSize(t->right, k - 1);
}
🌻功能实现(全部代码)
#define _CRT_SECURE_NO_WARNINGS
#include"tree.h"
//申请一个节点
tree* BuyNode(DataType x)
{
tree* d = (tree*)malloc(sizeof(tree));
if (d == NULL)
{
perror("malloc fail");
exit(-1);
}
d->a = x;
d->left = d->right = NULL;
return d;
}
//****************递归算法*******************************
//二叉树的前序遍历
void PreOrder(tree* t)
{
if (t == NULL)
{
printf("# ");
return;
}
else
{
printf("%d ",t->a);
PreOrder(t->left);
PreOrder(t->right);
}
}
//中序遍历
void InOrder(tree* t)
{
if (t == NULL)
{
printf("\n");
return;
}
else
{
PreOrder(t->left);
printf("%d ", t->a);
PreOrder(t->right);
}
}
//后序遍历
void PostOrder(tree* t)
{
if (t == NULL)
{
printf("\n");
return;
}
else
{
PreOrder(t->left);
PreOrder(t->right);
printf("%d ", t->a);
}
}
int size = 0;
//求二叉树的节点数
int TreeSize1(tree* t)
{
if (t == NULL)
return;
size++;
TreeSize1(t->left);
TreeSize1(t->right);
return size;
}
//求二叉树的节点数
int TreeSize2(tree* t)
{
return t == NULL ? 0 :
TreeSize2(t->left) + TreeSize2(t->right) + 1;
}
//叶子节点数
int TreeLeafSize(tree* t)
{
if (t == NULL)
{
return 0;
}
if (t->left == NULL && t->right == NULL)
{
return 1;
}
return TreeLeafSize(t->left) + TreeLeafSize(t->right);
}
//求二叉树的高度
int TreeHeight(tree* t)
{
if (t == NULL)
return 0;
int leftT = TreeHeight(t->left);
int rightT = TreeHeight(t->right);
return (leftT > rightT ? leftT : rightT) + 1;
}
//求k层上的节点个数 k >=1
int TreeLevelSize(tree* t, int k)
{
if (t == NULL)
return 0;
if (k == 1)
{
return 1;
}
return TreeLevelSize(t->left, k - 1) + TreeLevelSize(t->right, k - 1);
}
🌻测试代码
#define _CRT_SECURE_NO_WARNINGS
#include"tree.h"
test01()
{
tree* n1 = BuyNode(1);
tree* n2 = BuyNode(2);
tree* n3 = BuyNode(3);
tree* n4 = BuyNode(4);
tree* n5 = BuyNode(5);
tree* n6 = BuyNode(6);
n1->left = n2;
n2->left = n4;
n2->right = n5;
n1->right = n3;
n3->left = n6;
printf("二叉树的前序遍历结果为:");
PreOrder(n1);
printf("\n");
printf("结点个数为:%d\n", TreeSize1(n1));
printf("结点个数为:%d\n",TreeSize2(n1));
printf("叶子节点个数为:%d\n",TreeLeafSize(n1));
printf("二叉树的高度为:%d\n",TreeHeight(n1));
printf("第k层上的节点数为:%d\n", TreeLevelSize(n1, 2));
}
int main()
{
test01();
system("pause");
return 0;
}
感谢家人的阅读,若有不准确的地方 欢迎在评论区指正!