diff --git a/.gitignore b/.gitignore
index 8ada255..fff7f73 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,5 @@
+!OrigFiles/
+
### C++ template
# Prerequisites
*.d
diff --git a/Chapter1/Chapter1.sln b/Chapter1/Chapter1.sln
index 6d89f48..fc2b98b 100644
--- a/Chapter1/Chapter1.sln
+++ b/Chapter1/Chapter1.sln
@@ -1,11 +1,9 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
-VisualStudioVersion = 17.8.34322.80
+VisualStudioVersion = 17.9.34701.34
MinimumVisualStudioVersion = 10.0.40219.1
-Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "app1-4", "app1-4\app1-4.vcxproj", "{7A2BD8EC-2595-4070-BE3A-2BF2140848B2}"
-EndProject
-Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "b", "b\b.vcxproj", "{51CE097C-C445-4A3E-A678-539C70E334DF}"
+Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Chapter1", "Chapter1.vcxproj", "{38D719C8-7D69-48A6-93DC-234C93F7B6BC}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -15,27 +13,19 @@ Global
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
- {7A2BD8EC-2595-4070-BE3A-2BF2140848B2}.Debug|x64.ActiveCfg = Debug|x64
- {7A2BD8EC-2595-4070-BE3A-2BF2140848B2}.Debug|x64.Build.0 = Debug|x64
- {7A2BD8EC-2595-4070-BE3A-2BF2140848B2}.Debug|x86.ActiveCfg = Debug|Win32
- {7A2BD8EC-2595-4070-BE3A-2BF2140848B2}.Debug|x86.Build.0 = Debug|Win32
- {7A2BD8EC-2595-4070-BE3A-2BF2140848B2}.Release|x64.ActiveCfg = Release|x64
- {7A2BD8EC-2595-4070-BE3A-2BF2140848B2}.Release|x64.Build.0 = Release|x64
- {7A2BD8EC-2595-4070-BE3A-2BF2140848B2}.Release|x86.ActiveCfg = Release|Win32
- {7A2BD8EC-2595-4070-BE3A-2BF2140848B2}.Release|x86.Build.0 = Release|Win32
- {51CE097C-C445-4A3E-A678-539C70E334DF}.Debug|x64.ActiveCfg = Debug|x64
- {51CE097C-C445-4A3E-A678-539C70E334DF}.Debug|x64.Build.0 = Debug|x64
- {51CE097C-C445-4A3E-A678-539C70E334DF}.Debug|x86.ActiveCfg = Debug|Win32
- {51CE097C-C445-4A3E-A678-539C70E334DF}.Debug|x86.Build.0 = Debug|Win32
- {51CE097C-C445-4A3E-A678-539C70E334DF}.Release|x64.ActiveCfg = Release|x64
- {51CE097C-C445-4A3E-A678-539C70E334DF}.Release|x64.Build.0 = Release|x64
- {51CE097C-C445-4A3E-A678-539C70E334DF}.Release|x86.ActiveCfg = Release|Win32
- {51CE097C-C445-4A3E-A678-539C70E334DF}.Release|x86.Build.0 = Release|Win32
+ {38D719C8-7D69-48A6-93DC-234C93F7B6BC}.Debug|x64.ActiveCfg = Debug|x64
+ {38D719C8-7D69-48A6-93DC-234C93F7B6BC}.Debug|x64.Build.0 = Debug|x64
+ {38D719C8-7D69-48A6-93DC-234C93F7B6BC}.Debug|x86.ActiveCfg = Debug|Win32
+ {38D719C8-7D69-48A6-93DC-234C93F7B6BC}.Debug|x86.Build.0 = Debug|Win32
+ {38D719C8-7D69-48A6-93DC-234C93F7B6BC}.Release|x64.ActiveCfg = Release|x64
+ {38D719C8-7D69-48A6-93DC-234C93F7B6BC}.Release|x64.Build.0 = Release|x64
+ {38D719C8-7D69-48A6-93DC-234C93F7B6BC}.Release|x86.ActiveCfg = Release|Win32
+ {38D719C8-7D69-48A6-93DC-234C93F7B6BC}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
- SolutionGuid = {2E7BE105-4A42-43C5-A5F2-51518A78298B}
+ SolutionGuid = {E30ECD0C-3E43-481E-9914-3B07AAFE7C8B}
EndGlobalSection
EndGlobal
diff --git a/Chapter1/b/b.vcxproj b/Chapter1/Chapter1.vcxproj
similarity index 97%
rename from Chapter1/b/b.vcxproj
rename to Chapter1/Chapter1.vcxproj
index ae621c9..d56d019 100644
--- a/Chapter1/b/b.vcxproj
+++ b/Chapter1/Chapter1.vcxproj
@@ -18,14 +18,11 @@
x64
-
-
-
17.0
Win32Proj
- {51ce097c-c445-4a3e-a678-539c70e334df}
- b
+ {38d719c8-7d69-48a6-93dc-234c93f7b6bc}
+ Chapter1
10.0
@@ -129,6 +126,9 @@
true
+
+
+
diff --git a/Chapter1/app1-4/app1-4.vcxproj.filters b/Chapter1/Chapter1.vcxproj.filters
similarity index 100%
rename from Chapter1/app1-4/app1-4.vcxproj.filters
rename to Chapter1/Chapter1.vcxproj.filters
diff --git a/Chapter1/app1-4.cpp b/Chapter1/app1-4.cpp
new file mode 100644
index 0000000..5524dea
--- /dev/null
+++ b/Chapter1/app1-4.cpp
@@ -0,0 +1,33 @@
+#include//cout,cin
+using namespace std;
+
+struct stu
+{
+ int xh; // 学号
+ float height; //身高
+};
+
+int MaxH; // 最高身高
+
+//算法1.1
+int maxHeight(stu student[], int n)
+{
+ MaxH = 1; // 1. 设第1个学生身高最高
+ for (int i = 2; i <= n; i++) // 2. 从第2个学生起依次与最高身比
+ {
+ if (student[i - 1].height > student[MaxH - 1].height) // 2.1 大于最高身高
+ MaxH = i; // 2.2为新最高身高
+ }
+ return student[MaxH - 1].xh; // 3.返回最高身高学生学学号
+}
+
+int main()
+{
+ cout << "获取数据" << endl;
+ int n = 6; //学生数
+ int xh;
+ stu student[6] = { {1703001,176},{1703002,180},{1703003,175},{1703004,182.5},{1703005,158.5},{1703005,173.8} };
+ xh = maxHeight(student, n);
+ cout << "身高最高者是:" << xh << endl;
+ return 1;
+}
\ No newline at end of file
diff --git a/Chapter1/app1-4/app1-4.cpp b/Chapter1/app1-4/app1-4.cpp
deleted file mode 100644
index 244bb33..0000000
--- a/Chapter1/app1-4/app1-4.cpp
+++ /dev/null
@@ -1,21 +0,0 @@
-// USE B.CPP INSTEAD
-
-#include
-using namespace std;
-
-void main()
-{
- int i=0,j=0,count=0;
- for (i=2;i<=1000;i++)
- {
- for (j=2;j<=i-1;j++)
- if(i%j==0)
- break;
- if(j>i-1)
- {
- cout<
-using namespace std;
-
-void main()
-{
- int i = 0, j = 0, count = 0;
- for (i = 2; i <= 1000; i++)
- {
- for (j = 2; j <= i - 1; j++)
- if (i % j == 0)
- break;
- if (j > i - 1)
- {
- cout << j << '\t';
- count++;
- if (count % 5 == 0) cout << endl;
- }
- }
- return;
-}
\ No newline at end of file
diff --git a/Chapter2/2-5-ReverseLinkList/2-5-ReverseLinkList.vcxproj b/Chapter2/2-5-ReverseLinkList/2-5-ReverseLinkList.vcxproj
new file mode 100644
index 0000000..202b7b5
--- /dev/null
+++ b/Chapter2/2-5-ReverseLinkList/2-5-ReverseLinkList.vcxproj
@@ -0,0 +1,138 @@
+
+
+
+
+ Debug
+ Win32
+
+
+ Release
+ Win32
+
+
+ Debug
+ x64
+
+
+ Release
+ x64
+
+
+
+ 17.0
+ Win32Proj
+ {0b0a9a4c-185a-4345-bd4e-1e48565907e2}
+ My25ReverseLinkList
+ 10.0
+
+
+
+ Application
+ true
+ v143
+ Unicode
+
+
+ Application
+ false
+ v143
+ true
+ Unicode
+
+
+ Application
+ true
+ v143
+ Unicode
+
+
+ Application
+ false
+ v143
+ true
+ Unicode
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Level3
+ true
+ WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)
+ true
+
+
+ Console
+ true
+
+
+
+
+ Level3
+ true
+ true
+ true
+ WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)
+ true
+
+
+ Console
+ true
+ true
+ true
+
+
+
+
+ Level3
+ true
+ _DEBUG;_CONSOLE;%(PreprocessorDefinitions)
+ true
+
+
+ Console
+ true
+
+
+
+
+ Level3
+ true
+ true
+ true
+ NDEBUG;_CONSOLE;%(PreprocessorDefinitions)
+ true
+
+
+ Console
+ true
+ true
+ true
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/Chapter2/2-5-ReverseLinkList/2-5-ReverseLinkList.vcxproj.filters b/Chapter2/2-5-ReverseLinkList/2-5-ReverseLinkList.vcxproj.filters
new file mode 100644
index 0000000..a5d78e2
--- /dev/null
+++ b/Chapter2/2-5-ReverseLinkList/2-5-ReverseLinkList.vcxproj.filters
@@ -0,0 +1,27 @@
+
+
+
+
+ {4FC737F1-C7A5-4376-A066-2A32D752A2FF}
+ cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx
+
+
+ {93995380-89BD-4b04-88EB-625FBE52EBFB}
+ h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd
+
+
+ {67DA6AB6-F800-4c08-8B7A-83BB121AAD01}
+ rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms
+
+
+
+
+ 源文件
+
+
+
+
+ 头文件
+
+
+
\ No newline at end of file
diff --git a/Chapter2/2-5-ReverseLinkList/LinkList.h b/Chapter2/2-5-ReverseLinkList/LinkList.h
new file mode 100644
index 0000000..6dea850
--- /dev/null
+++ b/Chapter2/2-5-ReverseLinkList/LinkList.h
@@ -0,0 +1,237 @@
+
+template
+struct LNode //链表结点
+{
+ DT data; //数据域,存储数据元素值
+ LNode* next; //指针域,指向下一个结点
+};
+
+
+//【算法2.14】 创建空单链表
+template
+bool InitList(LNode*& L)
+{
+ L = new LNode; // 1.创建头结点
+ if (!L) exit(1); // 2.创建失败,退出
+ L->next = NULL; // 3.创建成功
+ return true; // 返回true
+}
+
+//【算法2.15】 尾插法创建n的元素
+template
+bool CreateList_1(LNode*& L, int n)
+{
+ LNode* p, * s;
+ p = L; //1.工作指针初始化,指向尾结点
+ cout << "依次输入" << n << "个数据元素:" << endl;
+ for (int i = 1; i <= n; i++) // 2.按元素位序正序创建各结点
+ {
+ s = new LNode; // 2.1 新建一个结点s
+ if (!s) // 2.2 创建失败,返回false
+ return false;
+ cin >> s->data; // 2.3 输入结点值
+ s->next = p->next; // 2.4 s 链在表尾
+ p->next = s;
+ p = s; // 2.5 工作指针指向 s
+ }
+ return true; // 3.创建成功,返回true
+}
+
+//【算法2.16】 头插法创建n个元素
+template
+bool CreateList_2(LNode* (&L), int n)
+{
+ LNode* s;
+ cout << "逆序输入" << n << "个数据元素:" << endl;
+ for (int i = 1; i <= n; i++) // 1.按元素位序逆序创建各结点
+ {
+ s = new LNode; // 1.1 新建一个结点 s
+ if (!s) // 1.2 创建失败,返回false
+ return false;
+ cin >> s->data; // 1.3 输入结点值
+ s->next = L->next; // 1.4 s 在头结点后
+ L->next = s;
+ }
+ return true; // 1.创建成功,返回true
+}
+
+//【算法2.17】
+template
+void DestroyList(LNode* (&L)) // 释放链表所占空间
+{
+ LNode* p;
+ while (L) // 1. 表非空,从头结点开始,依次释放结点
+ {
+ p = L; // 1.1 处理表头结点
+ L = L->next; // 1.2 头指针后移
+ delete p; // 1.3 释放表头结点所占内存
+ }
+ L = NULL; // 2.头指针指向空
+}
+
+//【算法2.18】 获取第i个元素
+template
+bool GetElem_i(LNode* L, int i, DT& e)
+{
+ LNode* p; // 1.初始化
+ p = L->next; // 1.1 设置工作指针,从首结点开始,数结点
+ int j = 1; // 1.2 计数器初始化
+ while (p && j < i) // 2.定位到第i个元素结点
+ {
+ p = p->next; j++;
+ }
+ if (!p || j > i) // 3 未找到,返回false
+ return false;
+ else // 4. 找到
+ {
+ e = p->data; // 获取第i个元素值
+ return true; // 返回true
+ }
+}
+
+//【算法2.19】 查找值为e的元素位序
+template
+int LocateElem_e(LNode* L, DT e)
+{
+
+ LNode* p; // 1.初始化从首元开始查找
+ p = L->next; // 1.1从首元开始查找
+ int j = 1; // 1.2 计数器初值
+ while (p && p->data != e) // 2.顺序查找
+ {
+ p = p->next; // 2.1未找到指针后移
+ j++; // 2.2 计数器增1
+ }
+ if (p == NULL) // 3. 判断是否找到
+ return 0; // 3.1末找到,返回0
+ else
+ return j; // 3.2 找到,返回位序
+}
+
+//【算法2.20】 插入第i个元素
+template
+bool InsertElem_i(LNode* (&L), int i, DT e)
+{
+
+ int j = 0;
+ LNode* p; // 1.初始化
+ p = L; // 工作指针初始化
+ while (p && j < i - 1) // 2. 定位到插入点前驱
+ {
+ p = p->next;//
+ j++;
+ }
+ if (!p || j > i - 1) // 3.判断定位是否成功:
+ return false; // 3.1 定位失败,不能插入
+ else // 3.2 定位成功
+ {
+ LNode* s;
+ s = new LNode; // 3.2.1建立新结点
+ s->data = e; // 3.2.2新结点赋值
+ s->next = p->next; // 3.2.3结点S链接到p结点之后
+ p->next = s;
+ return true; // 3.2.4 插入成功,返回true
+ }
+
+}
+
+//【算法2.21】 删除第i个元素
+template
+bool DeleElem_i(LNode* (&L), int i)
+{
+
+ LNode* p, * q; //1.初始化:设置工作指针
+ p = L; //查找从头结点开始
+ int j = 0; //计数器初始化
+ while (p->next && j < i - 1) //2.p定位到删除点的前驱
+ {
+ p = p->next; //
+ j++;
+ }
+ if (!p->next || j > i - 1) //3.删除位置不合理,不能删除
+ return false; //返回false
+ else //4.删除操作
+ {
+ q = p->next; //4.1暂存删除结点位置
+ p->next = q->next; //4.2从链表中摘除删除结点
+ delete q;
+ return true; //4.3删除成功,返回true
+ }
+}
+
+
+//【算法2.22】 修改第i个元素值
+template
+bool PutElem_i(LNode* (&L), int i, DT e)
+{
+ LNode* p; // 1.初始化:设置工作指针
+ p = L->next; // 从首结点开始,数结点
+ int j = 1; // 计数器初始化
+ while (p && j < i) // 2.查找第i个元素结点
+ {
+ p = p->next; j++;
+ }
+ if (!p || j > i) // 3.元素不存在,返回false
+ return false;
+ else // 4.定位成功
+ {
+ p->data = e; // 修改元素值
+ return true; // 返回true
+ }
+}
+
+// 释放链表所占空间
+template
+void ClearList(LNode* (&L))
+{
+
+ LNode* p;
+ while (L->next) // 从首元结点开始,依次释放结点
+ {
+ p = L->next;
+ L->next = p->next;
+ delete p;
+ }
+ cout << endl << "表已清空!" << endl;
+}
+
+
+//【算法2.23】 测表长
+template
+int ListLength(LNode* L)
+{ // 1.初始化
+ int len = 0; // 1.1 结点计数器赋初值0
+ LNode* p; // 1.2设置工作指针
+ p = L; // 指向头结点
+ while (p->next) // 2.数结点个数。有后继结点,
+ {
+ len++; p = p->next; // 结点数增1,指针后移
+ }
+ return len; // 3.返回表长
+}
+
+//
+template
+bool ListEmpty(LNode* L) //测表空
+{
+ if (L->next == NULL)
+ return true; //空表,返回1
+ else
+ return false; //不空,返回0
+}
+
+
+//【算法2.24】 遍历表
+template
+void DispList(LNode* L) // 显示表内容
+{
+ LNode* p; // 1. 设置工作指针
+ p = L->next; // 从首元结点开始遍历
+ while (p) // 2.依次输出各结点值
+ {
+ cout << p->data << "\t";
+ p = p->next;
+
+ }
+ cout << endl;
+}
diff --git a/Chapter2/2-5-ReverseLinkList/ReverseLinkList.cpp b/Chapter2/2-5-ReverseLinkList/ReverseLinkList.cpp
new file mode 100644
index 0000000..976551f
--- /dev/null
+++ b/Chapter2/2-5-ReverseLinkList/ReverseLinkList.cpp
@@ -0,0 +1,82 @@
+#include //cout,cin
+using namespace std;
+#include "LinkList.h"
+
+
+//算法2.25 单链表逆置
+void ReverseLinkList(LNode*& L)
+{
+ LNode* p, * q; // 1.设置工作指针
+ p = L->next; // 原链表头结点,作为逆置后表的头结点
+ L->next = NULL;
+ while (p) // 2. 依次摘除原链表结点,以头插法插入到逆置链表中
+ {
+ q = p; // 2.1 q取当前结点位置
+ p = p->next; // 2.2 p指向下一个待处理结点
+ q->next = L->next; // 2.3 将q 插入到头结点之后
+ L->next = q;
+ }
+}
+
+
+
+void dispmenu()
+{//显示主菜单
+ cout << endl;
+ cout << "1-创建单链表\n";
+ cout << "2-逆置单链表\n";
+ cout << "3-显示单链表\n";
+ cout << "4-退出\n";
+}
+
+char pause;
+
+//主函数
+int main()
+{
+ int n;
+ //int e;
+ LNode* L;
+ system("cls"); // 执行系统命令cls,清屏
+
+ int choice;
+ do
+ {
+ dispmenu(); // 显示主菜单
+ cout << "Enter choice(1~4):";
+ cin >> choice;
+ switch (choice)
+ {
+ case 1: // 创建单链表
+ InitList(L);
+ cout << "输入要创建元素个数:";
+ cin >> n;
+ cout << endl;
+ CreateList_1(L, n);
+ cout << "创建的单链表为:";
+ DispList(L);
+ cout << endl;
+ break;
+ case 2: // 逆置单链表
+ ReverseLinkList(L);
+ cout << "逆置后的单链表为:";
+ DispList(L);
+ cout << endl;
+ break;
+ case 3: // 显示表
+ DispList(L);
+ cout << endl;
+ cin.get(pause);
+ system("pause");
+ break;
+ case 4: // 退出
+ DestroyList(L);
+ cout << "结束运行" << endl;
+ break;
+ default: // 无效选择
+ cout << "Invalid choice\n";
+ break;
+ }
+ } while (choice != 4);
+ return 0;
+}//end of main function
\ No newline at end of file
diff --git a/Chapter2/Chapter2.sln b/Chapter2/Chapter2.sln
index 2c77d0e..580c60b 100644
--- a/Chapter2/Chapter2.sln
+++ b/Chapter2/Chapter2.sln
@@ -11,6 +11,8 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "2-3-SqListApp", "2-3-SqList
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "2-4-PolyAdd", "2-4-PolyAdd\2-4-PolyAdd.vcxproj", "{3BBBC9D3-2AEA-48CC-ABF7-908DA7982A0B}"
EndProject
+Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "2-5-ReverseLinkList", "2-5-ReverseLinkList\2-5-ReverseLinkList.vcxproj", "{0B0A9A4C-185A-4345-BD4E-1E48565907E2}"
+EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
@@ -51,6 +53,14 @@ Global
{3BBBC9D3-2AEA-48CC-ABF7-908DA7982A0B}.Release|x64.Build.0 = Release|x64
{3BBBC9D3-2AEA-48CC-ABF7-908DA7982A0B}.Release|x86.ActiveCfg = Release|Win32
{3BBBC9D3-2AEA-48CC-ABF7-908DA7982A0B}.Release|x86.Build.0 = Release|Win32
+ {0B0A9A4C-185A-4345-BD4E-1E48565907E2}.Debug|x64.ActiveCfg = Debug|x64
+ {0B0A9A4C-185A-4345-BD4E-1E48565907E2}.Debug|x64.Build.0 = Debug|x64
+ {0B0A9A4C-185A-4345-BD4E-1E48565907E2}.Debug|x86.ActiveCfg = Debug|Win32
+ {0B0A9A4C-185A-4345-BD4E-1E48565907E2}.Debug|x86.Build.0 = Debug|Win32
+ {0B0A9A4C-185A-4345-BD4E-1E48565907E2}.Release|x64.ActiveCfg = Release|x64
+ {0B0A9A4C-185A-4345-BD4E-1E48565907E2}.Release|x64.Build.0 = Release|x64
+ {0B0A9A4C-185A-4345-BD4E-1E48565907E2}.Release|x86.ActiveCfg = Release|Win32
+ {0B0A9A4C-185A-4345-BD4E-1E48565907E2}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
diff --git a/Chapter3/SqQueue/SqQueue.cpp b/Chapter3/SqQueue/SqQueue.cpp
index c771ee7..039a02f 100644
--- a/Chapter3/SqQueue/SqQueue.cpp
+++ b/Chapter3/SqQueue/SqQueue.cpp
@@ -108,7 +108,7 @@ int main()
cout << endl;
break;
case 10:
- cout << "\nQ.fornt=" << Q.front << endl;
+ cout << "\nQ.front=" << Q.front << endl;
cout << "Q.rear=" << Q.rear << endl;
break;
case 0: // 退出
diff --git a/Chapter3/SqQueue/SqQueue.h b/Chapter3/SqQueue/SqQueue.h
new file mode 100644
index 0000000..4024a2a
--- /dev/null
+++ b/Chapter3/SqQueue/SqQueue.h
@@ -0,0 +1,100 @@
+template
+struct SqQueue
+{
+ DT* base; // 存储空间基地址
+ int front; // 队头指针,指向队首元素
+ int rear; // 队尾指针,指向队尾元素的后面
+ int queuesize; // 队列容量
+};
+
+template // 算法3.14 初始化队列
+bool InitQueue(SqQueue& Q, int m) // 创建容量为m的空队列
+{
+ Q.base = new DT[m]; // 1. 申请一组连续的内存空间
+ if (!Q.base) exit(1); // 申请失败,退出
+ Q.front = Q.rear = 0; // 2. 申请成功,为队列属性赋值
+ Q.queuesize = m;
+ return true; // 3. 返回true
+}
+
+template // 算法3.15 销毁队列
+void DestroyQueue(SqQueue& Q) // 销毁循环队列
+{
+ delete[] Q.base; // 1. 释放循环队列占用的内存空间
+ Q.front = Q.rear = 0; // 2. 为队列属性赋值
+ Q.queuesize = 0;
+}
+
+template // 算法3.16 入队
+bool EnQueue(SqQueue& Q, DT e) // 在队尾插入一个新元素
+{
+ if ((Q.rear + 1) % Q.queuesize == Q.front) // 1. 队满的情况
+ return false; // 无法入队,返回false
+ Q.base[Q.rear] = e; // 2. 元素e放在队尾指针处
+ Q.rear = (Q.rear + 1) % Q.queuesize; // 队尾指针增1
+ return true; // 3. 返回true
+}
+
+template // 算法3.17 出队
+bool DeQueue(SqQueue& Q, DT& e) // 删除队头元素
+{
+ if (Q.front == Q.rear) // 1. 队空的情况
+ return false; // 无法出队,返回false
+ e = Q.base[Q.front]; // 2. 取队头元素,赋值给e
+ Q.front = (Q.front + 1) % Q.queuesize; // 队头指针加1
+ return true; // 3. 返回true
+}
+
+template // 算法3.18 取队头元素
+bool GetHead(SqQueue Q, DT& e) // 取队头元素
+{
+ if (Q.front == Q.rear) // 1. 队空的情况
+ return false; // 无队头元素,返回false
+ e = Q.base[Q.front]; // 2. 取队头元素,赋值给e
+ return true; // 3. 返回true
+}
+
+template
+bool GetTail(SqQueue Q, DT& e) // 取队尾元素
+{
+ if (Q.front == Q.rear) // 1. 队空的情况
+ return false; // 无队尾元素,返回false
+ e = Q.base[(Q.rear - 1 + Q.queuesize) % Q.queuesize]; // 2. 取队尾元素,赋值给e
+ return true; // 3. 返回true
+}
+
+template
+int QueueLength(SqQueue Q) // 测表长
+{
+ return (Q.rear - Q.front + Q.queuesize) % Q.queuesize;
+}
+
+template
+bool QueueEmpty(SqQueue Q) // 判队空
+{
+ return Q.front == Q.rear;
+}
+
+template
+bool QueueFull(SqQueue Q) // 判队满
+{
+ return (Q.rear + 1) % Q.queuesize == Q.front;
+}
+
+template
+void ClearQueue(SqQueue& Q) // 清空队
+{
+ Q.front = Q.rear = 0;
+}
+
+template
+void DispQueue(SqQueue Q) // 显示队
+{
+ int i = Q.front;
+ while (i != Q.rear)
+ {
+ cout << Q.base[i] << " ";
+ i = (i + 1) % Q.queuesize;
+ }
+ cout << endl;
+}
\ No newline at end of file
diff --git a/Chapter3/SqQueue/SqQueue.vcxproj b/Chapter3/SqQueue/SqQueue.vcxproj
index ed6ea7b..3c08411 100644
--- a/Chapter3/SqQueue/SqQueue.vcxproj
+++ b/Chapter3/SqQueue/SqQueue.vcxproj
@@ -129,6 +129,9 @@
+
+
+
diff --git a/Chapter3/SqQueue/SqQueue.vcxproj.filters b/Chapter3/SqQueue/SqQueue.vcxproj.filters
index fba4700..423f530 100644
--- a/Chapter3/SqQueue/SqQueue.vcxproj.filters
+++ b/Chapter3/SqQueue/SqQueue.vcxproj.filters
@@ -19,4 +19,9 @@
源文件
+
+
+ 头文件
+
+
\ No newline at end of file
diff --git a/Chapter4/AddMatrix/AddMatrix.cpp b/Chapter4/AddMatrix/AddMatrix.cpp
new file mode 100644
index 0000000..31a7c3e
--- /dev/null
+++ b/Chapter4/AddMatrix/AddMatrix.cpp
@@ -0,0 +1,203 @@
+#include//cout,cin
+#include"process.h"//exit()
+#include"stdio.h"//EOF,NULL
+using namespace std;
+
+struct MTNode // 三元组
+{
+ int i, j; // 行号,列号
+ int e; // 非零元
+ MTNode* next; // 指向同行下一个结点
+};
+
+typedef struct
+{
+ int mu, nu, tu; // 行数、列数、非零元个数
+ MTNode** rops; // 存放各行链表的头指针
+}LMatrix;
+
+int cmp(int a, int b) // 列号比较
+{
+ if (a < b) return -1;
+ else if (a == b) return 0;
+ else return 1;
+}
+
+void NodeCopy(MTNode*& s, MTNode* x) // 结点拷贝
+{
+ s->e = x->e; s->i = x->i; s->j = x->j;
+}
+
+void AddNode(MTNode*& lp, MTNode*& lq, MTNode* s) // 表尾添加结点
+{
+ MTNode* p;
+ p = new MTNode;
+ NodeCopy(p, s);
+ p->next = NULL;
+ if (lp == NULL) // 首元结点
+ {
+ lp = p;
+ lq = p;
+ }
+ else // 非首元结点
+ {
+ lq->next = p;
+ lq = p;
+ }
+}//
+
+
+LMatrix MatrixAdd(LMatrix ma, LMatrix mb) // 求矩阵和
+{
+ LMatrix mc;
+ MTNode* pa, * pb, * pc; // 分别指向被加数、加数、和矩阵行向量首元结点
+ MTNode* s;//
+ int i, sum;
+ int m, n; // 行数,列数
+ int flag = 1;
+ m = ma.mu;
+ n = ma.nu;
+ mc.mu = m; mc.nu = n; mc.tu = 0; mc.rops = NULL;
+ if (mc.rops) delete[] mc.rops;
+ mc.rops = new MTNode * [m];
+ for (i = 0; i < m; i++)
+ mc.rops[i] = NULL; // C行指针向量初始化
+ for (i = 0; i < m; i++)
+ {
+ pa = ma.rops[i];
+ pb = mb.rops[i];
+ pc = mc.rops[i];
+ while (pa && pb) // 被加矩阵、加矩阵行链不空
+ {
+ flag = 1;
+ if (pa->j < pb->j)
+ {
+ s = new MTNode;//
+ NodeCopy(s, pa);
+ s->next = NULL;
+ pa = pa->next;
+ }
+ else if (pa->j == pb->j)
+ {
+ sum = pa->e + pb->e;
+ if (sum == 0) flag = 0;
+ else
+ {
+ s = new MTNode;
+ NodeCopy(s, pa);
+ s->e = sum;
+ s->next = NULL;
+ }
+ pa = pa->next; pb = pb->next;//pa,pb后移
+ }
+ else
+ {
+ s = new MTNode;
+ NodeCopy(s, pb); // 复制pb所指结点
+ pb = pb->next; // pb后移
+ s->next = NULL;
+ }
+ if (flag) // 有新结点生成
+ {
+ mc.tu++;
+ AddNode(mc.rops[i], pc, s);
+ }
+ }//while
+ if (pa) // pa不空,复制pa剩余链到和矩阵中
+ {
+ while (pa)
+ {
+ s = new MTNode;
+ NodeCopy(s, pa); pa = pa->next;
+ AddNode(mc.rops[i], pc, s);
+ }//while
+ }//if(pa)
+ if (pb) // pb不空,复制pb剩余链到和矩阵中
+ {
+ while (pb)
+ {
+ s = new MTNode;
+ NodeCopy(s, pb); pb = pb->next;
+ AddNode(mc.rops[i], pc, s);
+ }//while
+ }//if(pb)
+ }//for
+ return mc;
+}//MAdd
+
+void MDisp(LMatrix a)
+{
+ MTNode* p;
+ int i, j, c = 0;
+ for (i = 0; i < a.mu; i++)
+ {
+ p = a.rops[i];
+ for (j = 0; j < a.nu; j++)
+ {
+ if (p == NULL)
+ cout << '\t' << c;
+ else if (j < p->j)
+ cout << '\t' << c;
+ else
+ {
+ cout << '\t' << p->e;
+ p = p->next;
+ }
+ }//for
+ cout << endl;
+ }//for
+}//MatrixDisp
+
+LMatrix MCreate(int d[][3], int m, int n, int k)
+{ // 由三元组的二维数组生成行向量稀疏存储矩阵
+ LMatrix M = { m,n,k,NULL };
+ int i, r1, r2;
+ MTNode* s, * p; // 工作指针
+ if (M.rops) delete[] M.rops;
+ M.rops = new MTNode * [m];
+ for (i = 0; i < m; i++)
+ M.rops[i] = NULL;
+ r1 = m;
+ p = M.rops[r1];//
+ for (i = 0; i < k; i++) // 扫描非零元数组
+ {
+ s = new MTNode;
+ s->i = d[i][0];
+ s->j = d[i][1];
+ s->e = d[i][2];
+ r2 = s->i; // 非零元所在行
+ if (r2 != r1) // 创建链表第1个结点
+ {
+ M.rops[r2] = s;
+ s->next = NULL;
+ p = s;
+ r1 = r2;
+ }
+ else // 创建链表非首元结点
+ {
+ s->next = p->next;
+ p->next = s;
+ p = s;
+ }
+ }//for
+ return M;
+}//MCreate
+
+
+void main()
+{
+ //MTNode *p;
+ LMatrix ma, mb, mc;
+ int m = 4, n = 6; // 行数,列数
+ int da[5][3] = { {0,1,3},{1,1,2},{1,3,5},{3,0,9},{3,5,1} };
+ int db[4][3] = { {0,2,7},{1,1,6},{1,3,-5},{2,1,4} };
+ ma = MCreate(da, 4, 6, 5); // 构造ma矩阵
+ cout << "ma=" << endl;
+ MDisp(ma); // 显示ma矩阵
+ mb = MCreate(db, 4, 6, 4); // 构造mb矩阵
+ cout << "mb=" << endl;
+ MDisp(mb); // 显示mb矩阵
+ mc = MatrixAdd(ma, mb); // 求mc=ma+mb
+ cout << "mc=ma+mb=" << endl;
+ MDisp(mc); // 输出和矩阵
+}
\ No newline at end of file
diff --git a/Chapter4/AddMatrix/AddMatrix.vcxproj b/Chapter4/AddMatrix/AddMatrix.vcxproj
new file mode 100644
index 0000000..bae4e33
--- /dev/null
+++ b/Chapter4/AddMatrix/AddMatrix.vcxproj
@@ -0,0 +1,135 @@
+
+
+
+
+ Debug
+ Win32
+
+
+ Release
+ Win32
+
+
+ Debug
+ x64
+
+
+ Release
+ x64
+
+
+
+ 17.0
+ Win32Proj
+ {01a69829-c675-4cad-99d2-35be3dc4ed2c}
+ AddMatrix
+ 10.0
+
+
+
+ Application
+ true
+ v143
+ Unicode
+
+
+ Application
+ false
+ v143
+ true
+ Unicode
+
+
+ Application
+ true
+ v143
+ Unicode
+
+
+ Application
+ false
+ v143
+ true
+ Unicode
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Level3
+ true
+ WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)
+ true
+
+
+ Console
+ true
+
+
+
+
+ Level3
+ true
+ true
+ true
+ WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)
+ true
+
+
+ Console
+ true
+ true
+ true
+
+
+
+
+ Level3
+ false
+ _DEBUG;_CONSOLE;%(PreprocessorDefinitions)
+ true
+
+
+ Console
+ true
+
+
+
+
+ Level3
+ true
+ true
+ true
+ NDEBUG;_CONSOLE;%(PreprocessorDefinitions)
+ true
+
+
+ Console
+ true
+ true
+ true
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/Chapter1/b/b.vcxproj.filters b/Chapter4/AddMatrix/AddMatrix.vcxproj.filters
similarity index 95%
rename from Chapter1/b/b.vcxproj.filters
rename to Chapter4/AddMatrix/AddMatrix.vcxproj.filters
index a43892b..e873347 100644
--- a/Chapter1/b/b.vcxproj.filters
+++ b/Chapter4/AddMatrix/AddMatrix.vcxproj.filters
@@ -15,7 +15,7 @@
-
+
源文件
diff --git a/Chapter4/Chapter4.sln b/Chapter4/Chapter4.sln
new file mode 100644
index 0000000..4a8b82a
--- /dev/null
+++ b/Chapter4/Chapter4.sln
@@ -0,0 +1,41 @@
+
+Microsoft Visual Studio Solution File, Format Version 12.00
+# Visual Studio Version 17
+VisualStudioVersion = 17.9.34701.34
+MinimumVisualStudioVersion = 10.0.40219.1
+Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "AddMatrix", "AddMatrix\AddMatrix.vcxproj", "{01A69829-C675-4CAD-99D2-35BE3DC4ED2C}"
+EndProject
+Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "MatrixTrans", "MatrixTrans\MatrixTrans.vcxproj", "{AE81732D-D011-4A36-9D89-0C258ADB5627}"
+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
+ {01A69829-C675-4CAD-99D2-35BE3DC4ED2C}.Debug|x64.ActiveCfg = Debug|x64
+ {01A69829-C675-4CAD-99D2-35BE3DC4ED2C}.Debug|x64.Build.0 = Debug|x64
+ {01A69829-C675-4CAD-99D2-35BE3DC4ED2C}.Debug|x86.ActiveCfg = Debug|Win32
+ {01A69829-C675-4CAD-99D2-35BE3DC4ED2C}.Debug|x86.Build.0 = Debug|Win32
+ {01A69829-C675-4CAD-99D2-35BE3DC4ED2C}.Release|x64.ActiveCfg = Release|x64
+ {01A69829-C675-4CAD-99D2-35BE3DC4ED2C}.Release|x64.Build.0 = Release|x64
+ {01A69829-C675-4CAD-99D2-35BE3DC4ED2C}.Release|x86.ActiveCfg = Release|Win32
+ {01A69829-C675-4CAD-99D2-35BE3DC4ED2C}.Release|x86.Build.0 = Release|Win32
+ {AE81732D-D011-4A36-9D89-0C258ADB5627}.Debug|x64.ActiveCfg = Debug|x64
+ {AE81732D-D011-4A36-9D89-0C258ADB5627}.Debug|x64.Build.0 = Debug|x64
+ {AE81732D-D011-4A36-9D89-0C258ADB5627}.Debug|x86.ActiveCfg = Debug|Win32
+ {AE81732D-D011-4A36-9D89-0C258ADB5627}.Debug|x86.Build.0 = Debug|Win32
+ {AE81732D-D011-4A36-9D89-0C258ADB5627}.Release|x64.ActiveCfg = Release|x64
+ {AE81732D-D011-4A36-9D89-0C258ADB5627}.Release|x64.Build.0 = Release|x64
+ {AE81732D-D011-4A36-9D89-0C258ADB5627}.Release|x86.ActiveCfg = Release|Win32
+ {AE81732D-D011-4A36-9D89-0C258ADB5627}.Release|x86.Build.0 = Release|Win32
+ EndGlobalSection
+ GlobalSection(SolutionProperties) = preSolution
+ HideSolutionNode = FALSE
+ EndGlobalSection
+ GlobalSection(ExtensibilityGlobals) = postSolution
+ SolutionGuid = {32AA252A-8427-41AB-8DDD-F9DC7EE96753}
+ EndGlobalSection
+EndGlobal
diff --git a/Chapter4/MatrixTrans/MatrixTrans.cpp b/Chapter4/MatrixTrans/MatrixTrans.cpp
new file mode 100644
index 0000000..ecd08d9
--- /dev/null
+++ b/Chapter4/MatrixTrans/MatrixTrans.cpp
@@ -0,0 +1,130 @@
+#include//cout,cin
+using namespace std;
+
+
+struct MNode //三元组结点
+{
+ int i, j; //行号,列号
+ int e; //非零元
+};
+
+
+struct TSMatrix
+{
+ int mu, nu, tu; // 行数、列数、非零元个数
+ MNode* data; // 三元组表
+};
+
+TSMatrix MCreate(int d[][3], int m, int n, int k) //由三元组的二维数组生成行向量稀疏存储矩阵
+{
+ TSMatrix M = { m,n,k,NULL };
+ if (k != 0)
+ M.data = new MNode[k];
+ for (int i = 0; i < k; i++)
+ {
+ M.data[i].i = d[i][0];
+ M.data[i].j = d[i][1];
+ M.data[i].e = d[i][2];
+ }
+ return M;
+}//
+
+
+void MDisp(TSMatrix a) //矩阵显示
+{
+ MNode p;
+ int i, j, k = 0, c = 0;
+ p = a.data[k];
+ for (i = 0; i < a.mu; i++)
+ {
+ for (j = 0; j < a.nu; j++)
+ {
+ if (k < a.tu && p.i == i && p.j == j)
+ {
+ cout << '\t' << p.e;
+ k++;
+ if (k < a.tu) p = a.data[k];
+ }
+ else
+ {
+ cout << '\t' << c;
+ }
+ }//for
+ cout << endl;
+ cout << endl;
+ }//for
+}//MatrixDisp
+
+//算法4.1 转置算法一
+void MatrixTrans_1(TSMatrix A, TSMatrix& B)
+{//
+ int col, p, q;
+ B.mu = A.nu; B.nu = A.mu; B.tu = A.tu; //B的行数、列数、非零元个数分别等于A的列数、行数和非零元个数
+ if (B.tu) // 矩阵B有非零元
+ {
+ q = 0;
+ for (col = 0; col < A.nu; ++col) // 按列扫描A矩阵
+ for (p = 0; p < A.tu; ++p)
+ if (A.data[p].j == col) // col列有非零元
+ {
+ B.data[q].i = A.data[p].j; // 交换行、列号,生成转置元素
+ B.data[q].j = A.data[p].i;
+ B.data[q].e = A.data[p].e;
+ ++q;
+ }
+ }
+}
+
+
+
+//算法4.2 快速转置
+void MatrixTrans_2(TSMatrix A, TSMatrix& B)
+{
+ int col, i, k, q;
+ int* num, * cpot;
+ B.mu = A.nu; B.nu = A.mu; B.tu = A.tu; //B的行数、列数、非零元个数分别等于A的列数、行数和非零元个数
+ num = new int[B.nu];
+ cpot = new int[B.nu];
+ if (B.tu) // 非零元个数不为零,实施转置
+ {
+ for (col = 0; col < A.nu; col++) //A中每一列非零元个数初始化为0
+ num[col] = 0;
+ for (i = 0; i < A.tu; i++) //求矩阵A中每一列非零元个数
+ num[A.data[i].j]++;
+ cpot[0] = 0; //A中第0列首个非零元在B中的下标
+ for (col = 1; col <= A.nu; ++col) // 求A中每一列首个非零元在B中的下标
+ cpot[col] = cpot[col - 1] + num[col - 1];
+ for (k = 0; k < A.tu; ++k) //扫描A的三元组表
+ {
+ col = A.data[k].j; //当前三元组列号
+ q = cpot[col]; //当前三元组在B中的下标
+ B.data[q].i = A.data[k].j;
+ B.data[q].j = A.data[k].i;
+ B.data[q].e = A.data[k].e;
+ cpot[col]++; // 预置同一列下一个三元组在B中的下标
+ }//for
+ }//if
+}//
+
+
+
+void main()
+{
+ TSMatrix ma, mb1, mb2;
+ int m1 = 4, n1 = 6, k1 = 6; //被乘矩阵行数,列数,非零元个数
+ int da[6][3] = { {0,2,11},{0,4,12},{1,3,22},
+ {2,1,31}, {2,3,32},{3,0,41} }; //ma阵的非零元
+ ma = MCreate(da, m1, n1, k1); //生成三元组顺序存储的ma阵
+ cout << "ma=" << endl;
+ MDisp(ma); //显示ma阵
+ mb1.data = new MNode[ma.tu];
+ mb2.data = new MNode[ma.tu];
+ cout << "方法一:直接取,顺序存" << endl; //用方法一转置矩阵
+ MatrixTrans_1(ma, mb1);
+ cout << "mb=" << endl; //显示转置矩阵
+ MDisp(mb1);
+ cout << "方法二:顺序取,直接存" << endl; //用方法二转置矩阵
+ MatrixTrans_2(ma, mb2);
+ cout << "mb=" << endl; //显示转置矩阵
+ MDisp(mb2);
+}//
\ No newline at end of file
diff --git a/Chapter4/MatrixTrans/MatrixTrans.vcxproj b/Chapter4/MatrixTrans/MatrixTrans.vcxproj
new file mode 100644
index 0000000..2dab3a5
--- /dev/null
+++ b/Chapter4/MatrixTrans/MatrixTrans.vcxproj
@@ -0,0 +1,135 @@
+
+
+
+
+ Debug
+ Win32
+
+
+ Release
+ Win32
+
+
+ Debug
+ x64
+
+
+ Release
+ x64
+
+
+
+ 17.0
+ Win32Proj
+ {ae81732d-d011-4a36-9d89-0c258adb5627}
+ MatrixTrans
+ 10.0
+
+
+
+ Application
+ true
+ v143
+ Unicode
+
+
+ Application
+ false
+ v143
+ true
+ Unicode
+
+
+ Application
+ true
+ v143
+ Unicode
+
+
+ Application
+ false
+ v143
+ true
+ Unicode
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Level3
+ true
+ WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)
+ true
+
+
+ Console
+ true
+
+
+
+
+ Level3
+ true
+ true
+ true
+ WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)
+ true
+
+
+ Console
+ true
+ true
+ true
+
+
+
+
+ Level3
+ true
+ _DEBUG;_CONSOLE;%(PreprocessorDefinitions)
+ true
+
+
+ Console
+ true
+
+
+
+
+ Level3
+ true
+ true
+ true
+ NDEBUG;_CONSOLE;%(PreprocessorDefinitions)
+ true
+
+
+ Console
+ true
+ true
+ true
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/Chapter4/MatrixTrans/MatrixTrans.vcxproj.filters b/Chapter4/MatrixTrans/MatrixTrans.vcxproj.filters
new file mode 100644
index 0000000..334c1b3
--- /dev/null
+++ b/Chapter4/MatrixTrans/MatrixTrans.vcxproj.filters
@@ -0,0 +1,22 @@
+
+
+
+
+ {4FC737F1-C7A5-4376-A066-2A32D752A2FF}
+ cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx
+
+
+ {93995380-89BD-4b04-88EB-625FBE52EBFB}
+ h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd
+
+
+ {67DA6AB6-F800-4c08-8B7A-83BB121AAD01}
+ rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms
+
+
+
+
+ 源文件
+
+
+
\ No newline at end of file
diff --git a/Chapter5/BiTree/BiTree.vcxproj b/Chapter5/BiTree/BiTree.vcxproj
new file mode 100644
index 0000000..2c4c165
--- /dev/null
+++ b/Chapter5/BiTree/BiTree.vcxproj
@@ -0,0 +1,138 @@
+
+
+
+
+ Debug
+ Win32
+
+
+ Release
+ Win32
+
+
+ Debug
+ x64
+
+
+ Release
+ x64
+
+
+
+ 17.0
+ Win32Proj
+ {904cfcdf-da23-415a-8d68-8e51ad913458}
+ BiTree
+ 10.0
+
+
+
+ Application
+ true
+ v143
+ Unicode
+
+
+ Application
+ false
+ v143
+ true
+ Unicode
+
+
+ Application
+ true
+ v143
+ Unicode
+
+
+ Application
+ false
+ v143
+ true
+ Unicode
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Level3
+ true
+ WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)
+ true
+
+
+ Console
+ true
+
+
+
+
+ Level3
+ true
+ true
+ true
+ WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)
+ true
+
+
+ Console
+ true
+ true
+ true
+
+
+
+
+ Level3
+ false
+ _DEBUG;_CONSOLE;%(PreprocessorDefinitions)
+ true
+
+
+ Console
+ true
+
+
+
+
+ Level3
+ true
+ true
+ true
+ NDEBUG;_CONSOLE;%(PreprocessorDefinitions)
+ true
+
+
+ Console
+ true
+ true
+ true
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/Chapter5/BiTree/BiTree.vcxproj.filters b/Chapter5/BiTree/BiTree.vcxproj.filters
new file mode 100644
index 0000000..81b66a3
--- /dev/null
+++ b/Chapter5/BiTree/BiTree.vcxproj.filters
@@ -0,0 +1,27 @@
+
+
+
+
+ {4FC737F1-C7A5-4376-A066-2A32D752A2FF}
+ cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx
+
+
+ {93995380-89BD-4b04-88EB-625FBE52EBFB}
+ h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd
+
+
+ {67DA6AB6-F800-4c08-8B7A-83BB121AAD01}
+ rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms
+
+
+
+
+ 头文件
+
+
+
+
+ 源文件
+
+
+
\ No newline at end of file
diff --git a/Chapter5/BiTree/bitree.cpp b/Chapter5/BiTree/bitree.cpp
new file mode 100644
index 0000000..c89a44a
--- /dev/null
+++ b/Chapter5/BiTree/bitree.cpp
@@ -0,0 +1,129 @@
+#include//cout,cin
+#include
+using namespace std;
+#include"bitree.h"
+
+//测试参考数据
+string fbt = "a b d # # e # # c f # # g # #"; // 满二叉树最后一层每个叶结点后有两个#
+string cbt = "a b d # # e # # c # #"; // 完全二叉树每个叶结点后有两个#
+string gbt = "a b # d # # c e # # #"; // 一般二叉树
+string obt = "a b c d # # # # #"; // 左斜树1
+
+
+void dispmenu()
+{ //显示主菜单
+ cout << endl;
+ cout << "1-创建二叉树\n";
+ cout << "2-先序递归遍历二叉树\n";
+ cout << "3-中序递归遍历二叉树\n";
+ cout << "4-后序递归遍历二叉树\n";
+ cout << "5-层序遍历二叉树\n";
+ cout << "6-先序非递归遍历二叉树\n";
+ cout << "7-中序非递归遍历二叉树\n";
+ cout << "8-后序非递归遍历二叉树\n";
+ cout << "9-结点查询\n";
+ cout << "10-求二叉树高度\n";
+ cout << "11-求二叉树结点个数\n";
+ cout << "12-显示二叉树\n";
+ cout << "0 -退出\n";
+}
+
+int main()
+{
+ int level;
+ BTNode* bt;
+
+ system("cls"); // 清屏
+
+ int choice;
+ do
+ {
+ dispmenu(); // 显示主菜单
+ cout << "Enter choice(1~12,0 退出):";
+ cin >> choice;
+ switch (choice)
+ {
+ case 1: // 创建二叉树
+ cout << "测试参考数据:" << endl;
+ cout << "满二叉树:" << fbt << endl;
+ cout << "完全二叉树:" << cbt << endl;
+ cout << "一般二叉树:" << gbt << endl;
+ cout << "左斜二叉树:" << obt << endl;
+ cout << "请按先序序列的顺序输入二叉树,#为空指针域标志:" << endl;
+ CreateBiTree(bt);
+ break;
+ case 2: // 先序递归遍历二叉树
+ cout << "先序遍历序列为:" << endl;
+ PreOrDerBiTree(bt);
+ break;
+ case 3: // 中序递归遍历二叉树
+ cout << "中序遍历序列为:" << endl;
+ InOrDerBiTree(bt);
+ cout << endl;
+ break;
+ case 4: // 后序递归遍历二叉树
+ cout << "后序遍历序列为:" << endl;
+ PostOrDerBiTree(bt);
+ cout << endl;
+ break;
+ case 5: // 层序遍历二叉树
+ cout << "层序遍历序列为:" << endl;
+ cout << endl;
+ LevelBiTree(bt);
+ break;
+ case 6: // 先序非递归遍历二叉树
+ cout << "先序非递归遍历序列为:" << endl;
+ PreOrderBiTree_N(bt);
+ cout << endl;
+ break;
+ case 7: // 中序非递归遍历二叉树
+ cout << "中序非递归遍历序列为:" << endl;
+ InOrderBiTree_N(bt);
+ cout << endl;
+ break;
+ case 8: // 后序非递归遍历二叉树
+ cout << "后序非递归遍历序列为:" << endl;
+ cout << endl;
+ PostOrderBiTree_N(bt);
+ break;
+ case 9: // 结点查询
+ char e;
+ BTNode* p;
+ cout << "输入要查询的结点值:" << endl;
+ cin >> e;
+ p = Search(bt, e);
+ if (p)
+ {
+ cout << "找到!";
+ cout << p->data << endl;
+ }
+ else
+ cout << "未找到!" << endl;
+ cout << endl;
+ break;
+ case 10: // 求二叉树高度
+ cout << "二叉树高度为:" << Depth(bt) << endl;
+ cout << endl;
+ break;
+ case 11: //求二叉树结点个数
+ cout << "二叉树结点数为:" << NodeCount(bt) << endl;
+ cout << endl;
+ break;
+ case 12: // 显示二叉树
+ cout << "2-显示二叉树" << endl;
+ level = 1;
+ DispBiTree(bt, level);
+ cout << endl;
+ break;
+ case 0: // 退出
+ DestroyBiTree(bt);
+ cout << "结束运行Bye-Bye!" << endl;
+ break;
+ default: // 无效选择
+ cout << "Invalid choice\n";
+ break;
+ }
+ } while (choice != 0);
+
+ return 0;
+}
\ No newline at end of file
diff --git a/Chapter5/BiTree/bitree.h b/Chapter5/BiTree/bitree.h
new file mode 100644
index 0000000..dd9d543
--- /dev/null
+++ b/Chapter5/BiTree/bitree.h
@@ -0,0 +1,421 @@
+template
+struct BTNode
+{
+ DT data; //数据域
+ BTNode* lchild; //指向左子树的指针
+ BTNode* rchild; //指向右子树的指针
+};
+
+//队列工具
+template
+struct SqQueue // 顺序队类
+{
+ BTNode** base; // 队列首址
+ int front; // 队头指针
+ int rear; // 队尾指针
+ int queuesize; // 队容量
+};
+
+//栈
+template
+struct SqStack // 顺序栈
+{
+ BTNode** base; // 栈首址
+ int top; // 栈顶指针
+ int stacksize; // 栈容量
+};
+
+//算法5.1 先序遍历递归算法
+template
+void PreOrDerBiTree(BTNode* bt)
+{
+ if (bt != NULL)
+ {
+ cout << bt->data << ' '; //输出结点上的数据
+ PreOrDerBiTree(bt->lchild); //递归的调用前序遍历左子树
+ PreOrDerBiTree(bt->rchild); //递归的调用前序遍历右子树
+ }
+ return;
+}
+
+//算法5.2 中序遍历递归算法
+template
+void InOrDerBiTree(BTNode* bt)
+{
+ if (bt != NULL)
+ {
+ InOrDerBiTree(bt->lchild); //递归的调用中序遍历左子树
+ cout << bt->data << ' '; //输出结点上的数据
+ InOrDerBiTree(bt->rchild); //递归的调用中序遍历右子树
+ }
+ return;
+}
+
+//算法5.3 后序遍历递归算法
+template
+void PostOrDerBiTree(BTNode* bt)
+{
+ if (bt != NULL)
+ {
+ PostOrDerBiTree(bt->lchild); //递归的调用后序遍历左子树
+ PostOrDerBiTree(bt->rchild); //递归的调用后序遍历右子树
+ cout << bt->data << ' '; //输出结点上的数据
+ }
+ return;
+}
+
+
+//算法5.4 层序遍历算法
+template
+void LevelBiTree(BTNode* bt)
+{
+ SqQueue Q; // 创建一个队
+ int m = 20;
+ InitQueue(Q, m);
+ BTNode* p;
+ p = bt;
+ if (p) EnQueue(Q, p); // 树非空,入队
+ while (!QueueEmpty(Q)) // 队非空
+ {
+ DeQueue(Q, p); // 出队
+ cout << p->data; // 访问
+ if (p->lchild != NULL) // 有左孩子
+ EnQueue(Q, p->lchild); // 左孩子入队
+ if (p->rchild != NULL) // 有右孩子
+ EnQueue(Q, p->rchild); // 右孩子入队
+ }
+ DestroyQueue(Q); // 销毁队列
+}
+
+
+//算法5.5 先序非遍历递归算法
+template
+void PreOrderBiTree_N(BTNode* bt)
+{
+ SqStack S; // 创建栈
+ int m = 20;
+ InitStack(S, m);
+ BTNode* p;
+ p = bt;
+ while (p != NULL || !StackEmpty(S)) // 树非空或栈非空
+ {
+ while (p != NULL) // 结点非空
+ {
+ cout << p->data << ' '; // 访问结点
+ Push(S, p); // 入栈
+ p = p->lchild; // 转左子树
+ }
+ if (!StackEmpty(S)) // 栈非空
+ {
+ Pop(S, p); // 出栈
+ p = p->rchild; // 转出栈结点的右子树
+ }
+ }
+ DestroyStack(S); //销毁栈
+}
+
+
+//算法5.6 中序非遍历递归算法
+template
+void InOrderBiTree_N(BTNode* bt)
+{
+ SqStack S; // 创建一个栈
+ int m = 20;
+ InitStack(S, m);
+ BTNode* p;
+ p = bt;
+ while (p != NULL || !StackEmpty(S)) // 结点非空或栈非空
+ {
+ while (p != NULL) // 结点非空
+ {
+ Push(S, p); // 入栈
+ p = p->lchild; // 转出栈结点右子树
+ }
+ if (!StackEmpty(S)) // 栈非空
+ {
+ Pop(S, p); // 出栈
+ cout << p->data << ' '; // 访问出栈结点
+ p = p->rchild; // 转出栈结点的右子树
+ }
+ }
+ DestroyStack(S); // 销毁栈
+}
+
+
+
+//算法5.7 后序非遍历递归算法
+template
+void PostOrderBiTree_N(BTNode* bt)
+{
+ SqStack S; // 创建一个栈
+ int m = 20;
+ InitStack(S, m);
+ BTNode* p;
+ BTNode* r;
+ p = bt;
+ bool flag;
+ do
+ {
+ while (p) // 结点非空
+ {
+ Push(S, p); // 结点入栈
+ p = p->lchild; // 转左子树
+ }
+ r = NULL; // 指向刚被访问点,初值为空
+ flag = true; // true表示处理栈顶结点
+ while (!StackEmpty(S) && flag) // 栈非空且当前处理的是栈顶结点
+ {
+ GetTop(S, p); // 获取栈顶元素
+ if (p->rchild == r) // 如果 当前结点是栈元素的右孩子
+ {
+ cout << p->data << ' '; // 访问栈顶元素
+ Pop(S, p); // 出栈
+ r = p; // r指向被访问结点
+ }
+ else // 否则
+ {
+ p = p->rchild; // 转栈顶元素右孩子
+ flag = false; // 处理非栈顶结点
+ }
+ }
+ } while (!StackEmpty(S)); // 栈非空,循环
+ cout << endl;
+ DestroyStack(S); // 销毁栈
+}
+
+
+//算法5.8 创建二叉树
+template
+void CreateBiTree(BTNode*& bt)
+{
+ char ch;
+ cin >> ch; // 输入根结点的数据
+ if (ch == '#') // # 表示指针为空,说明树为空
+ bt = NULL;
+ else
+ {
+ bt = new BTNode; // 申请内存
+ if (!bt)
+ {
+ cout << "申请内存失败!" << endl;
+ exit(-1); // 申请内存失败退出
+ }
+ bt->data = ch;
+ CreateBiTree(bt->lchild); // 创建根结点左子树
+ CreateBiTree(bt->rchild); // 创建根结点右子树
+ }
+ return;
+}
+
+
+//算法5.9 销毁二叉树
+template
+void DestroyBiTree(BTNode*& bt)
+{
+ if (bt) // 树非空
+ {
+ DestroyBiTree(bt->lchild); // 销毁左子树
+ DestroyBiTree(bt->rchild); // 销毁右子树
+ delete bt;
+ }
+}
+
+
+//算法5.10 结点查找
+
+template
+BTNode* Search(BTNode* bt, DT e) // 查找值为e的元素
+{
+ BTNode* p;
+ if (bt == NULL) // 结点为空,返回
+ return NULL;
+ else if (bt->data == e) // 找到,返回结点指针
+ return bt;
+ else // 结点值不为e
+ {
+ p = Search(bt->lchild, e); // 在左子树上查找
+ if (p != NULL) // 找到
+ return p; // 返回结点指针
+ else // 未找到
+ return Search(bt->rchild, e); // 转右子树上查找
+ }
+}
+
+//算法5.11 求树深
+template
+int Depth(BTNode* bt)
+{
+ int hl, hr;
+ if (bt == NULL) // 树空
+ return 0; // 深度为0
+ else // 树非空
+ {
+
+ hl = Depth(bt->lchild); // 求左子树深度
+ hr = Depth(bt->lchild); // 求右子树深度
+ if (hl > hr) // 左子树高
+ return hl + 1; // 树高为左子树高加1
+ else return hr + 1; // 左子树高,树高为左子树高加1
+ }
+}
+
+
+//算法5.12 结点计数
+template
+int NodeCount(BTNode* bt)
+{
+ if (bt == NULL) // 空树,结点数为0
+ return 0;
+ else // 非空树,结点数为左、右子树结点数的和加1
+ return NodeCount(bt->lchild) + NodeCount(bt->rchild) + 1;
+}
+
+template
+void DispBiTree(BTNode* bt, int level) // 显示树
+{
+ if (bt) //空二叉树不显示
+ {
+ DispBiTree(bt->rchild, level + 1); //显示右子树
+ cout << endl; //显示新行
+ for (int i = 0; i < level - 1; i++)
+ cout << " "; //确保在第level列显示节点
+ cout << bt->data; //显示节点
+ DispBiTree(bt->lchild, level + 1); //显示左子树
+ cout << endl;
+ }//if
+}
+
+
+template
+int leftCount(BTNode* bt)
+{
+ if (bt->lchild == NULL && bt->rchild == NULL) // 空树,结点数为0
+ return 0;
+ else // 非空树,结点数为左、右子树结点数的和加1
+ return NodeCount(bt->lchild) + NodeCount(bt->rchild) + 1;
+}
+
+
+//【算法3.14】 初始化队列
+template
+void InitQueue(SqQueue& Q, int m)
+{
+ Q.base = new BTNode*[m]; // 申请队列空间
+ if (Q.base == NULL) // 申请空间失败
+ {
+ cout << "未创建成功!";
+ exit(1); // 退出
+ }
+ Q.front = Q.rear = 0; // 设置队列属性
+ Q.queuesize = m;
+}
+
+
+//算法3.15】 销毁列列
+template
+void DestroyQueue(SqQueue& Q)
+{
+ delete[] Q.base; // 释放队列空间
+ Q.front = Q.rear = 0; // 设置队列属性
+ Q.queuesize = 0;
+}
+
+
+//【算法3.16】 入队
+template
+bool EnQueue(SqQueue& Q, BTNode* e)
+{
+ if ((Q.rear + 1) % Q.queuesize == Q.front) // 队满
+ return false; // 返回false
+ Q.base[Q.rear] = e; // 出队
+ Q.rear = (Q.rear + 1) % Q.queuesize; // 修改队列属性
+ return true; // 返回true
+}
+
+//【算法3.17】 出队
+template
+bool DeQueue(SqQueue& Q, BTNode*& e)
+{
+ if (Q.front == Q.rear) // 队空
+ return false;
+ e = Q.base[Q.front];
+ Q.front = (Q.front + 1) % Q.queuesize;
+ return true; // 删除成功,返回true
+}
+
+// 测队空
+template
+bool QueueEmpty(SqQueue Q)
+{
+ if (Q.front == Q.rear) // 队空
+ return true; // 返回true
+ else // 队不空
+ return false; // 返回false
+}
+
+//【算法3.1】 初始化栈
+template
+void InitStack(SqStack& S, int m)
+{
+ S.base = new BTNode*[m]; // 申请栈空间
+ if (S.base == NULL)
+ {
+ cout << "未创建成功!";
+ exit(1);
+ }
+ S.top = -1; // 空栈
+ S.stacksize = m; // 栈容量为m
+}
+
+
+//算法3.2】 销毁栈
+template
+void DestroyStack(SqStack& S)//析构函数
+{
+ delete[] S.base; //释放栈空间
+ S.top = -1;
+ S.stacksize = 0;
+}
+
+
+//【算法3.3】
+template
+bool Push(SqStack& S, BTNode* e)
+{
+ if (S.top == S.stacksize - 1) // 栈满,不能插入
+ return false;
+ S.top++;
+ S.base[S.top] = e;
+ return true; // 插入成功,返回true
+}
+
+//【算法3.4】 出栈
+template
+bool Pop(SqStack& S, BTNode*& e)
+{
+ if (S.top == -1) //栈空
+ return false;
+ e = S.base[S.top];
+ S.top--;
+ return true; // 出栈成功,返回true
+}
+
+template // 获取栈元素
+bool GetTop(SqStack& S, BTNode*& e)
+{
+ if (S.top == -1) // 栈空,返回false
+ return false;
+ e = S.base[S.top]; // 取栈元素
+ return true; // 返回true
+}
+
+
+// 测栈空
+template
+bool StackEmpty(SqStack S)
+{
+ if (S.top == -1) // 栈空,返回true
+ return true;
+ else // 栈非空,返回false
+ return false;
+}
\ No newline at end of file
diff --git a/Chapter5/Chapter5.sln b/Chapter5/Chapter5.sln
new file mode 100644
index 0000000..1e0c50a
--- /dev/null
+++ b/Chapter5/Chapter5.sln
@@ -0,0 +1,51 @@
+
+Microsoft Visual Studio Solution File, Format Version 12.00
+# Visual Studio Version 17
+VisualStudioVersion = 17.9.34701.34
+MinimumVisualStudioVersion = 10.0.40219.1
+Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "BiTree", "BiTree\BiTree.vcxproj", "{904CFCDF-DA23-415A-8D68-8E51AD913458}"
+EndProject
+Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Huffmn", "Huffmn\Huffmn.vcxproj", "{A456A451-7570-4D79-9836-04559D3E944B}"
+EndProject
+Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ThrBiTree", "ThrBiTree\ThrBiTree.vcxproj", "{37EF98CA-2156-4ECB-A15D-52041DBAC78C}"
+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
+ {904CFCDF-DA23-415A-8D68-8E51AD913458}.Debug|x64.ActiveCfg = Debug|x64
+ {904CFCDF-DA23-415A-8D68-8E51AD913458}.Debug|x64.Build.0 = Debug|x64
+ {904CFCDF-DA23-415A-8D68-8E51AD913458}.Debug|x86.ActiveCfg = Debug|Win32
+ {904CFCDF-DA23-415A-8D68-8E51AD913458}.Debug|x86.Build.0 = Debug|Win32
+ {904CFCDF-DA23-415A-8D68-8E51AD913458}.Release|x64.ActiveCfg = Release|x64
+ {904CFCDF-DA23-415A-8D68-8E51AD913458}.Release|x64.Build.0 = Release|x64
+ {904CFCDF-DA23-415A-8D68-8E51AD913458}.Release|x86.ActiveCfg = Release|Win32
+ {904CFCDF-DA23-415A-8D68-8E51AD913458}.Release|x86.Build.0 = Release|Win32
+ {A456A451-7570-4D79-9836-04559D3E944B}.Debug|x64.ActiveCfg = Debug|x64
+ {A456A451-7570-4D79-9836-04559D3E944B}.Debug|x64.Build.0 = Debug|x64
+ {A456A451-7570-4D79-9836-04559D3E944B}.Debug|x86.ActiveCfg = Debug|Win32
+ {A456A451-7570-4D79-9836-04559D3E944B}.Debug|x86.Build.0 = Debug|Win32
+ {A456A451-7570-4D79-9836-04559D3E944B}.Release|x64.ActiveCfg = Release|x64
+ {A456A451-7570-4D79-9836-04559D3E944B}.Release|x64.Build.0 = Release|x64
+ {A456A451-7570-4D79-9836-04559D3E944B}.Release|x86.ActiveCfg = Release|Win32
+ {A456A451-7570-4D79-9836-04559D3E944B}.Release|x86.Build.0 = Release|Win32
+ {37EF98CA-2156-4ECB-A15D-52041DBAC78C}.Debug|x64.ActiveCfg = Debug|x64
+ {37EF98CA-2156-4ECB-A15D-52041DBAC78C}.Debug|x64.Build.0 = Debug|x64
+ {37EF98CA-2156-4ECB-A15D-52041DBAC78C}.Debug|x86.ActiveCfg = Debug|Win32
+ {37EF98CA-2156-4ECB-A15D-52041DBAC78C}.Debug|x86.Build.0 = Debug|Win32
+ {37EF98CA-2156-4ECB-A15D-52041DBAC78C}.Release|x64.ActiveCfg = Release|x64
+ {37EF98CA-2156-4ECB-A15D-52041DBAC78C}.Release|x64.Build.0 = Release|x64
+ {37EF98CA-2156-4ECB-A15D-52041DBAC78C}.Release|x86.ActiveCfg = Release|Win32
+ {37EF98CA-2156-4ECB-A15D-52041DBAC78C}.Release|x86.Build.0 = Release|Win32
+ EndGlobalSection
+ GlobalSection(SolutionProperties) = preSolution
+ HideSolutionNode = FALSE
+ EndGlobalSection
+ GlobalSection(ExtensibilityGlobals) = postSolution
+ SolutionGuid = {9F80D9BF-39B0-44C0-A084-A3293924BB63}
+ EndGlobalSection
+EndGlobal
diff --git a/Chapter1/app1-4/app1-4.vcxproj b/Chapter5/Huffmn/Huffmn.vcxproj
similarity index 96%
rename from Chapter1/app1-4/app1-4.vcxproj
rename to Chapter5/Huffmn/Huffmn.vcxproj
index 5fe8cd1..b8d0d55 100644
--- a/Chapter1/app1-4/app1-4.vcxproj
+++ b/Chapter5/Huffmn/Huffmn.vcxproj
@@ -18,16 +18,11 @@
x64
-
-
- true
-
-
17.0
Win32Proj
- {7a2bd8ec-2595-4070-be3a-2bf2140848b2}
- app14
+ {a456a451-7570-4d79-9836-04559d3e944b}
+ Huffmn
10.0
@@ -131,6 +126,11 @@
true
+
+
+ false
+
+
diff --git a/Chapter5/Huffmn/Huffmn.vcxproj.filters b/Chapter5/Huffmn/Huffmn.vcxproj.filters
new file mode 100644
index 0000000..ad4ee75
--- /dev/null
+++ b/Chapter5/Huffmn/Huffmn.vcxproj.filters
@@ -0,0 +1,22 @@
+
+
+
+
+ {4FC737F1-C7A5-4376-A066-2A32D752A2FF}
+ cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx
+
+
+ {93995380-89BD-4b04-88EB-625FBE52EBFB}
+ h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd
+
+
+ {67DA6AB6-F800-4c08-8B7A-83BB121AAD01}
+ rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms
+
+
+
+
+ 源文件
+
+
+
\ No newline at end of file
diff --git a/Chapter5/Huffmn/HumanTree.cpp b/Chapter5/Huffmn/HumanTree.cpp
new file mode 100644
index 0000000..0a0fc98
--- /dev/null
+++ b/Chapter5/Huffmn/HumanTree.cpp
@@ -0,0 +1,165 @@
+#include"iostream"
+#include
+#include
+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;
+}
\ No newline at end of file
diff --git a/Chapter5/ThrBiTree/ThrBiTree.cpp b/Chapter5/ThrBiTree/ThrBiTree.cpp
new file mode 100644
index 0000000..0680867
--- /dev/null
+++ b/Chapter5/ThrBiTree/ThrBiTree.cpp
@@ -0,0 +1,222 @@
+#include//cout,cin
+#include
+using namespace std;
+
+template
+struct ThrBTNode
+{
+ DT data; // 数据域
+ int lflag; // 左标志域
+ int rflag; // 右标志域
+ ThrBTNode* lchild; // 左指针域
+ ThrBTNode* rchild; // 右指针域
+};
+
+
+template // 创建二叉树
+void CreateBiTree(ThrBTNode*& bt)
+{ // 按先序序列的顺序输入二叉树,#为空指针域标志;
+ char ch;
+ cin >> ch; // 输入根结点的数据
+ if (ch == '#') // # 表示指针为空,说明树为空
+ bt = NULL;
+ else
+ {
+ bt = new ThrBTNode; // 新建结点
+ if (!bt)
+ {
+ cout << "申请内存失败!" << endl;
+ exit(-1); // 申请内存失败退出
+ }
+ bt->lflag = 0; // 非线索
+ bt->rflag = 0; // 非线索
+ bt->data = ch;
+ CreateBiTree(bt->lchild); // 递归创建根结点左子树
+ CreateBiTree(bt->rchild); // 递归创建根结点右子树
+ }
+ return;
+}
+
+ThrBTNode* pre;
+
+//算法5.13 中序线索化二叉树
+template
+void InThread(ThrBTNode*& p)
+{
+
+ if (p != NULL) // 结点非空
+ {
+ InThread(p->lchild); // 中序线索化左子树
+ if (p->lchild == NULL) // 结点无左孩子
+ {
+ p->lflag = 1; // 设置前驱线索标识
+ p->lchild = pre; // 左孩子指向结点遍历前驱
+ }
+ if (pre->rchild == NULL) // 结点无右孩子
+ {
+ pre->rflag = 1; // 设置后继线索标识
+ pre->rchild = p; // 右孩子指向结点遍历后继
+ }
+ pre = p; // 前驱指向当前结点
+ InThread(p->rchild); // 中序线索化右子树
+ }
+}
+
+
+template // 构建线索二叉树
+ThrBTNode* CreateInThread(ThrBTNode*& bt)
+{
+ ThrBTNode* root;
+ root = new ThrBTNode; // 创建头结点
+ root->lflag = 0;
+ root->rflag = 1;
+ root->rchild = bt;
+ if (bt == NULL)
+ root->lchild = root;
+ else
+ {
+ root->lchild = bt;
+ pre = root;
+ InThread(bt); // 中序线索化二叉树
+ pre->rchild = root; // 设置循环链表
+ pre->rflag = 1;
+ root->rchild = pre;
+ }
+ return root;
+}
+
+
+
+
+//算法5.15 中序遍历中序线索二叉树
+template
+void InThrBiTree(ThrBTNode* bt)
+{
+ ThrBTNode* p;
+ p = bt->lchild; // 从根结点开始
+ while (p != bt) // 结点非空
+ {
+ while (p->lflag == 0) // 有左孩子
+ p = p->lchild; // 一路左行
+ cout << p->data; // 访问结点
+ while (p->rflag == 1 && p->rchild != bt) // 有后继线索且非空
+ {
+ p = p->rchild; // 转向后继
+ cout << p->data << " "; // 访问后继结点
+ }
+ p = p->rchild; // 无后继线索,转向右子树
+ }
+}
+
+template // 显示线索二叉树
+void DispBiTree(ThrBTNode* bt, int level)
+{
+ if (bt) // 空二叉树不显示
+ {
+ DispBiTree(bt->rchild, level + 1); // 显示右子树
+ cout << endl; // 显示新行
+ for (int i = 0; i < level - 1; i++)
+ cout << " "; // 确保在第level列显示节点
+ cout << bt->data; // 显示节点
+ DispBiTree(bt->lchild, level + 1); // 显示左子树
+ cout << endl;
+ }//if
+}//DisplayBTree
+
+//算法5.9 销毁二叉树
+template
+void DestroyThrBiTree(ThrBTNode*& bt)
+{
+ if (bt)
+ {
+ DestroyThrBiTree(bt->lchild);
+ DestroyThrBiTree(bt->rchild);
+ delete bt;
+ }
+}
+
+void dispmenu()
+{ // 显示主菜单
+ cout << endl;
+ cout << "1-创建二叉树\n";
+ //cout<<"2-先序线索化二叉树的先序遍历\n";
+ //cout<<"3-先序遍历先序线索化二叉树\n";
+ cout << "4-中序线索化二叉树\n";
+ cout << "5-中序遍历中序线索二叉树\n";
+ cout << "6-显示二叉树\n";
+ cout << "7-退出\n";
+}
+
+// 测试数据
+string fbt = "a b d # # e # # c f # # g # #"; // 满二叉树最后一层每个叶结点后有两个#
+string cbt = "a b d # # e # # c # #"; // 完全二叉树每个叶结点后有两个#
+string gbt = "a b # d # # c e # # #"; // 一般二叉树
+string obt = "a b c d # # # # #"; // 左斜树
+
+int main()
+{
+
+ //char pause;
+ int level;
+ ThrBTNode* bt;
+
+
+ system("cls"); // 清屏
+
+ int choice;
+ do
+ {
+ dispmenu(); //显示主菜单
+ cout << "Enter choice(1~7):";
+ cin >> choice;
+ switch (choice)
+ {
+ case 1: //创建二叉树
+ cout << "测试数据参考:" << endl;
+ cout << "满二叉树:" << fbt << endl;
+ cout << "完全二叉树:" << cbt << endl;
+ cout << "一般二叉树:" << gbt << endl;
+ cout << "左斜二叉树:" << obt << endl;
+ cout << "请按先序序列的顺序输入二叉树,#为空指针域标志:" << endl;
+ CreateBiTree(bt);
+ break;
+ case 2://先序线索化二叉树
+
+ //PreThread(bt);
+ //cin.get(pause);
+ //system("pause");
+ break;
+ case 3://先序遍历先线索化二叉树
+
+ //PreOrDerBiTree(bt);
+ //cin.get(pause);
+ //system("pause");
+ //breCreateInThread(bt);
+ case 4: //中序线索化二叉树
+ bt = CreateInThread(bt);
+ cout << "中序线索化成功!";
+ cout << endl;
+ break;
+ case 5: //中序遍历中序线索二叉树
+ cout << "中序遍历序列为:" << endl;
+ cout << endl;
+ InThrBiTree(bt);
+ break;
+ case 6: //显示二叉树
+ cout << "2-显示二叉树" << endl;
+ level = 1;
+ DispBiTree(bt, level);
+ cout << endl;
+ break;
+ case 7://退出
+ cout << "结束运行!" << endl;
+ DestroyThrBiTree(bt);
+ break;
+ default://非法选择
+ cout << "Invalid choice\n";
+ break;
+ }
+ } while (choice != 7);
+
+ return 0;
+}
\ No newline at end of file
diff --git a/Chapter5/ThrBiTree/ThrBiTree.vcxproj b/Chapter5/ThrBiTree/ThrBiTree.vcxproj
new file mode 100644
index 0000000..2c70883
--- /dev/null
+++ b/Chapter5/ThrBiTree/ThrBiTree.vcxproj
@@ -0,0 +1,135 @@
+
+
+
+
+ Debug
+ Win32
+
+
+ Release
+ Win32
+
+
+ Debug
+ x64
+
+
+ Release
+ x64
+
+
+
+ 17.0
+ Win32Proj
+ {37ef98ca-2156-4ecb-a15d-52041dbac78c}
+ ThrBiTree
+ 10.0
+
+
+
+ Application
+ true
+ v143
+ Unicode
+
+
+ Application
+ false
+ v143
+ true
+ Unicode
+
+
+ Application
+ true
+ v143
+ Unicode
+
+
+ Application
+ false
+ v143
+ true
+ Unicode
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Level3
+ true
+ WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)
+ true
+
+
+ Console
+ true
+
+
+
+
+ Level3
+ true
+ true
+ true
+ WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)
+ true
+
+
+ Console
+ true
+ true
+ true
+
+
+
+
+ Level3
+ false
+ _DEBUG;_CONSOLE;%(PreprocessorDefinitions)
+ true
+
+
+ Console
+ true
+
+
+
+
+ Level3
+ true
+ true
+ true
+ NDEBUG;_CONSOLE;%(PreprocessorDefinitions)
+ true
+
+
+ Console
+ true
+ true
+ true
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/Chapter5/ThrBiTree/ThrBiTree.vcxproj.filters b/Chapter5/ThrBiTree/ThrBiTree.vcxproj.filters
new file mode 100644
index 0000000..8bbed55
--- /dev/null
+++ b/Chapter5/ThrBiTree/ThrBiTree.vcxproj.filters
@@ -0,0 +1,22 @@
+
+
+
+
+ {4FC737F1-C7A5-4376-A066-2A32D752A2FF}
+ cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx
+
+
+ {93995380-89BD-4b04-88EB-625FBE52EBFB}
+ h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd
+
+
+ {67DA6AB6-F800-4c08-8B7A-83BB121AAD01}
+ rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms
+
+
+
+
+ 源文件
+
+
+
\ No newline at end of file
diff --git a/Exercise/BiTreeExterned/BiTreeExterned.sln b/Exercise/BiTreeExterned/BiTreeExterned.sln
new file mode 100644
index 0000000..0ec47fc
--- /dev/null
+++ b/Exercise/BiTreeExterned/BiTreeExterned.sln
@@ -0,0 +1,31 @@
+
+Microsoft Visual Studio Solution File, Format Version 12.00
+# Visual Studio Version 17
+VisualStudioVersion = 17.9.34728.123
+MinimumVisualStudioVersion = 10.0.40219.1
+Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "BiTreeExterned", "BiTreeExterned.vcxproj", "{67DB3E23-69FE-496E-B980-2F234B1777BA}"
+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
+ {67DB3E23-69FE-496E-B980-2F234B1777BA}.Debug|x64.ActiveCfg = Debug|x64
+ {67DB3E23-69FE-496E-B980-2F234B1777BA}.Debug|x64.Build.0 = Debug|x64
+ {67DB3E23-69FE-496E-B980-2F234B1777BA}.Debug|x86.ActiveCfg = Debug|Win32
+ {67DB3E23-69FE-496E-B980-2F234B1777BA}.Debug|x86.Build.0 = Debug|Win32
+ {67DB3E23-69FE-496E-B980-2F234B1777BA}.Release|x64.ActiveCfg = Release|x64
+ {67DB3E23-69FE-496E-B980-2F234B1777BA}.Release|x64.Build.0 = Release|x64
+ {67DB3E23-69FE-496E-B980-2F234B1777BA}.Release|x86.ActiveCfg = Release|Win32
+ {67DB3E23-69FE-496E-B980-2F234B1777BA}.Release|x86.Build.0 = Release|Win32
+ EndGlobalSection
+ GlobalSection(SolutionProperties) = preSolution
+ HideSolutionNode = FALSE
+ EndGlobalSection
+ GlobalSection(ExtensibilityGlobals) = postSolution
+ SolutionGuid = {313D7A25-E7B0-4ED7-BCA3-EFCA771BB40E}
+ EndGlobalSection
+EndGlobal
diff --git a/Exercise/BiTreeExterned/BiTreeExterned.vcxproj b/Exercise/BiTreeExterned/BiTreeExterned.vcxproj
new file mode 100644
index 0000000..a4fe4ca
--- /dev/null
+++ b/Exercise/BiTreeExterned/BiTreeExterned.vcxproj
@@ -0,0 +1,138 @@
+
+
+
+
+ Debug
+ Win32
+
+
+ Release
+ Win32
+
+
+ Debug
+ x64
+
+
+ Release
+ x64
+
+
+
+ 17.0
+ Win32Proj
+ {67db3e23-69fe-496e-b980-2f234b1777ba}
+ BiTreeExterned
+ 10.0
+
+
+
+ Application
+ true
+ v143
+ Unicode
+
+
+ Application
+ false
+ v143
+ true
+ Unicode
+
+
+ Application
+ true
+ v143
+ Unicode
+
+
+ Application
+ false
+ v143
+ true
+ Unicode
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Level3
+ true
+ WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)
+ true
+
+
+ Console
+ true
+
+
+
+
+ Level3
+ true
+ true
+ true
+ WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)
+ true
+
+
+ Console
+ true
+ true
+ true
+
+
+
+
+ Level3
+ false
+ _DEBUG;_CONSOLE;%(PreprocessorDefinitions)
+ true
+
+
+ Console
+ true
+
+
+
+
+ Level3
+ true
+ true
+ true
+ NDEBUG;_CONSOLE;%(PreprocessorDefinitions)
+ true
+
+
+ Console
+ true
+ true
+ true
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/Exercise/BiTreeExterned/BiTreeExterned.vcxproj.filters b/Exercise/BiTreeExterned/BiTreeExterned.vcxproj.filters
new file mode 100644
index 0000000..1c99778
--- /dev/null
+++ b/Exercise/BiTreeExterned/BiTreeExterned.vcxproj.filters
@@ -0,0 +1,27 @@
+
+
+
+
+ {4FC737F1-C7A5-4376-A066-2A32D752A2FF}
+ cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx
+
+
+ {93995380-89BD-4b04-88EB-625FBE52EBFB}
+ h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd
+
+
+ {67DA6AB6-F800-4c08-8B7A-83BB121AAD01}
+ rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms
+
+
+
+
+ 源文件
+
+
+
+
+ 头文件
+
+
+
\ No newline at end of file
diff --git a/Exercise/BiTreeExterned/bitree.cpp b/Exercise/BiTreeExterned/bitree.cpp
new file mode 100644
index 0000000..580fb66
--- /dev/null
+++ b/Exercise/BiTreeExterned/bitree.cpp
@@ -0,0 +1,136 @@
+#include//cout,cin
+#include
+using namespace std;
+#include"bitree.h"
+
+//测试参考数据
+string fbt = "a b d # # e # # c f # # g # #"; // 满二叉树最后一层每个叶结点后有两个#
+string cbt = "a b d # # e # # c # #"; // 完全二叉树每个叶结点后有两个#
+string gbt = "a b # d # # c e # # #"; // 一般二叉树
+string obt = "a b c d # # # # #"; // 左斜树1
+
+
+void dispmenu()
+{ //显示主菜单
+ cout << endl;
+ cout << "1-创建二叉树\n";
+ cout << "2-先序递归遍历二叉树\n";
+ cout << "3-中序递归遍历二叉树\n";
+ cout << "4-后序递归遍历二叉树\n";
+ cout << "5-层序遍历二叉树\n";
+ cout << "6-先序非递归遍历二叉树\n";
+ cout << "7-中序非递归遍历二叉树\n";
+ cout << "8-后序非递归遍历二叉树\n";
+ cout << "9-结点查询\n";
+ cout << "10-求二叉树高度\n";
+ cout << "11-求二叉树结点个数\n";
+ cout << "12-显示二叉树\n";
+ cout << "13-删除以值为x的结点的子树\n";
+ cout << "0 -退出\n";
+}
+
+int main()
+{
+ int level;
+ BTNode* bt;
+
+ system("cls"); // 清屏
+
+ int choice;
+ do
+ {
+ dispmenu(); // 显示主菜单
+ cout << "Enter choice(1~12,0 退出):";
+ cin >> choice;
+ switch (choice)
+ {
+ case 1: // 创建二叉树
+ cout << "测试参考数据:" << endl;
+ cout << "满二叉树:" << fbt << endl;
+ cout << "完全二叉树:" << cbt << endl;
+ cout << "一般二叉树:" << gbt << endl;
+ cout << "左斜二叉树:" << obt << endl;
+ cout << "请按先序序列的顺序输入二叉树,#为空指针域标志:" << endl;
+ CreateBiTree(bt);
+ break;
+ case 2: // 先序递归遍历二叉树
+ cout << "先序遍历序列为:" << endl;
+ PreOrDerBiTree(bt);
+ break;
+ case 3: // 中序递归遍历二叉树
+ cout << "中序遍历序列为:" << endl;
+ InOrDerBiTree(bt);
+ cout << endl;
+ break;
+ case 4: // 后序递归遍历二叉树
+ cout << "后序遍历序列为:" << endl;
+ PostOrDerBiTree(bt);
+ cout << endl;
+ break;
+ case 5: // 层序遍历二叉树
+ cout << "层序遍历序列为:" << endl;
+ cout << endl;
+ LevelBiTree(bt);
+ break;
+ case 6: // 先序非递归遍历二叉树
+ cout << "先序非递归遍历序列为:" << endl;
+ PreOrderBiTree_N(bt);
+ cout << endl;
+ break;
+ case 7: // 中序非递归遍历二叉树
+ cout << "中序非递归遍历序列为:" << endl;
+ InOrderBiTree_N(bt);
+ cout << endl;
+ break;
+ case 8: // 后序非递归遍历二叉树
+ cout << "后序非递归遍历序列为:" << endl;
+ cout << endl;
+ PostOrderBiTree_N(bt);
+ break;
+ case 9: // 结点查询
+ char e;
+ BTNode* p;
+ cout << "输入要查询的结点值:" << endl;
+ cin >> e;
+ p = Search(bt, e);
+ if (p)
+ {
+ cout << "找到!";
+ cout << p->data << endl;
+ }
+ else
+ cout << "未找到!" << endl;
+ cout << endl;
+ break;
+ case 10: // 求二叉树高度
+ cout << "二叉树高度为:" << Depth(bt) << endl;
+ cout << endl;
+ break;
+ case 11: //求二叉树结点个数
+ cout << "二叉树结点数为:" << NodeCount(bt) << endl;
+ cout << endl;
+ break;
+ case 12: // 显示二叉树
+ cout << "2-显示二叉树" << endl;
+ level = 1;
+ DispBiTree(bt, level);
+ cout << endl;
+ break;
+ case 13:
+ cout << "输入结点的值:";
+ char x;
+ cin >> x;
+ DelChildWithRootX(bt, x);
+ break;
+ case 0: // 退出
+ DestroyBiTree(bt);
+ cout << "结束运行Bye-Bye!" << endl;
+ break;
+ default: // 无效选择
+ cout << "Invalid choice\n";
+ break;
+ }
+ } while (choice != 0);
+
+ return 0;
+}
\ No newline at end of file
diff --git a/Exercise/BiTreeExterned/bitree.h b/Exercise/BiTreeExterned/bitree.h
new file mode 100644
index 0000000..ef78468
--- /dev/null
+++ b/Exercise/BiTreeExterned/bitree.h
@@ -0,0 +1,436 @@
+template
+struct BTNode
+{
+ DT data; //数据域
+ BTNode* lchild; //指向左子树的指针
+ BTNode* rchild; //指向右子树的指针
+};
+
+//队列工具
+template
+struct SqQueue // 顺序队类
+{
+ BTNode** base; // 队列首址
+ int front; // 队头指针
+ int rear; // 队尾指针
+ int queuesize; // 队容量
+};
+
+//栈
+template
+struct SqStack // 顺序栈
+{
+ BTNode** base; // 栈首址
+ int top; // 栈顶指针
+ int stacksize; // 栈容量
+};
+
+//算法5.1 先序遍历递归算法
+template
+void PreOrDerBiTree(BTNode* bt)
+{
+ if (bt != NULL)
+ {
+ cout << bt->data << ' '; //输出结点上的数据
+ PreOrDerBiTree(bt->lchild); //递归的调用前序遍历左子树
+ PreOrDerBiTree(bt->rchild); //递归的调用前序遍历右子树
+ }
+ return;
+}
+
+//算法5.2 中序遍历递归算法
+template
+void InOrDerBiTree(BTNode* bt)
+{
+ if (bt != NULL)
+ {
+ InOrDerBiTree(bt->lchild); //递归的调用中序遍历左子树
+ cout << bt->data << ' '; //输出结点上的数据
+ InOrDerBiTree(bt->rchild); //递归的调用中序遍历右子树
+ }
+ return;
+}
+
+//算法5.3 后序遍历递归算法
+template
+void PostOrDerBiTree(BTNode* bt)
+{
+ if (bt != NULL)
+ {
+ PostOrDerBiTree(bt->lchild); //递归的调用后序遍历左子树
+ PostOrDerBiTree(bt->rchild); //递归的调用后序遍历右子树
+ cout << bt->data << ' '; //输出结点上的数据
+ }
+ return;
+}
+
+
+//算法5.4 层序遍历算法
+template
+void LevelBiTree(BTNode* bt)
+{
+ SqQueue Q; // 创建一个队
+ int m = 20;
+ InitQueue(Q, m);
+ BTNode* p;
+ p = bt;
+ if (p) EnQueue(Q, p); // 树非空,入队
+ while (!QueueEmpty(Q)) // 队非空
+ {
+ DeQueue(Q, p); // 出队
+ cout << p->data; // 访问
+ if (p->lchild != NULL) // 有左孩子
+ EnQueue(Q, p->lchild); // 左孩子入队
+ if (p->rchild != NULL) // 有右孩子
+ EnQueue(Q, p->rchild); // 右孩子入队
+ }
+ DestroyQueue(Q); // 销毁队列
+}
+
+
+//算法5.5 先序非遍历递归算法
+template
+void PreOrderBiTree_N(BTNode* bt)
+{
+ SqStack S; // 创建栈
+ int m = 20;
+ InitStack(S, m);
+ BTNode* p;
+ p = bt;
+ while (p != NULL || !StackEmpty(S)) // 树非空或栈非空
+ {
+ while (p != NULL) // 结点非空
+ {
+ cout << p->data << ' '; // 访问结点
+ Push(S, p); // 入栈
+ p = p->lchild; // 转左子树
+ }
+ if (!StackEmpty(S)) // 栈非空
+ {
+ Pop(S, p); // 出栈
+ p = p->rchild; // 转出栈结点的右子树
+ }
+ }
+ DestroyStack(S); //销毁栈
+}
+
+
+//算法5.6 中序非遍历递归算法
+template
+void InOrderBiTree_N(BTNode* bt)
+{
+ SqStack S; // 创建一个栈
+ int m = 20;
+ InitStack(S, m);
+ BTNode* p;
+ p = bt;
+ while (p != NULL || !StackEmpty(S)) // 结点非空或栈非空
+ {
+ while (p != NULL) // 结点非空
+ {
+ Push(S, p); // 入栈
+ p = p->lchild; // 转出栈结点右子树
+ }
+ if (!StackEmpty(S)) // 栈非空
+ {
+ Pop(S, p); // 出栈
+ cout << p->data << ' '; // 访问出栈结点
+ p = p->rchild; // 转出栈结点的右子树
+ }
+ }
+ DestroyStack(S); // 销毁栈
+}
+
+
+
+//算法5.7 后序非遍历递归算法
+template
+void PostOrderBiTree_N(BTNode* bt)
+{
+ SqStack S; // 创建一个栈
+ int m = 20;
+ InitStack(S, m);
+ BTNode* p;
+ BTNode* r;
+ p = bt;
+ bool flag;
+ do
+ {
+ while (p) // 结点非空
+ {
+ Push(S, p); // 结点入栈
+ p = p->lchild; // 转左子树
+ }
+ r = NULL; // 指向刚被访问点,初值为空
+ flag = true; // true表示处理栈顶结点
+ while (!StackEmpty(S) && flag) // 栈非空且当前处理的是栈顶结点
+ {
+ GetTop(S, p); // 获取栈顶元素
+ if (p->rchild == r) // 如果 当前结点是栈元素的右孩子
+ {
+ cout << p->data << ' '; // 访问栈顶元素
+ Pop(S, p); // 出栈
+ r = p; // r指向被访问结点
+ }
+ else // 否则
+ {
+ p = p->rchild; // 转栈顶元素右孩子
+ flag = false; // 处理非栈顶结点
+ }
+ }
+ } while (!StackEmpty(S)); // 栈非空,循环
+ cout << endl;
+ DestroyStack(S); // 销毁栈
+}
+
+
+//算法5.8 创建二叉树
+template
+void CreateBiTree(BTNode*& bt)
+{
+ char ch;
+ cin >> ch; // 输入根结点的数据
+ if (ch == '#') // # 表示指针为空,说明树为空
+ bt = NULL;
+ else
+ {
+ bt = new BTNode; // 申请内存
+ if (!bt)
+ {
+ cout << "申请内存失败!" << endl;
+ exit(-1); // 申请内存失败退出
+ }
+ bt->data = ch;
+ CreateBiTree(bt->lchild); // 创建根结点左子树
+ CreateBiTree(bt->rchild); // 创建根结点右子树
+ }
+ return;
+}
+
+
+//算法5.9 销毁二叉树
+template
+void DestroyBiTree(BTNode*& bt)
+{
+ if (bt) // 树非空
+ {
+ DestroyBiTree(bt->lchild); // 销毁左子树
+ DestroyBiTree(bt->rchild); // 销毁右子树
+ delete bt;
+ }
+}
+
+
+//算法5.10 结点查找
+
+template
+BTNode* Search(BTNode* bt, DT e) // 查找值为e的元素
+{
+ BTNode* p;
+ if (bt == NULL) // 结点为空,返回
+ return NULL;
+ else if (bt->data == e) // 找到,返回结点指针
+ return bt;
+ else // 结点值不为e
+ {
+ p = Search(bt->lchild, e); // 在左子树上查找
+ if (p != NULL) // 找到
+ return p; // 返回结点指针
+ else // 未找到
+ return Search(bt->rchild, e); // 转右子树上查找
+ }
+}
+
+//算法5.11 求树深
+template
+int Depth(BTNode* bt)
+{
+ int hl, hr;
+ if (bt == NULL) // 树空
+ return 0; // 深度为0
+ else // 树非空
+ {
+
+ hl = Depth(bt->lchild); // 求左子树深度
+ hr = Depth(bt->lchild); // 求右子树深度
+ if (hl > hr) // 左子树高
+ return hl + 1; // 树高为左子树高加1
+ else return hr + 1; // 左子树高,树高为左子树高加1
+ }
+}
+
+
+//算法5.12 结点计数
+template
+int NodeCount(BTNode* bt)
+{
+ if (bt == NULL) // 空树,结点数为0
+ return 0;
+ else // 非空树,结点数为左、右子树结点数的和加1
+ return NodeCount(bt->lchild) + NodeCount(bt->rchild) + 1;
+}
+
+template
+void DispBiTree(BTNode* bt, int level) // 显示树
+{
+ if (bt) //空二叉树不显示
+ {
+ DispBiTree(bt->rchild, level + 1); //显示右子树
+ cout << endl; //显示新行
+ for (int i = 0; i < level - 1; i++)
+ cout << " "; //确保在第level列显示节点
+ cout << bt->data; //显示节点
+ DispBiTree(bt->lchild, level + 1); //显示左子树
+ cout << endl;
+ }//if
+}
+
+template
+void DelChildWithRootX(BTNode*& bt, DT x) {
+ if (bt == NULL) return;
+ else {
+ if (bt->data == x) {
+ DestroyBiTree(bt);
+ bt = NULL;
+ return;
+ }
+ else {
+ DelChildWithRootX(bt->lchild, x);
+ DelChildWithRootX(bt->rchild, x);
+ }
+ }
+}
+
+template
+int leftCount(BTNode* bt)
+{
+ if (bt->lchild == NULL && bt->rchild == NULL) // 空树,结点数为0
+ return 0;
+ else // 非空树,结点数为左、右子树结点数的和加1
+ return NodeCount(bt->lchild) + NodeCount(bt->rchild) + 1;
+}
+
+
+//【算法3.14】 初始化队列
+template
+void InitQueue(SqQueue& Q, int m)
+{
+ Q.base = new BTNode*[m]; // 申请队列空间
+ if (Q.base == NULL) // 申请空间失败
+ {
+ cout << "未创建成功!";
+ exit(1); // 退出
+ }
+ Q.front = Q.rear = 0; // 设置队列属性
+ Q.queuesize = m;
+}
+
+
+//算法3.15】 销毁列列
+template
+void DestroyQueue(SqQueue& Q)
+{
+ delete[] Q.base; // 释放队列空间
+ Q.front = Q.rear = 0; // 设置队列属性
+ Q.queuesize = 0;
+}
+
+
+//【算法3.16】 入队
+template
+bool EnQueue(SqQueue& Q, BTNode* e)
+{
+ if ((Q.rear + 1) % Q.queuesize == Q.front) // 队满
+ return false; // 返回false
+ Q.base[Q.rear] = e; // 出队
+ Q.rear = (Q.rear + 1) % Q.queuesize; // 修改队列属性
+ return true; // 返回true
+}
+
+//【算法3.17】 出队
+template
+bool DeQueue(SqQueue& Q, BTNode*& e)
+{
+ if (Q.front == Q.rear) // 队空
+ return false;
+ e = Q.base[Q.front];
+ Q.front = (Q.front + 1) % Q.queuesize;
+ return true; // 删除成功,返回true
+}
+
+// 测队空
+template
+bool QueueEmpty(SqQueue Q)
+{
+ if (Q.front == Q.rear) // 队空
+ return true; // 返回true
+ else // 队不空
+ return false; // 返回false
+}
+
+//【算法3.1】 初始化栈
+template
+void InitStack(SqStack& S, int m)
+{
+ S.base = new BTNode*[m]; // 申请栈空间
+ if (S.base == NULL)
+ {
+ cout << "未创建成功!";
+ exit(1);
+ }
+ S.top = -1; // 空栈
+ S.stacksize = m; // 栈容量为m
+}
+
+
+//算法3.2】 销毁栈
+template
+void DestroyStack(SqStack& S)//析构函数
+{
+ delete[] S.base; //释放栈空间
+ S.top = -1;
+ S.stacksize = 0;
+}
+
+
+//【算法3.3】
+template
+bool Push(SqStack& S, BTNode* e)
+{
+ if (S.top == S.stacksize - 1) // 栈满,不能插入
+ return false;
+ S.top++;
+ S.base[S.top] = e;
+ return true; // 插入成功,返回true
+}
+
+//【算法3.4】 出栈
+template
+bool Pop(SqStack& S, BTNode*& e)
+{
+ if (S.top == -1) //栈空
+ return false;
+ e = S.base[S.top];
+ S.top--;
+ return true; // 出栈成功,返回true
+}
+
+template // 获取栈元素
+bool GetTop(SqStack& S, BTNode