idk why these stuffs get stashed for so long and I didn't ever commit them
This commit is contained in:
137
Chapter5/Huffmn/Huffmn.vcxproj
Normal file
137
Chapter5/Huffmn/Huffmn.vcxproj
Normal file
@@ -0,0 +1,137 @@
|
||||
<?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>{a456a451-7570-4d79-9836-04559d3e944b}</ProjectGuid>
|
||||
<RootNamespace>Huffmn</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>
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
<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>
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="HumanTree.cpp">
|
||||
<SDLCheck Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">false</SDLCheck>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
</ImportGroup>
|
||||
</Project>
|
||||
22
Chapter5/Huffmn/Huffmn.vcxproj.filters
Normal file
22
Chapter5/Huffmn/Huffmn.vcxproj.filters
Normal 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="HumanTree.cpp">
|
||||
<Filter>源文件</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
165
Chapter5/Huffmn/HumanTree.cpp
Normal file
165
Chapter5/Huffmn/HumanTree.cpp
Normal file
@@ -0,0 +1,165 @@
|
||||
#include"iostream"
|
||||
#include<iomanip>
|
||||
#include<string>
|
||||
using namespace std;
|
||||
|
||||
struct HTNode
|
||||
{
|
||||
int weight; // 权值,设为整型
|
||||
int parent; // 双亲位置
|
||||
int lchild; // 左孩子位置
|
||||
int rchild; // 右孩子位置
|
||||
};
|
||||
|
||||
|
||||
void select(HTNode* HT, int k, int& i1, int& i2) // 在前K-1个结点中选择权值最小的两个根结点i和j
|
||||
{
|
||||
int m1, m2;
|
||||
m1 = m2 = 32767; //
|
||||
i1 = i2 = 0;
|
||||
for (int j = 0; j < k; j++)
|
||||
{
|
||||
if (HT[j].weight < m1 && HT[j].parent == -1)
|
||||
{
|
||||
m2 = m1, i2 = i1;
|
||||
m1 = HT[j].weight;
|
||||
i1 = j;
|
||||
}
|
||||
else if (HT[j].weight < m2 && HT[j].parent == -1)
|
||||
{
|
||||
m2 = HT[j].weight;
|
||||
i2 = j;
|
||||
}
|
||||
}
|
||||
}
|
||||
// 算法5.16 // 构造哈夫曼树
|
||||
void HuffmanTree(HTNode*& HT, int* w, int n)
|
||||
{ // n是叶子结点的个数,w是叶子结点的权值数组
|
||||
HTNode* p;
|
||||
int k, i;
|
||||
int i1, i2;
|
||||
p = HT;
|
||||
for (i = 0; i < 2 * n - 1; i++)
|
||||
{ // 设置初始状态,所有结点的指针为空
|
||||
HT[i].weight = 0;
|
||||
HT[i].parent = -1;
|
||||
HT[i].lchild = -1;
|
||||
HT[i].rchild = -1;
|
||||
}
|
||||
for (i = 0; i < n; i++)
|
||||
{ // 前n个结点的权值分别为个结点的权值
|
||||
HT[i].weight = w[i];
|
||||
}
|
||||
for (k = n; k < 2 * n - 1; k++)
|
||||
{ // 构造最优二叉树
|
||||
select(HT, k, i1, i2); // 在前K-1个结点中选择权值最小的两个根结点i1和i2
|
||||
HT[i1].parent = k;
|
||||
HT[i2].parent = k;
|
||||
HT[k].weight = HT[i1].weight + HT[i2].weight;
|
||||
HT[k].lchild = i1;
|
||||
HT[k].rchild = i2;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void DispHT(HTNode* HT, int n) // 显示哈夫曼树存储
|
||||
{
|
||||
HTNode* p;
|
||||
p = HT;
|
||||
cout << "k" << setw(7) << "Weight" << setw(7) << "parent"
|
||||
<< setw(7) << "lchild" << setw(7) << "rchild" << endl;
|
||||
for (int k = 0; k < 2 * n - 1; k++)
|
||||
{
|
||||
cout << k << setw(7) << (p + k)->weight << setw(7) << (p + k)->parent
|
||||
<< setw(7) << (p + k)->lchild << setw(7) << (p + k)->rchild << endl;
|
||||
}
|
||||
}
|
||||
|
||||
//算法5.17 // 构建哈夫曼编码
|
||||
void CreateHFCode(HTNode* HT, int n, char** HC)
|
||||
{
|
||||
int i, start, c, f;
|
||||
char* cd;
|
||||
cd = new char[n];
|
||||
cd[n - 1] = '\0';
|
||||
for (i = 0; i < n; i++)
|
||||
{
|
||||
start = n - 1;
|
||||
c = i;
|
||||
f = HT[i].parent;
|
||||
while (f != -1)
|
||||
{
|
||||
if (HT[f].lchild == c)
|
||||
cd[--start] = '0';
|
||||
else
|
||||
cd[--start] = '1';
|
||||
c = f; f = HT[f].parent;
|
||||
}
|
||||
cout << endl;
|
||||
HC[i] = new char[n - start];
|
||||
HC[i] = &cd[start];
|
||||
int j = 0;
|
||||
while (HC[i][j] != '\0') // 显示编码
|
||||
{
|
||||
cout << HC[i][j];
|
||||
j++;
|
||||
}
|
||||
|
||||
}
|
||||
delete cd;
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
|
||||
int op;
|
||||
|
||||
int main()
|
||||
{
|
||||
int* w; // 权值数组
|
||||
int n; // 权值个数
|
||||
int i; // 工作变量
|
||||
HTNode* HT; // 哈夫曼树
|
||||
char** HC;
|
||||
do
|
||||
{
|
||||
cout << "1-输入结点权值" << endl;
|
||||
cout << "2-生成最优二叉树" << endl;
|
||||
cout << "3-求哈夫曼编码" << endl;
|
||||
cout << "4-退出程序" << endl;
|
||||
cout << "请选择操作(1~4):";
|
||||
cout << "\b\b";
|
||||
cin >> op;
|
||||
switch (op)
|
||||
{
|
||||
case 1:
|
||||
cout << "测试案例" << endl;
|
||||
cout << "7,5,2,3,5,6" << endl;
|
||||
cout << "请输入结点的个数:";
|
||||
cin >> n;
|
||||
w = new int[n];
|
||||
cout << "请依次输入权值:" << endl;
|
||||
for (i = 0; i < n; i++)
|
||||
{
|
||||
cout << "请输入第" << i + 1 << "个权值:";
|
||||
cin >> w[i];
|
||||
}
|
||||
break;
|
||||
case 2:
|
||||
HT = new HTNode[2 * n - 1]; // 申请最优二叉树存储空间
|
||||
HuffmanTree(HT, w, n);
|
||||
cout << "创建的哈夫曼树为:\n";
|
||||
DispHT(HT, n);
|
||||
system("pause");
|
||||
break;
|
||||
case 3:
|
||||
HC = new char* [n]; // 存储哈示曼编码
|
||||
cout << "哈夫曼编码为:\n";
|
||||
CreateHFCode(HT, n, HC);
|
||||
break;
|
||||
case 4:
|
||||
cout << "结束运行,Bye-Bye!" << endl;
|
||||
break;
|
||||
}
|
||||
} while (op != 4);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user