diff --git a/Alogrithm/.vs/Alogrithm/v16/.suo b/Alogrithm/.vs/Alogrithm/v16/.suo index 429e388..e37636e 100644 Binary files a/Alogrithm/.vs/Alogrithm/v16/.suo and b/Alogrithm/.vs/Alogrithm/v16/.suo differ diff --git a/Alogrithm/Alogrithm/Alogrithm.vcxproj b/Alogrithm/Alogrithm/Alogrithm.vcxproj index 0da0661..29b9647 100644 --- a/Alogrithm/Alogrithm/Alogrithm.vcxproj +++ b/Alogrithm/Alogrithm/Alogrithm.vcxproj @@ -30,7 +30,7 @@ Application true v142 - Unicode + MultiByte Application @@ -144,6 +144,7 @@ + @@ -151,12 +152,14 @@ + + diff --git a/Alogrithm/Alogrithm/Alogrithm.vcxproj.filters b/Alogrithm/Alogrithm/Alogrithm.vcxproj.filters index 1b932e1..3d3b3bc 100644 --- a/Alogrithm/Alogrithm/Alogrithm.vcxproj.filters +++ b/Alogrithm/Alogrithm/Alogrithm.vcxproj.filters @@ -39,6 +39,9 @@ 源文件\src + + 源文件\src + @@ -53,6 +56,9 @@ 头文件\include + + 头文件\include + @@ -67,5 +73,8 @@ 资源文件\config + + 资源文件\config + \ No newline at end of file diff --git a/Alogrithm/Alogrithm/config/5_MinDepth.ini b/Alogrithm/Alogrithm/config/5_MinDepth.ini new file mode 100644 index 0000000..5fa6d8b --- /dev/null +++ b/Alogrithm/Alogrithm/config/5_MinDepth.ini @@ -0,0 +1,18 @@ +[Test1] +input=3,9,20,NULL,NULL,15,7 +output=2 +[Test2] +input=4,3,1,2,NULL,6,8 +output=3 +[Test3] +input=4,3,1,NULL,2,6,8 +output=3 +[Test5] +input=NULL +output=1 +[Test6] +input= +output=0 +[Test7] +input=1,0,0,2,NULL,NULL,0 +output=3 \ No newline at end of file diff --git a/Alogrithm/Alogrithm/config/6_ContainsDuplicate.ini b/Alogrithm/Alogrithm/config/6_ContainsDuplicate.ini new file mode 100644 index 0000000..e69de29 diff --git a/Alogrithm/Alogrithm/include/5_MinDepth.h b/Alogrithm/Alogrithm/include/5_MinDepth.h new file mode 100644 index 0000000..8ad75fd --- /dev/null +++ b/Alogrithm/Alogrithm/include/5_MinDepth.h @@ -0,0 +1,18 @@ +#pragma once +#include +#include +#include + + +struct TreeNode +{ + int val; + struct TreeNode* left; + struct TreeNode* right; +}; + + +int MinDepth(struct TreeNode* root); +TreeNode* CreatBitTree(char str[][50], int return_count); +void CreatBitTreeNode1(char str[][50], int return_count, TreeNode* cur, int curIndex); +void free_tree(TreeNode* T); diff --git a/Alogrithm/Alogrithm/include/6_ContainsDuplicate.h b/Alogrithm/Alogrithm/include/6_ContainsDuplicate.h new file mode 100644 index 0000000..e69de29 diff --git a/Alogrithm/Alogrithm/src/5_MinDepth.cpp b/Alogrithm/Alogrithm/src/5_MinDepth.cpp new file mode 100644 index 0000000..704e717 --- /dev/null +++ b/Alogrithm/Alogrithm/src/5_MinDepth.cpp @@ -0,0 +1,83 @@ +#include "../include/5_MinDepth.h" + +TreeNode* CreatBitTree(char str[][50], int return_count) +{ + if (str == NULL || return_count <= 0) { + return NULL; + } + TreeNode* head = (TreeNode*)malloc(sizeof(TreeNode)); + if (NULL == head) { + return NULL; + } + head->val = _ttoi(str[0]); + CreatBitTreeNode1(str, return_count, head, 0); + return head; +} +void CreatBitTreeNode1(char str[][50], int return_count, TreeNode* cur, int curIndex) +{ + if (str == NULL || return_count <= 0 || cur == NULL || curIndex >= return_count || curIndex < 0) { + return; + } + int last = return_count - 1; + if ((2 * curIndex + 1 <= last) && strcmp(str[2 * curIndex + 1], "NULL")) { + cur->left = (TreeNode*)malloc(sizeof(TreeNode)); + if (NULL == cur->left) { + return; + } + cur->left->val = _ttoi(str[2 * curIndex + 1]); + } + else { + cur->left = NULL; + } + if ((2 * curIndex + 2 <= last) && strcmp(str[2 * curIndex + 2], "NULL")) { + cur->right = (TreeNode*)malloc(sizeof(TreeNode)); + if (NULL == cur->right) { + return; + } + cur->right->val = _ttoi(str[2 * curIndex + 2]); + } + else { + cur->right = NULL; + } + CreatBitTreeNode1(str, return_count, cur->left, 2 * curIndex + 1); + CreatBitTreeNode1(str, return_count, cur->right, 2 * curIndex + 2); +} +int MinDepth(struct TreeNode* root) +{ + int Depth = 0; + int L1, L2; + if (NULL == root) { + return 0; + } + else if ((NULL == root->left) && (NULL == root->right)) { + return 1; + } + else if ((NULL != root->left) && (NULL == root->right)) { + Depth = MinDepth(root->left); + } + else if ((NULL == root->left) && (NULL != root->right)) { + Depth = MinDepth(root->right); + } + else { + L1 = MinDepth(root->left); + L2 = MinDepth(root->right); + Depth = fmin(L1, L2); + } + return Depth + 1; +} +void free_tree(TreeNode* T)//ͷ +{ + if (!T) { + return; + } + if (T->left) { + free_tree(T->left); + } + if (T->right) { + free_tree(T->right); + } + if (T) { + free(T); + T = NULL; + } +} \ No newline at end of file diff --git a/Alogrithm/Alogrithm/src/6_ContainsDuplicate.cpp b/Alogrithm/Alogrithm/src/6_ContainsDuplicate.cpp new file mode 100644 index 0000000..e69de29 diff --git a/Alogrithm/UnitTest/UnitTest.cpp b/Alogrithm/UnitTest/UnitTest.cpp index 08d0654..7fc0a00 100644 --- a/Alogrithm/UnitTest/UnitTest.cpp +++ b/Alogrithm/UnitTest/UnitTest.cpp @@ -6,6 +6,7 @@ using namespace Microsoft::VisualStudio::CppUnitTestFramework; #define FileName_2 "../Alogrithm/config/2_ExcelSheetColumnTiTle.ini" #define FileName_3 "../Alogrithm/config/3_bool IsUgly.ini" #define FileName_4 "../Alogrithm/config/4_IsPalindrome.ini" +#define FileName_5 "../Alogrithm/config/5_MinDepth.ini" namespace UnitTest { @@ -73,4 +74,23 @@ namespace UnitTest } } }; + TEST_CLASS(UnitTest_5) + { + TEST_METHOD(TestMethode1) + { + char Section_Name[100][10] = { 0 }; + int Section_Count = CalcCount(100, Section_Name, FileName_5); + CString Na, nExpect; + for (int i = 0; i < Section_Count; i++) { + GetPrivateProfileString(Section_Name[i], "input", " ", Na.GetBuffer(200), 200, FileName_5); + GetPrivateProfileString(Section_Name[i], "output", " ", nExpect.GetBuffer(20), 20, FileName_5); + char return_str[100][50] = { 0 }; + int return_count = str_device2(Na, return_str); + TreeNode* root = CreatBitTree(return_str, return_count); + int nReal = MinDepth(root); + Assert::AreEqual(nReal,_ttoi(nExpect)); + free_tree(root); + } + } + }; } diff --git a/Alogrithm/UnitTest/UnitTest.vcxproj b/Alogrithm/UnitTest/UnitTest.vcxproj index 0f62b5f..aa3f479 100644 --- a/Alogrithm/UnitTest/UnitTest.vcxproj +++ b/Alogrithm/UnitTest/UnitTest.vcxproj @@ -103,7 +103,7 @@ Windows $(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories) DebugFull - ../Alogrithm/Debug/1_ContainsNearbyDuplicate.obj;../Alogrithm/Debug/2_ExcelSheetColumnTitle.obj;../Alogrithm/Debug/3_bool IsUgly.obj;../Alogrithm/Debug/4_IsPalindrome.obj;%(AdditionalDependencies) + ../Alogrithm/Debug/1_ContainsNearbyDuplicate.obj;../Alogrithm/Debug/2_ExcelSheetColumnTitle.obj;../Alogrithm/Debug/3_bool IsUgly.obj;../Alogrithm/Debug/4_IsPalindrome.obj;../Alogrithm/Debug/5_MinDepth.obj;%(AdditionalDependencies) diff --git a/Alogrithm/UnitTest/pch.cpp b/Alogrithm/UnitTest/pch.cpp index 3936926..39c05ed 100644 --- a/Alogrithm/UnitTest/pch.cpp +++ b/Alogrithm/UnitTest/pch.cpp @@ -70,7 +70,25 @@ int* str_device(CString str, int* value_count) return Section_devide; //返回数组首地址 } #endif - +//字符串切割函数,返回值为切割后元素的个数,每个元素的类型为字符串型 +int str_device2(CString str, char(*return_str)[50]) +{ + int value_count = 0; + char* token; //存放被切割后的第一个子串 + char Section_value[500] = { 0 };//存放nums转换成string类型的结果 + memset(Section_value, 0, sizeof(char) * 500); + strcpy(Section_value, str);//将CString类型的字符串转换成char类型,方便后面切割字符串 + //获得切割到的第一个字符串 + token = strtok(Section_value, ","); + /* 继续获取其他的子字符串 */ + while (token != NULL) { + strcpy(return_str[value_count], token); + token = strtok(NULL, ","); + value_count++; //记录存了多少个元素 + } + return value_count; //返回数组首地址 +} +//字符串转bool bool CstrToBool(CString str) { if (str == "1") { @@ -78,3 +96,5 @@ bool CstrToBool(CString str) } return 0; } + + diff --git a/Alogrithm/UnitTest/pch.h b/Alogrithm/UnitTest/pch.h index eb25a56..01f0539 100644 --- a/Alogrithm/UnitTest/pch.h +++ b/Alogrithm/UnitTest/pch.h @@ -17,11 +17,14 @@ #include"../Alogrithm/include/2_ExcelSheetColumnTitle.h" #include"../Alogrithm/include/3_bool IsUgly.h" #include"../Alogrithm/include/4_IsPalindrome.h" +#include"../Alogrithm/include/5_MinDepth.h" int CalcCount(int n, char(*str)[10],const char *FileName); int* str_device(CString str, int* value_count); +int str_device2(CString str, char(*return_str)[50]); bool CstrToBool(CString str); + #endif //PCH_H