Archived
1
0
This commit is contained in:
2025-11-15 20:25:57 +08:00
parent 5478d0ed37
commit bb4a37acc4
99 changed files with 4315 additions and 16 deletions

159
shiyan3/shiyan3.cpp Normal file
View File

@@ -0,0 +1,159 @@
#include<stdio.h>
#include<stdlib.h>
typedef struct Tree {
char data;
struct Tree *lchild;
struct Tree *rchild;
} Tree;
// 初始化树
void initTree(Tree *T) {
T->lchild = NULL;
T->rchild = NULL;
T->data = '0';
}
// 创建树
void CreateTree(Tree* T) {
int n = 0, m = 0, i = 0;
if (T->data == '0') {
printf("请输入该子树根节点的数据:");
//getchar();
//char x;
scanf_s("%c", &T->data);
getchar();
}
printf("节点%c是否有左子树0没有1:", T->data);
scanf_s("%d", &n);
getchar();
if (n == 1) {
Tree* lchild = (Tree*)malloc(sizeof(Tree));
T->lchild = lchild;
lchild->lchild = NULL;
lchild->rchild = NULL;
lchild->data = '0';
CreateTree(T->lchild);
printf("该节点%c是否存在右子树0没有 1", T->data);
scanf_s("%d", &i);
getchar();
if (i == 1) {
Tree* rchild = (Tree*)malloc(sizeof(Tree));
T->rchild = rchild;
rchild->lchild = NULL;
rchild->rchild = NULL;
rchild->data = '0';
CreateTree(T->rchild);
}
}
else if (n == 0) {
printf("节点%c是否有右子树0没有1:", T->data);
scanf_s("%d", &m);
getchar();
if (m == 1) {
Tree* rchild = (Tree*)malloc(sizeof(Tree));
T->rchild = rchild;
rchild->lchild = NULL;
rchild->rchild = NULL;
rchild->data = '0';
CreateTree(T->rchild);
}
}
}
// 中序遍历
void LDR(Tree *T) {
if (T != NULL) {
LDR(T->lchild);
printf("%c ", T->data);
LDR(T->rchild);
}
}
// 先序遍历
void DLR(Tree *T) {
if (T != NULL) {
printf("%c ", T->data);
DLR(T->lchild);
DLR(T->rchild);
}
}
// 后序遍历
void LRD(Tree *T) {
if (T != NULL) {
LRD(T->lchild);
LRD(T->rchild);
printf("%c ", T->data);
}
}
// 计算叶子节点个数
int leaves(Tree *T) {
int count = 0;
if (T == NULL) {
return 0;
}
if (T->lchild == NULL && T->rchild == NULL) {
count ++;
} else {
return leaves(T->lchild) + leaves(T->rchild);
}
return count;
}
// 计算树的深度
int depth(Tree *T) {
int ldepth = 0, rdepth = 0;
if (T == NULL) {
return 0;
} else {
ldepth = depth(T->lchild);
rdepth = depth(T->rchild);
return (ldepth > rdepth) ? (ldepth + 1) : (rdepth + 1);
}
}
// 计算树的节点个数
int nodes(Tree *T) {
if (T == NULL) {
return 0;
} else {
return nodes(T->lchild) + nodes(T->rchild) + 1;
}
}
// 计算度为2的节点个数
int degree2nodes(Tree *T) {
int count = 0;
if (T == NULL) {
return 0;
}
if (T->lchild != NULL && T->rchild != NULL) {
count ++;
}
return count + degree2nodes(T->lchild) + degree2nodes(T->rchild);
}
int main() {
Tree bss;
initTree(&bss);
CreateTree(&bss);
printf("中序遍历结果为:\n");
LDR(&bss);
printf("\n先序遍历结果为:\n");
DLR(&bss);
printf("\n后序遍历结果为:\n");
LRD(&bss);
printf("\n叶子节点个数为:%d\n", leaves(&bss));
printf("树的深度为:%d\n", depth(&bss));
printf("树的节点个数为:%d\n", nodes(&bss));
printf("度为2的节点个数为%d\n", degree2nodes(&bss));
return 0;
}

31
shiyan3/shiyan3.sln Normal file
View File

@@ -0,0 +1,31 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.14.36623.8 d17.14
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "shiyan3", "shiyan3.vcxproj", "{EDB604AC-AC4B-44E1-9CC6-94E4D3B90D25}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{EDB604AC-AC4B-44E1-9CC6-94E4D3B90D25}.Debug|x64.ActiveCfg = Debug|x64
{EDB604AC-AC4B-44E1-9CC6-94E4D3B90D25}.Debug|x64.Build.0 = Debug|x64
{EDB604AC-AC4B-44E1-9CC6-94E4D3B90D25}.Debug|x86.ActiveCfg = Debug|Win32
{EDB604AC-AC4B-44E1-9CC6-94E4D3B90D25}.Debug|x86.Build.0 = Debug|Win32
{EDB604AC-AC4B-44E1-9CC6-94E4D3B90D25}.Release|x64.ActiveCfg = Release|x64
{EDB604AC-AC4B-44E1-9CC6-94E4D3B90D25}.Release|x64.Build.0 = Release|x64
{EDB604AC-AC4B-44E1-9CC6-94E4D3B90D25}.Release|x86.ActiveCfg = Release|Win32
{EDB604AC-AC4B-44E1-9CC6-94E4D3B90D25}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {B9410CB9-2A4A-4538-BEB4-3EE6716DD001}
EndGlobalSection
EndGlobal

