From 2d04f611c6b05e158e2e99b56f96eba4ce7ae821 Mon Sep 17 00:00:00 2001 From: linyongji Date: Mon, 18 Jan 2021 09:17:13 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=86=E6=94=AF=E4=B8=83=E5=AE=8C=E6=88=90?= =?UTF-8?q?=E4=BA=86=E9=A2=98=E7=9B=AE=20=EF=BC=8C=E5=B9=B6=E5=81=9A?= =?UTF-8?q?=E4=BA=86=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 4 + Alogrithm/.vs/Alogrithm/v16/.suo | Bin 71168 -> 65536 bytes Alogrithm/Alogrithm/Alogrithm.vcxproj | 3 + Alogrithm/Alogrithm/Alogrithm.vcxproj.filters | 9 ++ Alogrithm/Alogrithm/config/7_MaxDepth.ini | 15 ++++ Alogrithm/Alogrithm/include/7_MaxDepth.h | 18 ++++ Alogrithm/Alogrithm/src/7_MaxDepth.cpp | 83 ++++++++++++++++++ Alogrithm/UnitTest/UnitTest.cpp | 20 +++++ Alogrithm/UnitTest/UnitTest.vcxproj | 2 +- Alogrithm/UnitTest/pch.h | 2 +- 10 files changed, 154 insertions(+), 2 deletions(-) create mode 100644 Alogrithm/Alogrithm/config/7_MaxDepth.ini create mode 100644 Alogrithm/Alogrithm/include/7_MaxDepth.h create mode 100644 Alogrithm/Alogrithm/src/7_MaxDepth.cpp diff --git a/.gitignore b/.gitignore index 4f84f06..df973dd 100644 --- a/.gitignore +++ b/.gitignore @@ -104,3 +104,7 @@ Alogrithm/.vs/Alogrithm/v16/ipch/AutoPCH/ab4a44a8d749bee1/6_CONTAINSDUPLICATE.ip >>>>>>> 71fa9786fe524fa393358479cab8105f9239c68c Alogrithm/Alogrithm/Debug/6_ContainsDuplicate.obj Alogrithm/Alogrithm/config/6_ContainsDuplicate.ini.rej +Alogrithm/.vs/Alogrithm/v16/ipch/AutoPCH/8d72cbe9b24557ef/7_MAXDEPTH.ipch +Alogrithm/.vs/Alogrithm/v16/ipch/AutoPCH/d67b185b0905d0e0/7_MAXDEPTH.ipch +Alogrithm/.vs/Alogrithm/v16/TestStore/3/001.testlog +Alogrithm/Alogrithm/Debug/7_MaxDepth.obj diff --git a/Alogrithm/.vs/Alogrithm/v16/.suo b/Alogrithm/.vs/Alogrithm/v16/.suo index 88ad43f7859c823e816ec5afee79854ad759cba7..b3b564a614ef266fd890a2c57275d49791280df3 100644 GIT binary patch delta 6174 zcmeHL3s93+7XI(gA4mw%2tg=d2v1Q1goKxZ5+I^d6+vyWRS`5Qt^(R1_#*Mq+FEVl zKO(K&RScGH)%wbg-LajPs=K3(b<=i7J006acl(&uU8@7OSd~2|B$!rfAG4k5?(Ahg z?z!i_&OPTp=iD3HKEY-a8nc=F(CB1d<}CYSO~yaZvFT9DTlvp8xXw5DPT)9hj}NT$ zDU|kNkzf<1TuD$YMnVk2e^`Qn2|KK3x z61*5e?Id?iv$TT(9z~M73akHAwdI6-8Iov9>Vs+h#81@D31)X&PJ*X zQI4Py6BiCB&phNwrV5wzwi ztXCsYH?rOaB(f23lhw$Ki};2gYfY=SHECHY3S5a)oQ$=%ZK;n;B|``Z(rf@C6hRs! zlg~$Zo1Es{%jCpIlUrS9(cmPH&Ha)7KAY1dZ<`OovBY!R+(nbgiT6V$r!k{Bw6V+P zO~v{DL3-tZ4tj=EqtT=u zD5G!+X_=p8qcstknt#DCSn%C6Q5vpy9=&;u7?MweSHahLtxPg}*AG77Pkn!zP7jaF+IJ!KZZO7R;U~KCU+l?#(m^lZ= zR~rG$iO_j`Aav#n0dcq~eM!Lx@uk7x7>q3olPaq!2Vq(m)Grf>=C~DE$9$WR+JN~j z>Ir3IM_A6)GQ;NZV=tdc(L53CP@evhflV3DMnV+-02o73C2ctTEc7T7SxP_e2=Y$4 zcsduaW3V8dhP>$r(v{>5B+fw7L>+X7gu{uDY`dNpGc7KEPsDHB0psYzV6*1@=A7FzDgNyz_O*Kl1KQi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KuV1>@Hm`iSZT_G@ zV_%+vU>He%7@ug__Cqw*(H}Gdrw#2IZ-{tmxCXa49*`$~+b(wW2Xl+`R`;`FH}!Nk z5})9(h+Q6y4|-??SWft29k46oTzJqj%MUp_xqgy$*ztVFq4+6l$|_PSGWaZ3T@<}m z9L!1d*cZ{^-lYy#Bk`3Ni;fv85ku)|dYrD|WPc-@+Rvm> zuO-J_atz-xw%eomr1;6kuEX-Md)L(?U+!J#Ij+2$$UZ*9ozcBt@a%^iPIssGydYd3 zt3@n{u7}CGcQ-{Hts}>59e!HpGvHDDKVzCSU%DPQn{w$(rQkU&wSLe@4Jons63PEs zd7MIzR^u9@ppQQ{`B%Wg*RE$^wNvMWDem^S4T4Xzdhi34*yhz{Wuh`)0h{T_+C=JkuY%q@yp>-2aFcL(N_1qU(ZSOy zTJ^>Z&)VuR<)C?gYWs8{6+WCmmK#*imDKpzTCyFh`N@yME$T3C{#hZ6lKTUw;WHJL sp4mv|ldH)#<3X|0J1Yh&EuEplFH4PQil}CifoeWd4bGTB;YyMJ4b;daA^-pY diff --git a/Alogrithm/Alogrithm/Alogrithm.vcxproj b/Alogrithm/Alogrithm/Alogrithm.vcxproj index 0733cea..cb9a2f8 100644 --- a/Alogrithm/Alogrithm/Alogrithm.vcxproj +++ b/Alogrithm/Alogrithm/Alogrithm.vcxproj @@ -146,6 +146,7 @@ + @@ -155,6 +156,7 @@ + @@ -163,6 +165,7 @@ + diff --git a/Alogrithm/Alogrithm/Alogrithm.vcxproj.filters b/Alogrithm/Alogrithm/Alogrithm.vcxproj.filters index 34e6ca5..9c8452f 100644 --- a/Alogrithm/Alogrithm/Alogrithm.vcxproj.filters +++ b/Alogrithm/Alogrithm/Alogrithm.vcxproj.filters @@ -45,6 +45,9 @@ 源文件\src + + 源文件\src + @@ -65,6 +68,9 @@ 头文件\include + + 头文件\include + @@ -85,5 +91,8 @@ 资源文件\config + + 资源文件\config + \ No newline at end of file diff --git a/Alogrithm/Alogrithm/config/7_MaxDepth.ini b/Alogrithm/Alogrithm/config/7_MaxDepth.ini new file mode 100644 index 0000000..c0820a8 --- /dev/null +++ b/Alogrithm/Alogrithm/config/7_MaxDepth.ini @@ -0,0 +1,15 @@ +[Test1] +input=3,9,20,NULL,NULL,15,7 +output=3 +[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 diff --git a/Alogrithm/Alogrithm/include/7_MaxDepth.h b/Alogrithm/Alogrithm/include/7_MaxDepth.h new file mode 100644 index 0000000..3299e7b --- /dev/null +++ b/Alogrithm/Alogrithm/include/7_MaxDepth.h @@ -0,0 +1,18 @@ +#pragma once +#include +#include +#include + + +struct TreeNode2 +{ + int val; + struct TreeNode2* left; + struct TreeNode2* right; +}; + + +int MaxDepth(struct TreeNode2* root); +TreeNode2* CreatBitTree2(char str[][50], int return_count); +void CreatBitTreeNode2(char str[][50], int return_count, TreeNode2* cur, int curIndex); +void free_tree2(TreeNode2* T); diff --git a/Alogrithm/Alogrithm/src/7_MaxDepth.cpp b/Alogrithm/Alogrithm/src/7_MaxDepth.cpp new file mode 100644 index 0000000..52af908 --- /dev/null +++ b/Alogrithm/Alogrithm/src/7_MaxDepth.cpp @@ -0,0 +1,83 @@ +#include "../include/7_MaxDepth.h" + +TreeNode2* CreatBitTree2(char str[][50], int return_count) +{ + if (str == NULL || return_count <= 0) { + return NULL; + } + TreeNode2* head = (TreeNode2*)malloc(sizeof(TreeNode2)); + if (NULL == head) { + return NULL; + } + head->val = _ttoi(str[0]); + CreatBitTreeNode2(str, return_count, head, 0); + return head; +} +void CreatBitTreeNode2(char str[][50], int return_count, TreeNode2* 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 = (TreeNode2*)malloc(sizeof(TreeNode2)); + 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 = (TreeNode2*)malloc(sizeof(TreeNode2)); + if (NULL == cur->right) { + return; + } + cur->right->val = _ttoi(str[2 * curIndex + 2]); + } + else { + cur->right = NULL; + } + CreatBitTreeNode2(str, return_count, cur->left, 2 * curIndex + 1); + CreatBitTreeNode2(str, return_count, cur->right, 2 * curIndex + 2); +} +int MaxDepth(struct TreeNode2* 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 = MaxDepth(root->left); + } + else if ((NULL == root->left) && (NULL != root->right)) { + Depth = MaxDepth(root->right); + } + else { + L1 = MaxDepth(root->left); + L2 = MaxDepth(root->right); + Depth = fmax(L1, L2); + } + return Depth + 1; +} +void free_tree2(TreeNode2* T)//ͷ +{ + if (!T) { + return; + } + if (T->left) { + free_tree2(T->left); + } + if (T->right) { + free_tree2(T->right); + } + if (T) { + free(T); + T = NULL; + } +} \ No newline at end of file diff --git a/Alogrithm/UnitTest/UnitTest.cpp b/Alogrithm/UnitTest/UnitTest.cpp index 68f3a8b..86c93e6 100644 --- a/Alogrithm/UnitTest/UnitTest.cpp +++ b/Alogrithm/UnitTest/UnitTest.cpp @@ -8,6 +8,7 @@ using namespace Microsoft::VisualStudio::CppUnitTestFramework; #define FileName_4 "../Alogrithm/config/4_IsPalindrome.ini" #define FileName_5 "../Alogrithm/config/5_MinDepth.ini" #define FileName_6 "../Alogrithm/config/6_ContainsDuplicate.ini" +#define FileName_7 "../Alogrithm/config/7_MaxDepth.ini" namespace UnitTest { @@ -112,4 +113,23 @@ namespace UnitTest } } }; + TEST_CLASS(UnitTest_7) + { + TEST_METHOD(TestMethode1) + { + char Section_Name[100][10] = { 0 }; + int Section_Count = CalcCount(100, Section_Name, FileName_7); + CString Na, nExpect; + for (int i = 0; i < Section_Count; i++) { + GetPrivateProfileString(Section_Name[i], "input", " ", Na.GetBuffer(200), 200, FileName_7); + GetPrivateProfileString(Section_Name[i], "output", " ", nExpect.GetBuffer(20), 20, FileName_7); + char return_str[100][50] = { 0 }; + int return_count = str_device2(Na, return_str); + TreeNode2* root = CreatBitTree2(return_str, return_count); + int nReal = MaxDepth(root); + Assert::AreEqual(nReal, _ttoi(nExpect)); + free_tree2(root); + } + } + }; } diff --git a/Alogrithm/UnitTest/UnitTest.vcxproj b/Alogrithm/UnitTest/UnitTest.vcxproj index 5284fc8..9573ad6 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;../Alogrithm/Debug/5_MinDepth.obj;../Alogrithm/Debug/6_ContainsDuplicate.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;../Alogrithm/Debug/6_ContainsDuplicate.obj;../Alogrithm/Debug/7_MaxDepth.obj;%(AdditionalDependencies) diff --git a/Alogrithm/UnitTest/pch.h b/Alogrithm/UnitTest/pch.h index 55a32ff..63a8b83 100644 --- a/Alogrithm/UnitTest/pch.h +++ b/Alogrithm/UnitTest/pch.h @@ -19,7 +19,7 @@ #include"../Alogrithm/include/4_IsPalindrome.h" #include"../Alogrithm/include/5_MinDepth.h" #include"../Alogrithm/include/6_ContainsDuplicate.h" - +#include"../Alogrithm/include/7_MaxDepth.h" int CalcCount(int n, char(*str)[10],const char *FileName);