131
shiyan3/shiyan3.vcxproj Normal file
View File

@@ -0,0 +1,131 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>17.0</VCProjectVersion>
<Keyword>Win32Proj</Keyword>
<ProjectGuid>{edb604ac-ac4b-44e1-9cc6-94e4d3b90d25}</ProjectGuid>
<RootNamespace>shiyan3</RootNamespace>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="Shared">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="shiyan3.cpp" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>

View File

@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="源文件">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="头文件">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd</Extensions>
</Filter>
<Filter Include="资源文件">
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="shiyan3.cpp">
<Filter>源文件</Filter>
</ClCompile>
</ItemGroup>
</Project>

159
shiyan3/作业7.cpp Normal file
View File

@@ -0,0 +1,159 @@
#include<stdio.h>
#include<stdlib.h>
typedef struct Tree {
char data;
struct Tree *lchild;
struct Tree *rchild;
} Tree;
// 初始化树
void initTree(Tree *T) {
T->lchild = NULL;
T->rchild = NULL;
T->data = '0';
}
// 创建树
void CreateTree(Tree* T) {
int n = 0, m = 0, i = 0;
if (T->data == '0') {
printf("请输入该子树根节点的数据:");
//getchar();
//char x;
scanf_s("%c", &T->data);
getchar();
}
printf("节点%c是否有左子树0没有1:", T->data);
scanf_s("%d", &n);
getchar();
if (n == 1) {
Tree* lchild = (Tree*)malloc(sizeof(Tree));
T->lchild = lchild;
lchild->lchild = NULL;
lchild->rchild = NULL;
lchild->data = '0';
CreateTree(T->lchild);
printf("该节点%c是否存在右子树0没有 1", T->data);
scanf_s("%d", &i);
getchar();
if (i == 1) {
Tree* rchild = (Tree*)malloc(sizeof(Tree));
T->rchild = rchild;
rchild->lchild = NULL;
rchild->rchild = NULL;
rchild->data = '0';
CreateTree(T->rchild);
}
}
else if (n == 0) {
printf("节点%c是否有右子树0没有1:", T->data);
scanf_s("%d", &m);
getchar();
if (m == 1) {
Tree* rchild = (Tree*)malloc(sizeof(Tree));
T->rchild = rchild;
rchild->lchild = NULL;
rchild->rchild = NULL;
rchild->data = '0';
CreateTree(T->rchild);
}
}
}
// 中序遍历
void LDR(Tree *T) {
if (T != NULL) {
LDR(T->lchild);
printf("%c ", T->data);
LDR(T->rchild);
}
}
// 先序遍历
void DLR(Tree *T) {
if (T != NULL) {
printf("%c ", T->data);
DLR(T->lchild);
DLR(T->rchild);
}
}
// 后序遍历
void LRD(Tree *T) {
if (T != NULL) {
LRD(T->lchild);
LRD(T->rchild);
printf("%c ", T->data);
}
}
// 计算叶子节点个数
int leaves(Tree *T) {
int count = 0;
if (T == NULL) {
return 0;
}
if (T->lchild == NULL && T->rchild == NULL) {
count ++;
} else {
return leaves(T->lchild) + leaves(T->rchild);
}
return count;
}
// 计算树的深度
int depth(Tree *T) {
int ldepth = 0, rdepth = 0;
if (T == NULL) {
return 0;
} else {
ldepth = depth(T->lchild);
rdepth = depth(T->rchild);
return (ldepth > rdepth) ? (ldepth + 1) : (rdepth + 1);
}
}
// 计算树的节点个数
int nodes(Tree *T) {
if (T == NULL) {
return 0;
} else {
return nodes(T->lchild) + nodes(T->rchild) + 1;
}
}
// 计算度为2的节点个数
int degree2nodes(Tree *T) {
int count = 0;
if (T == NULL) {
return 0;
}
if (T->lchild != NULL && T->rchild != NULL) {
count ++;
}
return count + degree2nodes(T->lchild) + degree2nodes(T->rchild);
}
int main() {
Tree bss;
initTree(&bss);
CreateTree(&bss);
printf("中序遍历结果为:\n");
LDR(&bss);
printf("\n先序遍历结果为:\n");
DLR(&bss);
printf("\n后序遍历结果为:\n");
LRD(&bss);
printf("\n叶子节点个数为:%d\n", leaves(&bss));
printf("树的深度为:%d\n", depth(&bss));
printf("树的节点个数为:%d\n", nodes(&bss));
printf("度为2的节点个数为%d\n", degree2nodes(&bss));
return 0;
}