├── .gitattributes ├── .gitignore ├── IntroductionToAlgorithms.sln ├── IntroductionToAlgorithms ├── IntroductionToAlgorithms.vcxproj ├── IntroductionToAlgorithms.vcxproj.filters ├── b-tree.txt ├── base.h ├── btree │ └── 124.txt ├── main.c ├── 第02章 算法基础 │ ├── 02_1_插入排序.h │ ├── 02_2_分析算法.h │ └── 02_3_设计算法.h ├── 第04章 分治策略 │ ├── 04_1_最大子数组问题.h │ └── 04_2_矩阵乘法的Strassen算法.h ├── 第06章 堆排序 │ ├── 06_1_堆_2_堆的性质.h │ ├── 06_3_建堆.h │ ├── 06_4_堆排序算法.h │ └── 06_5_优先队列.h ├── 第07章 快速排序 │ ├── 07_1_快速排序的描述.h │ ├── 07_3_快速排序的随机化版本.h │ └── 07_习题.h ├── 第08章 线性时间排序 │ ├── 08_2_计数排序.h │ └── 08_4_桶排序.h ├── 第09章 中位数和顺序统计量 │ ├── 09_1_最大值和最小值.h │ └── 09_2_期望为线性时间的选择算法.h ├── 第10章 基本数据结构 │ ├── 10_1_栈和队列.h │ └── 10_2_链表.h ├── 第11章 散列表 │ ├── 11_1_直接寻址表.h │ ├── 11_2_散列表.h │ └── 11_3_散列函数.h ├── 第12章 二叉搜索树 │ ├── 12_1_建立_2_查询BST.h │ ├── 12_3_插入和删除.h │ └── test.h ├── 第13章 红黑树 │ └── 13_红黑树.h ├── 第15章 动态规划 │ ├── 15_1_钢条切割.h │ ├── 15_4_最长公共子序列.h │ └── 15_5_最优二叉树.h ├── 第16章 贪心算法 │ ├── 16_1_活动选择问题.h │ ├── 16_2_贪心算法原理.h │ └── 16_3_哈夫曼编码.h ├── 第18章 B树 │ └── 18_2_B树上的基本操作_18_3_删除.h ├── 第19章 斐波那契堆 │ └── 19_斐波那契堆.h ├── 第22章 基本的图算法 │ ├── 22_1_图的表示.h │ ├── 22_2_广度优先搜索.h │ ├── 22_3_深度优先搜索.h │ └── 22_5_强连通分量.h ├── 第23章 最小生成树 │ ├── 23_2_Kruskal算法.h │ └── 23_2_Prim算法.h ├── 第24章 单源最短路径 │ ├── 24_2_Bellman-Ford算法.h │ ├── 24_3_Dijkstra算法.h │ └── 松弛操作.h └── 第32章 字符串匹配 │ ├── 32_1_朴素字符串匹配算法.h │ ├── 32_2_Rabin-Karp算法.h │ └── 32_4_Hnuth-Morris-Pratt算法.h └── README.md /.gitattributes: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # Set default behavior to automatically normalize line endings. 3 | ############################################################################### 4 | * text=auto 5 | 6 | ############################################################################### 7 | # Set default behavior for command prompt diff. 8 | # 9 | # This is need for earlier builds of msysgit that does not have it on by 10 | # default for csharp files. 11 | # Note: This is only used by command line 12 | ############################################################################### 13 | #*.cs diff=csharp 14 | 15 | ############################################################################### 16 | # Set the merge driver for project and solution files 17 | # 18 | # Merging from the command prompt will add diff markers to the files if there 19 | # are conflicts (Merging from VS is not affected by the settings below, in VS 20 | # the diff markers are never inserted). Diff markers may cause the following 21 | # file extensions to fail to load in VS. An alternative would be to treat 22 | # these files as binary and thus will always conflict and require user 23 | # intervention with every merge. To do so, just uncomment the entries below 24 | ############################################################################### 25 | #*.sln merge=binary 26 | #*.csproj merge=binary 27 | #*.vbproj merge=binary 28 | #*.vcxproj merge=binary 29 | #*.vcproj merge=binary 30 | #*.dbproj merge=binary 31 | #*.fsproj merge=binary 32 | #*.lsproj merge=binary 33 | #*.wixproj merge=binary 34 | #*.modelproj merge=binary 35 | #*.sqlproj merge=binary 36 | #*.wwaproj merge=binary 37 | 38 | ############################################################################### 39 | # behavior for image files 40 | # 41 | # image files are treated as binary by default. 42 | ############################################################################### 43 | #*.jpg binary 44 | #*.png binary 45 | #*.gif binary 46 | 47 | ############################################################################### 48 | # diff behavior for common document formats 49 | # 50 | # Convert binary document formats to text before diffing them. This feature 51 | # is only available from the command line. Turn it on by uncommenting the 52 | # entries below. 53 | ############################################################################### 54 | #*.doc diff=astextplain 55 | #*.DOC diff=astextplain 56 | #*.docx diff=astextplain 57 | #*.DOCX diff=astextplain 58 | #*.dot diff=astextplain 59 | #*.DOT diff=astextplain 60 | #*.pdf diff=astextplain 61 | #*.PDF diff=astextplain 62 | #*.rtf diff=astextplain 63 | #*.RTF diff=astextplain 64 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | 4 | # User-specific files 5 | *.suo 6 | *.user 7 | *.userosscache 8 | *.sln.docstates 9 | 10 | # User-specific files (MonoDevelop/Xamarin Studio) 11 | *.userprefs 12 | 13 | # Build results 14 | [Dd]ebug/ 15 | [Dd]ebugPublic/ 16 | [Rr]elease/ 17 | [Rr]eleases/ 18 | x64/ 19 | x86/ 20 | bld/ 21 | [Bb]in/ 22 | [Oo]bj/ 23 | [Ll]og/ 24 | 25 | # Visual Studio 2015 cache/options directory 26 | .vs/ 27 | # Uncomment if you have tasks that create the project's static files in wwwroot 28 | #wwwroot/ 29 | 30 | # MSTest test Results 31 | [Tt]est[Rr]esult*/ 32 | [Bb]uild[Ll]og.* 33 | 34 | # NUNIT 35 | *.VisualState.xml 36 | TestResult.xml 37 | 38 | # Build Results of an ATL Project 39 | [Dd]ebugPS/ 40 | [Rr]eleasePS/ 41 | dlldata.c 42 | 43 | # DNX 44 | project.lock.json 45 | project.fragment.lock.json 46 | artifacts/ 47 | 48 | *_i.c 49 | *_p.c 50 | *_i.h 51 | *.ilk 52 | *.meta 53 | *.obj 54 | *.pch 55 | *.pdb 56 | *.pgc 57 | *.pgd 58 | *.rsp 59 | *.sbr 60 | *.tlb 61 | *.tli 62 | *.tlh 63 | *.tmp 64 | *.tmp_proj 65 | *.log 66 | *.vspscc 67 | *.vssscc 68 | .builds 69 | *.pidb 70 | *.svclog 71 | *.scc 72 | 73 | # Chutzpah Test files 74 | _Chutzpah* 75 | 76 | # Visual C++ cache files 77 | ipch/ 78 | *.aps 79 | *.ncb 80 | *.opendb 81 | *.opensdf 82 | *.sdf 83 | *.cachefile 84 | *.VC.db 85 | *.VC.VC.opendb 86 | 87 | # Visual Studio profiler 88 | *.psess 89 | *.vsp 90 | *.vspx 91 | *.sap 92 | 93 | # TFS 2012 Local Workspace 94 | $tf/ 95 | 96 | # Guidance Automation Toolkit 97 | *.gpState 98 | 99 | # ReSharper is a .NET coding add-in 100 | _ReSharper*/ 101 | *.[Rr]e[Ss]harper 102 | *.DotSettings.user 103 | 104 | # JustCode is a .NET coding add-in 105 | .JustCode 106 | 107 | # TeamCity is a build add-in 108 | _TeamCity* 109 | 110 | # DotCover is a Code Coverage Tool 111 | *.dotCover 112 | 113 | # NCrunch 114 | _NCrunch_* 115 | .*crunch*.local.xml 116 | nCrunchTemp_* 117 | 118 | # MightyMoose 119 | *.mm.* 120 | AutoTest.Net/ 121 | 122 | # Web workbench (sass) 123 | .sass-cache/ 124 | 125 | # Installshield output folder 126 | [Ee]xpress/ 127 | 128 | # DocProject is a documentation generator add-in 129 | DocProject/buildhelp/ 130 | DocProject/Help/*.HxT 131 | DocProject/Help/*.HxC 132 | DocProject/Help/*.hhc 133 | DocProject/Help/*.hhk 134 | DocProject/Help/*.hhp 135 | DocProject/Help/Html2 136 | DocProject/Help/html 137 | 138 | # Click-Once directory 139 | publish/ 140 | 141 | # Publish Web Output 142 | *.[Pp]ublish.xml 143 | *.azurePubxml 144 | # TODO: Comment the next line if you want to checkin your web deploy settings 145 | # but database connection strings (with potential passwords) will be unencrypted 146 | #*.pubxml 147 | *.publishproj 148 | 149 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 150 | # checkin your Azure Web App publish settings, but sensitive information contained 151 | # in these scripts will be unencrypted 152 | PublishScripts/ 153 | 154 | # NuGet Packages 155 | *.nupkg 156 | # The packages folder can be ignored because of Package Restore 157 | **/packages/* 158 | # except build/, which is used as an MSBuild target. 159 | !**/packages/build/ 160 | # Uncomment if necessary however generally it will be regenerated when needed 161 | #!**/packages/repositories.config 162 | # NuGet v3's project.json files produces more ignoreable files 163 | *.nuget.props 164 | *.nuget.targets 165 | 166 | # Microsoft Azure Build Output 167 | csx/ 168 | *.build.csdef 169 | 170 | # Microsoft Azure Emulator 171 | ecf/ 172 | rcf/ 173 | 174 | # Windows Store app package directories and files 175 | AppPackages/ 176 | BundleArtifacts/ 177 | Package.StoreAssociation.xml 178 | _pkginfo.txt 179 | 180 | # Visual Studio cache files 181 | # files ending in .cache can be ignored 182 | *.[Cc]ache 183 | # but keep track of directories ending in .cache 184 | !*.[Cc]ache/ 185 | 186 | # Others 187 | ClientBin/ 188 | ~$* 189 | *~ 190 | *.dbmdl 191 | *.dbproj.schemaview 192 | *.jfm 193 | *.pfx 194 | *.publishsettings 195 | node_modules/ 196 | orleans.codegen.cs 197 | 198 | # Since there are multiple workflows, uncomment next line to ignore bower_components 199 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 200 | #bower_components/ 201 | 202 | # RIA/Silverlight projects 203 | Generated_Code/ 204 | 205 | # Backup & report files from converting an old project file 206 | # to a newer Visual Studio version. Backup files are not needed, 207 | # because we have git ;-) 208 | _UpgradeReport_Files/ 209 | Backup*/ 210 | UpgradeLog*.XML 211 | UpgradeLog*.htm 212 | 213 | # SQL Server files 214 | *.mdf 215 | *.ldf 216 | 217 | # Business Intelligence projects 218 | *.rdl.data 219 | *.bim.layout 220 | *.bim_*.settings 221 | 222 | # Microsoft Fakes 223 | FakesAssemblies/ 224 | 225 | # GhostDoc plugin setting file 226 | *.GhostDoc.xml 227 | 228 | # Node.js Tools for Visual Studio 229 | .ntvs_analysis.dat 230 | 231 | # Visual Studio 6 build log 232 | *.plg 233 | 234 | # Visual Studio 6 workspace options file 235 | *.opt 236 | 237 | # Visual Studio LightSwitch build output 238 | **/*.HTMLClient/GeneratedArtifacts 239 | **/*.DesktopClient/GeneratedArtifacts 240 | **/*.DesktopClient/ModelManifest.xml 241 | **/*.Server/GeneratedArtifacts 242 | **/*.Server/ModelManifest.xml 243 | _Pvt_Extensions 244 | 245 | # Paket dependency manager 246 | .paket/paket.exe 247 | paket-files/ 248 | 249 | # FAKE - F# Make 250 | .fake/ 251 | 252 | # JetBrains Rider 253 | .idea/ 254 | *.sln.iml 255 | 256 | # CodeRush 257 | .cr/ 258 | 259 | # Python Tools for Visual Studio (PTVS) 260 | __pycache__/ 261 | *.pyc -------------------------------------------------------------------------------- /IntroductionToAlgorithms.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 15 4 | VisualStudioVersion = 15.0.27130.2036 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "IntroductionToAlgorithms", "IntroductionToAlgorithms\IntroductionToAlgorithms.vcxproj", "{A599420D-E478-4ADE-A7BF-70B75509DC62}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|x64 = Debug|x64 11 | Debug|x86 = Debug|x86 12 | Release|x64 = Release|x64 13 | Release|x86 = Release|x86 14 | EndGlobalSection 15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 16 | {A599420D-E478-4ADE-A7BF-70B75509DC62}.Debug|x64.ActiveCfg = Debug|x64 17 | {A599420D-E478-4ADE-A7BF-70B75509DC62}.Debug|x64.Build.0 = Debug|x64 18 | {A599420D-E478-4ADE-A7BF-70B75509DC62}.Debug|x86.ActiveCfg = Debug|Win32 19 | {A599420D-E478-4ADE-A7BF-70B75509DC62}.Debug|x86.Build.0 = Debug|Win32 20 | {A599420D-E478-4ADE-A7BF-70B75509DC62}.Release|x64.ActiveCfg = Release|x64 21 | {A599420D-E478-4ADE-A7BF-70B75509DC62}.Release|x64.Build.0 = Release|x64 22 | {A599420D-E478-4ADE-A7BF-70B75509DC62}.Release|x86.ActiveCfg = Release|Win32 23 | {A599420D-E478-4ADE-A7BF-70B75509DC62}.Release|x86.Build.0 = Release|Win32 24 | EndGlobalSection 25 | GlobalSection(SolutionProperties) = preSolution 26 | HideSolutionNode = FALSE 27 | EndGlobalSection 28 | GlobalSection(ExtensibilityGlobals) = postSolution 29 | SolutionGuid = {BA3EF7EC-7455-4CAC-B7B8-BDE9B3E203A4} 30 | EndGlobalSection 31 | EndGlobal 32 | -------------------------------------------------------------------------------- /IntroductionToAlgorithms/IntroductionToAlgorithms.vcxproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | Debug 14 | x64 15 | 16 | 17 | Release 18 | x64 19 | 20 | 21 | 22 | 15.0 23 | {A599420D-E478-4ADE-A7BF-70B75509DC62} 24 | Win32Proj 25 | IntroductionToAlgorithms 26 | 10.0.16299.0 27 | 28 | 29 | 30 | Application 31 | true 32 | v141 33 | Unicode 34 | 35 | 36 | Application 37 | false 38 | v141 39 | true 40 | Unicode 41 | 42 | 43 | Application 44 | true 45 | v141 46 | Unicode 47 | 48 | 49 | Application 50 | false 51 | v141 52 | true 53 | Unicode 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | true 75 | 76 | 77 | true 78 | 79 | 80 | false 81 | 82 | 83 | false 84 | 85 | 86 | 87 | NotUsing 88 | Level3 89 | Disabled 90 | false 91 | WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) 92 | true 93 | 94 | 95 | Console 96 | true 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | false 106 | 107 | 108 | 109 | 110 | NotUsing 111 | Level3 112 | Disabled 113 | true 114 | _DEBUG;_CONSOLE;%(PreprocessorDefinitions) 115 | true 116 | true 117 | 118 | 119 | Console 120 | true 121 | 122 | 123 | 124 | 125 | Use 126 | Level3 127 | MaxSpeed 128 | true 129 | true 130 | true 131 | WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 132 | true 133 | 134 | 135 | Console 136 | true 137 | true 138 | true 139 | 140 | 141 | 142 | 143 | Use 144 | Level3 145 | MaxSpeed 146 | true 147 | true 148 | true 149 | NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 150 | true 151 | 152 | 153 | Console 154 | true 155 | true 156 | true 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | 166 | 167 | 168 | 169 | 170 | 171 | 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | 185 | 186 | 187 | 188 | 189 | 190 | 191 | 192 | 193 | 194 | 195 | 196 | 197 | 198 | 199 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 209 | 210 | 211 | 212 | 213 | -------------------------------------------------------------------------------- /IntroductionToAlgorithms/IntroductionToAlgorithms.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | 头文件 20 | 21 | 22 | 头文件 23 | 24 | 25 | 头文件 26 | 27 | 28 | 头文件 29 | 30 | 31 | 头文件 32 | 33 | 34 | 头文件 35 | 36 | 37 | 头文件 38 | 39 | 40 | 头文件 41 | 42 | 43 | 头文件 44 | 45 | 46 | 头文件 47 | 48 | 49 | 头文件 50 | 51 | 52 | 头文件 53 | 54 | 55 | 头文件 56 | 57 | 58 | 头文件 59 | 60 | 61 | 头文件 62 | 63 | 64 | 头文件 65 | 66 | 67 | 头文件 68 | 69 | 70 | 头文件 71 | 72 | 73 | 头文件 74 | 75 | 76 | 头文件 77 | 78 | 79 | 头文件 80 | 81 | 82 | 头文件 83 | 84 | 85 | 头文件 86 | 87 | 88 | 头文件 89 | 90 | 91 | 头文件 92 | 93 | 94 | 头文件 95 | 96 | 97 | 头文件 98 | 99 | 100 | 头文件 101 | 102 | 103 | 头文件 104 | 105 | 106 | 头文件 107 | 108 | 109 | 头文件 110 | 111 | 112 | 头文件 113 | 114 | 115 | 头文件 116 | 117 | 118 | 头文件 119 | 120 | 121 | 头文件 122 | 123 | 124 | 头文件 125 | 126 | 127 | 头文件 128 | 129 | 130 | 头文件 131 | 132 | 133 | 头文件 134 | 135 | 136 | 头文件 137 | 138 | 139 | 头文件 140 | 141 | 142 | 头文件 143 | 144 | 145 | 头文件 146 | 147 | 148 | 头文件 149 | 150 | 151 | 头文件 152 | 153 | 154 | 155 | 156 | 源文件 157 | 158 | 159 | -------------------------------------------------------------------------------- /IntroductionToAlgorithms/b-tree.txt: -------------------------------------------------------------------------------- 1 | hello world! 2 | hello world! 3 | hello world! 4 | hello world! 5 | hello world! 6 | hello world! 7 | hello world! 8 | hello world! 9 | -------------------------------------------------------------------------------- /IntroductionToAlgorithms/base.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef BASE 3 | #define BASE 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #define SIZE 50 10 | 11 | #define MAX(a,b) ((a)>(b)?(a):(b)) 12 | #define MIN(a,b) ((a)<(b)?(a):(b)) 13 | 14 | int Random(int a, int b) 15 | { 16 | srand((unsigned)time(NULL)); 17 | return rand() % (b - a) + a; 18 | } 19 | 20 | #endif // !BASE 21 | 22 | 23 | -------------------------------------------------------------------------------- /IntroductionToAlgorithms/btree/124.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ting1e/Introduction-To-Algorithms/312cc9c51864175581089f329a41c120cc5c591d/IntroductionToAlgorithms/btree/124.txt -------------------------------------------------------------------------------- /IntroductionToAlgorithms/main.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ting1e/Introduction-To-Algorithms/312cc9c51864175581089f329a41c120cc5c591d/IntroductionToAlgorithms/main.c -------------------------------------------------------------------------------- /IntroductionToAlgorithms/第02章 算法基础/02_1_插入排序.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ting1e/Introduction-To-Algorithms/312cc9c51864175581089f329a41c120cc5c591d/IntroductionToAlgorithms/第02章 算法基础/02_1_插入排序.h -------------------------------------------------------------------------------- /IntroductionToAlgorithms/第02章 算法基础/02_2_分析算法.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ting1e/Introduction-To-Algorithms/312cc9c51864175581089f329a41c120cc5c591d/IntroductionToAlgorithms/第02章 算法基础/02_2_分析算法.h -------------------------------------------------------------------------------- /IntroductionToAlgorithms/第02章 算法基础/02_3_设计算法.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ting1e/Introduction-To-Algorithms/312cc9c51864175581089f329a41c120cc5c591d/IntroductionToAlgorithms/第02章 算法基础/02_3_设计算法.h -------------------------------------------------------------------------------- /IntroductionToAlgorithms/第04章 分治策略/04_1_最大子数组问题.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "..\base.h" 3 | 4 | //本节几乎所有函数都返回一个指针,使用完成后都应该被手动释放 5 | 6 | /* 7 | 问题描述:给出一个数组,数组中的值有正有负,找出数组 8 | 中的子数组,子数组各个值的和应大于任何其他子数组(可能有多个), 9 | 10 | 分治策略: 11 | 找到子数组中间的位置mid,最大子数组必定位于这三种情况之一: 12 | 1.在start到mid之间,2.在mid+1到end之间,3.在start到end之间且跨越mid 13 | */ 14 | 15 | //找到第三种情况的的结果 该函数返回一个指向有三个int的数组的指针 16 | //(*ret)[0] 最大子数组的左边界 17 | //(*ret)[1] 最大子数组的右边界 18 | //(*ret)[2] 最大子数组各个值得和 19 | int (*FindMaxCrossingSubArray(int array[], int start, int middle, int end))[3] 20 | { 21 | int(*ret_val)[3] = (int(*)[3])malloc((sizeof(int)) * 3); 22 | int i; 23 | int sum = 0, left_sum = array[middle], right_sum = array[middle+1]; 24 | (*ret_val)[0] = middle; 25 | for (i = middle; i >= start; i--) 26 | { 27 | sum += array[i]; 28 | if (sum > left_sum) 29 | { 30 | left_sum = sum; 31 | (*ret_val)[0] = i; 32 | } 33 | } 34 | 35 | sum = 0; 36 | (*ret_val)[1] = middle; 37 | for (int i = middle+1; i <=end; i++) 38 | { 39 | sum += array[i]; 40 | if (sum > right_sum) 41 | { 42 | right_sum = sum; 43 | (*ret_val)[1] = i; 44 | } 45 | } 46 | (*ret_val)[2] = right_sum + left_sum; 47 | return ret_val; 48 | } 49 | 50 | //获得最大子数组的分治算法主体 只返回一个最大子数组 51 | //(*ret)[0] 最大子数组的左边界 52 | //(*ret)[1] 最大子数组的右边界 53 | //(*ret)[2] 最大子数组各个值得和 54 | int(*FindMaxSubArray(int array[], int start,int end))[3] 55 | { 56 | if (start == end) 57 | { 58 | int(*ret_val)[3] = (int(*)[3])malloc((sizeof(int)) * 3); 59 | (*ret_val)[0] = start; 60 | (*ret_val)[1] = end; 61 | (*ret_val)[2] = array[start]; 62 | return ret_val; 63 | } 64 | else 65 | { 66 | int middle = (start + end) / 2; 67 | int(*max_left)[3], (*max_right)[3], (*max_cross)[3]; 68 | max_left = FindMaxSubArray(array, start, middle); 69 | max_right = FindMaxSubArray(array, middle + 1, end); //0.0 70 | max_cross = FindMaxCrossingSubArray(array, start, middle, end); 71 | if ((*max_left)[2] >= (*max_right)[2]) 72 | { 73 | free(max_right); 74 | if ((*max_left)[2] >= (*max_cross)[2]) 75 | { 76 | free(max_cross); 77 | return max_left; 78 | } 79 | else 80 | { 81 | free(max_left); 82 | return max_cross; 83 | } 84 | } 85 | else 86 | { 87 | free(max_left); 88 | if ((*max_right)[2] >= (*max_cross)[2]) 89 | { 90 | free(max_cross); 91 | return max_right; 92 | } 93 | else 94 | { 95 | free(max_right); 96 | return max_cross; 97 | } 98 | } 99 | 100 | } 101 | } 102 | 103 | /*练习4.1-2 暴力膜 两个思想: 104 | 1. 运用之前的FindMaxCrossingSubArray()函数。然后middle值从第一个一直到最后一个 105 | 时间复杂度为n*n=n²; 106 | 2.排列问题 时间复杂度为至少大于n²,应该比第一种复杂 107 | */ 108 | int(*FindMaxSubArrayForce1(int array[], int start, int end))[3] 109 | { 110 | int(*temp)[3]; 111 | int(*max)[3] = (int(*)[3])malloc((sizeof(int)) * 3); 112 | (*max)[2] = 0; 113 | for (int i = start; i <= end; i++) 114 | { 115 | temp = FindMaxCrossingSubArray(array, start, i, end); 116 | if ((*temp)[2] > (*max)[2]) 117 | { 118 | free(max); 119 | max = temp; 120 | } 121 | } 122 | return max; 123 | } 124 | int(*FindMaxSubArrayForce2(int array[], int start, int end))[3] 125 | { 126 | int(*max)[3] = (int(*)[3])malloc((sizeof(int)) * 3); 127 | (*max)[2] = array[start]; 128 | int sum ; 129 | for (int i = start; i <= end; i++) 130 | { 131 | for (int j = i; j <= end; j++) 132 | { 133 | sum = 0; 134 | for (int k = i; k <= j; k++) 135 | { 136 | sum += array[k]; 137 | } 138 | if (sum > (*max)[2]) 139 | { 140 | (*max)[2] = sum; 141 | (*max)[0] = i; 142 | (*max)[1] = j; 143 | } 144 | } 145 | } 146 | return max; 147 | } 148 | //练习4.1 - 5 149 | /* 150 | 思想:由左至右处理,记录目前为止已经处理过的最大子数组,若已知A【1。。j】的最大子数组, 151 | A【1.。J+1】的最大子数组要么是已知的A【1。。j】的最大子数组,要么是某个右边界为j+1的某个 152 | 子数组。 153 | */ 154 | 155 | 156 | int(*FindMaxSubArrayLoop(int array[], int start, int end))[3] 157 | { 158 | int(*max)[3] = (int(*)[3])malloc((sizeof(int)) * 3); 159 | int left,sum; 160 | int temp_max; 161 | (*max)[0] = start; 162 | (*max)[1] = start; 163 | (*max)[2] = array[start]; 164 | for (int i = start; i < end; i++) 165 | { 166 | sum = 0; 167 | temp_max = array[i + 1]; 168 | left = i + 1; 169 | for (int j = i+1; j >= start; j--) 170 | { 171 | sum += array[j]; 172 | if (sum > temp_max) 173 | { 174 | temp_max = sum; 175 | left = j; 176 | } 177 | } 178 | if (temp_max > (*max)[2]) 179 | { 180 | (*max)[0] = left; 181 | (*max)[1] = i + 1; 182 | (*max)[2] = temp_max; 183 | } 184 | } 185 | return max; 186 | } 187 | 188 | //获取最大子串简单测试 189 | void FindMaxSubArrayTest() 190 | { 191 | int a[] = { 13,-3,-25,20,-3,-16,-23,18,20,-7,12,-5,-22,15,-4,7 }; 192 | int(*val)[3] = FindMaxSubArray(a, 0, 14); 193 | free(val); 194 | } 195 | /* 196 | 练习4.1-1 返回数组中最大的一个负数 197 | 198 | 练习4.1-4 判断子数组的大小如果小于0,则返回空子数组 199 | 200 | */ -------------------------------------------------------------------------------- /IntroductionToAlgorithms/第04章 分治策略/04_2_矩阵乘法的Strassen算法.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ting1e/Introduction-To-Algorithms/312cc9c51864175581089f329a41c120cc5c591d/IntroductionToAlgorithms/第04章 分治策略/04_2_矩阵乘法的Strassen算法.h -------------------------------------------------------------------------------- /IntroductionToAlgorithms/第06章 堆排序/06_1_堆_2_堆的性质.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ting1e/Introduction-To-Algorithms/312cc9c51864175581089f329a41c120cc5c591d/IntroductionToAlgorithms/第06章 堆排序/06_1_堆_2_堆的性质.h -------------------------------------------------------------------------------- /IntroductionToAlgorithms/第06章 堆排序/06_3_建堆.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ting1e/Introduction-To-Algorithms/312cc9c51864175581089f329a41c120cc5c591d/IntroductionToAlgorithms/第06章 堆排序/06_3_建堆.h -------------------------------------------------------------------------------- /IntroductionToAlgorithms/第06章 堆排序/06_4_堆排序算法.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ting1e/Introduction-To-Algorithms/312cc9c51864175581089f329a41c120cc5c591d/IntroductionToAlgorithms/第06章 堆排序/06_4_堆排序算法.h -------------------------------------------------------------------------------- /IntroductionToAlgorithms/第06章 堆排序/06_5_优先队列.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ting1e/Introduction-To-Algorithms/312cc9c51864175581089f329a41c120cc5c591d/IntroductionToAlgorithms/第06章 堆排序/06_5_优先队列.h -------------------------------------------------------------------------------- /IntroductionToAlgorithms/第07章 快速排序/07_1_快速排序的描述.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ting1e/Introduction-To-Algorithms/312cc9c51864175581089f329a41c120cc5c591d/IntroductionToAlgorithms/第07章 快速排序/07_1_快速排序的描述.h -------------------------------------------------------------------------------- /IntroductionToAlgorithms/第07章 快速排序/07_3_快速排序的随机化版本.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ting1e/Introduction-To-Algorithms/312cc9c51864175581089f329a41c120cc5c591d/IntroductionToAlgorithms/第07章 快速排序/07_3_快速排序的随机化版本.h -------------------------------------------------------------------------------- /IntroductionToAlgorithms/第07章 快速排序/07_习题.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ting1e/Introduction-To-Algorithms/312cc9c51864175581089f329a41c120cc5c591d/IntroductionToAlgorithms/第07章 快速排序/07_习题.h -------------------------------------------------------------------------------- /IntroductionToAlgorithms/第08章 线性时间排序/08_2_计数排序.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ting1e/Introduction-To-Algorithms/312cc9c51864175581089f329a41c120cc5c591d/IntroductionToAlgorithms/第08章 线性时间排序/08_2_计数排序.h -------------------------------------------------------------------------------- /IntroductionToAlgorithms/第08章 线性时间排序/08_4_桶排序.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "..\base.h" 3 | 4 | typedef struct ELEMENT 5 | { 6 | double x; 7 | struct ELEMENT *next; 8 | }Element; 9 | 10 | void InsertAnElement(Element *e, double a) //链表尾部插入一个数据 11 | { 12 | while (e->next) 13 | e = e->next; 14 | Element *new_one = (Element *)malloc(sizeof(Element)); 15 | new_one->next = NULL; 16 | new_one->x = a; 17 | e->next = new_one; 18 | } 19 | 20 | void InsertSort(Element *e) //链表插入排序 21 | { 22 | if (e->next == NULL) 23 | return; 24 | Element *current = e->next->next; 25 | e->next->next = NULL; 26 | while (current) 27 | { 28 | Element *target = e; 29 | while (target->next!=NULL&&target->next->xx) 30 | { 31 | target = target->next; 32 | } 33 | Element *temp = current; 34 | current = current->next; 35 | temp->next = target->next; 36 | target->next = temp; 37 | } 38 | } 39 | 40 | 41 | /* 42 | 桶排序: 43 | 将【0,1】区间划分为n个相同大小的子区间或称为桶,然后将这n个数放到各个桶中 44 | */ 45 | void BucketSort(double a[], int size) 46 | { 47 | Element *b = (Element *)malloc(sizeof(Element)*size); 48 | for (int i = 0; i < size; i++) 49 | { 50 | b[i].next = NULL; 51 | } 52 | for (int i = 0; i < size; i++) 53 | { 54 | InsertAnElement(&b[(int)(a[i] * size)], a[i]); 55 | } 56 | for (int i = 0; i < size; i++) 57 | { 58 | InsertSort(&b[(int)(a[i] * size)]); 59 | } 60 | int j = 0; 61 | for (int i = 0; i < size; i++) 62 | { 63 | Element *temp = b[i].next; 64 | while (temp!=NULL) 65 | { 66 | a[j++] = temp->x; 67 | temp = temp->next; 68 | } 69 | } 70 | } 71 | 72 | void BucketSortTest() 73 | { 74 | double a[] = { 0.78,0.17,0.39,0.26,0.72,0.94,0.21,0.12,0.23,0.68 }; 75 | BucketSort(a,10); 76 | 77 | } 78 | 79 | /* 80 | 练习 8.4-2 当所有数都在一个区间时,因插入排序运行时间为Θ(n²) 故。。。 81 | 修改链表排序的方式,此排序方法最坏情况下的时间代价为O(n lgn) 82 | 83 | 练习 8.4-3 84 | X=0 0.25 85 | X=1 0.5 86 | X=2 0.25 87 | E(X)=1 88 | E²(X)=1 89 | E(X²)=1.5 90 | 91 | 练习 8.4-3 r∈(0,1) r分成n段,分别为 92 | 0 - √(1/n),√(1/n) - √(2/n),√(2/n) - √(3/n),.........√((n-1)/n) - 1 93 | */ -------------------------------------------------------------------------------- /IntroductionToAlgorithms/第09章 中位数和顺序统计量/09_1_最大值和最小值.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ting1e/Introduction-To-Algorithms/312cc9c51864175581089f329a41c120cc5c591d/IntroductionToAlgorithms/第09章 中位数和顺序统计量/09_1_最大值和最小值.h -------------------------------------------------------------------------------- /IntroductionToAlgorithms/第09章 中位数和顺序统计量/09_2_期望为线性时间的选择算法.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ting1e/Introduction-To-Algorithms/312cc9c51864175581089f329a41c120cc5c591d/IntroductionToAlgorithms/第09章 中位数和顺序统计量/09_2_期望为线性时间的选择算法.h -------------------------------------------------------------------------------- /IntroductionToAlgorithms/第10章 基本数据结构/10_1_栈和队列.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "..//base.h" 3 | 4 | #ifndef TYPE 5 | #define TYPE int 6 | #endif // !TYPE 7 | 8 | 9 | struct Stack 10 | { 11 | int ele[SIZE]; 12 | int top; 13 | }; 14 | 15 | void InitStack(struct Stack *S) 16 | { 17 | S->top=-1; 18 | } 19 | 20 | 21 | int StackEmpty(struct Stack *S) 22 | { 23 | return S->top==-1; 24 | } 25 | 26 | int Push(struct Stack *S,int x) 27 | { 28 | if(S->top>=50&&S->top<-1) 29 | { 30 | return 0; 31 | } 32 | else 33 | { 34 | S->ele[++S->top]=x; 35 | return 1; 36 | } 37 | 38 | } 39 | 40 | int Pop(struct Stack *S,int *x) 41 | { 42 | if(StackEmpty(S)) 43 | { 44 | return 0; 45 | } 46 | else 47 | { 48 | *x=S->ele[S->top]; 49 | S->top--; 50 | return 1; 51 | } 52 | } 53 | 54 | void StackTest() 55 | { 56 | int a=10,b=20,c; 57 | struct Stack S; 58 | InitStack(&S); 59 | Push(&S,11); 60 | Push(&S,51); 61 | Pop(&S,&a); 62 | Pop(&S,&b); 63 | printf("%d\n",a); 64 | Pop(&S,&c); 65 | printf("%d %d %d ",a,b,c); 66 | } 67 | 68 | struct Queue 69 | { 70 | TYPE ele[SIZE]; 71 | int head; 72 | int tail; 73 | }; 74 | void InitQueue(struct Queue *Q) 75 | { 76 | Q->head=0; 77 | Q->tail=0; 78 | } 79 | 80 | int EnQueue(struct Queue *Q, TYPE x) //可以处理上溢和下溢出 81 | { 82 | if((Q->tail+1)%(SIZE-1)==Q->head) 83 | { 84 | return 0; 85 | } 86 | else 87 | { 88 | Q->ele[Q->tail++]=x; 89 | if(Q->tail>=SIZE) 90 | { 91 | Q->tail=0; 92 | } 93 | return 1; 94 | } 95 | } 96 | 97 | int DeQueue(struct Queue *Q, TYPE *x) 98 | { 99 | if(Q->head==Q->tail) 100 | { 101 | return 0; 102 | } 103 | else 104 | { 105 | *x=Q->ele[Q->head++]; 106 | if(Q->head>=SIZE) 107 | { 108 | Q->head=0; 109 | } 110 | return 1; 111 | } 112 | } 113 | // 练习10.1-5 下面两个加上上面两个。 114 | int EnQueueHead(struct Queue *Q, TYPE x) 115 | { 116 | if((Q->tail+1)%(SIZE-1)==Q->head) 117 | { 118 | return 0; 119 | } 120 | else 121 | { 122 | Q->ele[--Q->head]=x; 123 | if(Q->head<0) 124 | { 125 | Q->head=SIZE-1; 126 | } 127 | return 1; 128 | } 129 | } 130 | 131 | int DeQueueTail(struct Queue *Q, TYPE *x) 132 | { 133 | if(Q->head==Q->tail) 134 | { 135 | return 0; 136 | } 137 | else 138 | { 139 | *x=Q->ele[--Q->tail]; 140 | if(Q->tail<0) 141 | { 142 | Q->tail=SIZE-1; 143 | } 144 | return 1; 145 | } 146 | } 147 | 148 | 149 | /* 150 | void QueueTest() 151 | { 152 | struct Queue Q; 153 | TYPE x; 154 | TYPE i=1; 155 | InitQueue(&Q); 156 | while(EnQueue(&Q,i)) 157 | { 158 | i++; 159 | } 160 | DeQueue(&Q,&x); 161 | DeQueue(&Q,&x); 162 | DeQueue(&Q,&x); 163 | while(EnQueue(&Q,i)) 164 | { 165 | i++; 166 | } 167 | while(DeQueue(&Q,&x)) 168 | { 169 | printf("%d\n",x); 170 | } 171 | 172 | } 173 | */ 174 | 175 | /* 176 | 练习10.1-2 分别以1和n为栈底,从两侧向中心生长。 177 | 178 | 练习10.1-7 179 | 180 | 两个栈S1,S2 181 | Depend on the operation 182 | EnQueue(x) //θ(1) 183 | S1.Push(x) 184 | 185 | Dequeue(x) //O(n) I am not sure about the low boundary. 186 | //Guess e[X]==θ(1) 187 | if S2 is empty 188 | S2.Push(all S1) 189 | else 190 | S2.Pop 191 | 192 | 193 | 194 | 练习10.1-8 195 | Q1,Q2 //let Q1,Q2 circle maybe a good choice 196 | Push(x) //θ(1) 197 | Q1.EnQueue(x) 198 | 199 | Pop //θ(n) 200 | Q2.EnQueue(all Q1) and record last one 201 | Q1.EnQueue(all Q2 expect last one ) 202 | return Q2.Dequeue 203 | 204 | 205 | */ 206 | -------------------------------------------------------------------------------- /IntroductionToAlgorithms/第10章 基本数据结构/10_2_链表.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "..\base.h" 3 | //双向链表、、 4 | //前面几种都是简单的双向链表 5 | // 6 | typedef struct LIST 7 | { 8 | int key; 9 | struct LIST *prev; 10 | struct LIST *next; 11 | } LinkList; 12 | 13 | LinkList *ListSearch(LinkList *l,int k) 14 | { 15 | while(l!=NULL&&l->key!=k ) 16 | { 17 | l=l->next; 18 | } 19 | return l; 20 | } 21 | 22 | int ListInsert(LinkList **l,int x) 23 | { 24 | if(*l==NULL) 25 | { 26 | return 0; 27 | } 28 | LinkList * new_node = (LinkList *)malloc(sizeof(LinkList)); 29 | 30 | new_node->key=x; 31 | new_node->prev=NULL; 32 | new_node->next = *l; 33 | (*l)->prev=new_node; 34 | *l=new_node; 35 | return 1; 36 | } 37 | int ListDelete(LinkList **l,int x) 38 | { 39 | LinkList *temp =*l; 40 | while(temp!=NULL&&temp->key!=x); 41 | if(temp==NULL) 42 | { 43 | return 0; 44 | } 45 | else 46 | { 47 | temp->next->prev=temp->prev; 48 | temp->prev->next=temp->next; 49 | free(temp); 50 | return 1; 51 | } 52 | } 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | -------------------------------------------------------------------------------- /IntroductionToAlgorithms/第11章 散列表/11_1_直接寻址表.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "..\base.h" 3 | // 散列表 4 | 5 | #define SIZE 100 6 | #define ((x).key) (x) 7 | 8 | 9 | int t[SIZE]; 10 | 11 | int DirectAddressSearch(int t[],int k) 12 | { 13 | return t[k]; 14 | } 15 | 16 | void DirectAddressInsert(int t[],int x) 17 | { 18 | t[x.key]=x; 19 | } 20 | 21 | /* 22 | *注意本章所有均未测试。。。。。。 23 | * 24 | * 25 | * 26 | * 27 | * 28 | * 29 | * 30 | * 31 | * 32 | * 33 | */ 34 | -------------------------------------------------------------------------------- /IntroductionToAlgorithms/第11章 散列表/11_2_散列表.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "..\base.h" 3 | #include "..\第十章 基本数据结构\10_2_链表.h" 4 | 5 | 6 | 7 | LinkList *t[100]; 8 | 9 | void ChainedHashInsert(LinkList *t[],int x) 10 | { 11 | //ListInsert(t[x.key],x); 12 | } 13 | 14 | 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /IntroductionToAlgorithms/第11章 散列表/11_3_散列函数.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "..\base.h" 3 | 4 | //除法散列函数 5 | 6 | int DivideHash(int k) 7 | { 8 | return k%701; 9 | } 10 | 11 | //乘法散列函数 12 | // h(k)=(m*(kA mod 1)) 13 | // 14 | // 15 | int MultHash(int k) 16 | { 17 | int m=2<<10; 18 | return (int)((double)k*0.618034)*m; 19 | } 20 | -------------------------------------------------------------------------------- /IntroductionToAlgorithms/第12章 二叉搜索树/12_1_建立_2_查询BST.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "..\base.h" 3 | 4 | typedef struct BINARYSEARCHTREE 5 | { 6 | int key; 7 | struct BINARYSEARCHTREE *left; 8 | struct BINARYSEARCHTREE *right; 9 | struct BINARYSEARCHTREE *p; 10 | }BSTree; 11 | 12 | 13 | 14 | //you need to make sure the array observe the rule of bst 15 | // 16 | // 17 | BSTree *BuildTree(BSTree *parent, int a[],int i,int size) 18 | { 19 | BSTree *head=NULL; 20 | if(ip=parent; 24 | head->key=a[i]; 25 | 26 | head->left=BuildTree(head,a,2*i+1,size); 27 | head->right=BuildTree(head,a,2*i+2,size); 28 | } 29 | 30 | return head; 31 | } 32 | // 33 | void InorderTreeWalk(BSTree *root) 34 | { 35 | if(root!=NULL) 36 | { 37 | InorderTreeWalk(root->left); 38 | printf("%d ",root->key); 39 | InorderTreeWalk(root->right); 40 | } 41 | } 42 | 43 | //练习12.1-3 44 | //非递归遍历算法 45 | 46 | void InorderNoRecursion(BSTree *root) 47 | { 48 | if(root==NULL) 49 | return ; 50 | BSTree *current=root; 51 | BSTree *child=NULL; 52 | while(1) 53 | { 54 | if(child!=current->left) 55 | { 56 | while(current->left) 57 | current=current->left; 58 | } 59 | printf("%d ",current->key); 60 | if(current->right) 61 | { 62 | current=current->right; 63 | continue; 64 | } 65 | do 66 | { 67 | child=current; 68 | current=current->p; 69 | if(current==NULL) 70 | return; 71 | 72 | }while(child==current->right); 73 | } 74 | 75 | } 76 | 77 | 78 | BSTree *TreeSearch(BSTree *root,int k) 79 | { 80 | //递归版本 81 | if(root==NULL||root->key==k) 82 | { 83 | return root; 84 | } 85 | if(kkey) 86 | { 87 | return TreeSearch(root->left,k); 88 | } 89 | else 90 | { 91 | return TreeSearch(root->right,k); 92 | } 93 | } 94 | 95 | //迭代版本 96 | BSTree *IterativeTreeSearch(BSTree *root,int k) 97 | { 98 | while(root!=NULL&&k!=root->key) 99 | { 100 | if(kkey) 101 | { 102 | root=root->left; 103 | } 104 | else 105 | { 106 | root=root->right; 107 | } 108 | } 109 | return root; 110 | } 111 | 112 | BSTree *TreeMinimum(BSTree *root) 113 | { 114 | while(root->left) 115 | root=root->left; 116 | return root; 117 | } 118 | 119 | BSTree *TreeMaximum(BSTree *root) 120 | { 121 | while(root->right) 122 | root=root->right; 123 | return root; 124 | } 125 | 126 | BSTree *TreeSuccessor(BSTree *x) 127 | { 128 | if(x->right!=NULL) 129 | { 130 | return TreeMinimum(x->right); 131 | } 132 | 133 | BSTree *y=x->p; 134 | while(y!=NULL&&x==y->right) 135 | { 136 | x=y; 137 | y=y->p; 138 | } 139 | return y; 140 | 141 | } 142 | 143 | BSTree *TreePredecessor(BSTree *x) 144 | { 145 | if(x->left) 146 | { 147 | TreeMaximum(x->left); 148 | 149 | } 150 | BSTree *y=x->p; 151 | while(y!=NULL&&x==y->left) 152 | { 153 | x=y; 154 | y=y->p; 155 | } 156 | return y; 157 | } 158 | 159 | // 12.2-1 c不是查找过的序列 160 | // 161 | // 162 | // 163 | //12.2-2 非递归版本 原理一致所以只写一个啦 164 | // 165 | BSTree *TreeMinimum1(BSTree *root) 166 | { 167 | if(root->left) 168 | { 169 | TreeMinimum1(root->left); 170 | } 171 | else 172 | { 173 | return root; 174 | } 175 | } 176 | 177 | // 178 | //12.2-5 二叉树中一个节点的后继,一定在此节点的右侧,而且是此侧最小的节点,如果这个节点还有左孩子,那么它一定不是最小的节点 179 | // 前驱同理 180 | // 181 | // 182 | //12.2-6 如果一个节点没有右子树,那它的后继一定不是它的子节点而是它的某个祖先,因为父节点一定大于左子树上所有节点,而小于右子树上所有节点 183 | // 如果这个后继的左孩子不是它的祖先,那个这个后继的右孩子是它的祖先,因父节点一定大于右子树上所有节点,不可能是他的后继,故不成立 184 | // 因而原题得证。 185 | // 186 | // 187 | // 188 | // 189 | // 190 | // 191 | // 192 | // 193 | // 194 | // 195 | 196 | 197 | 198 | 199 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 209 | 210 | -------------------------------------------------------------------------------- /IntroductionToAlgorithms/第12章 二叉搜索树/12_3_插入和删除.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "..\base.h" 3 | #include "12_1_建立_2_查询BST.h" 4 | 5 | void TreeInsert(BSTree **root,int z) 6 | { 7 | BSTree *x=*root; 8 | BSTree *y=NULL; 9 | while(x!=NULL) 10 | { 11 | y=x; 12 | if(x->key>=z) 13 | { 14 | x=x->left; 15 | } 16 | else 17 | { 18 | x=x->right; 19 | } 20 | } 21 | 22 | BSTree *node=(BSTree *)malloc(sizeof(BSTree)); 23 | node->key=z; 24 | node->left=NULL; 25 | node->right=NULL; 26 | if(y==NULL) 27 | { 28 | node->p=NULL; 29 | *root=node; 30 | } 31 | else if(zkey) 32 | { 33 | node->p=y; 34 | y->left=node; 35 | } 36 | else 37 | { 38 | node->p=y; 39 | y->right=node; 40 | } 41 | 42 | } 43 | 44 | 45 | void Transplant(BSTree **root,BSTree *u,BSTree *v) 46 | { 47 | if(u->p==NULL) 48 | { 49 | *root=v; 50 | } 51 | else if(u==u->p->left) 52 | { 53 | u->p->left=v; 54 | } 55 | else 56 | { 57 | u->p->right=v; 58 | } 59 | if(v!=NULL) 60 | { 61 | v->p=u->p; 62 | } 63 | } 64 | 65 | void TreeDelete(BSTree **root,int k) 66 | { 67 | BSTree *z=IterativeTreeSearch(*root,k); 68 | if(z==NULL) 69 | { 70 | return; 71 | } 72 | 73 | if(z->left==NULL) 74 | { 75 | Transplant(root,z,z->right); 76 | } 77 | else if(z->right==NULL) 78 | { 79 | Transplant(root,z,z->left); 80 | } 81 | else 82 | { 83 | BSTree *y=TreeMinimum(z->right); 84 | if(y->p!=z) 85 | { 86 | Transplant(root,y,y->right); 87 | y->right=z->right; 88 | y->right->p=y; 89 | } 90 | Transplant(root,z,y); 91 | y->left=z->left; 92 | y->left->p=y; 93 | } 94 | free(z); 95 | } 96 | 97 | //练习 12.3-12 树插入的一个递归版本 98 | void MyTreeInsert(BSTree **t,BSTree *p,int k) 99 | { 100 | if(*t==NULL) 101 | { 102 | (*t)=(BSTree *)malloc(sizeof(BSTree)); 103 | (*t)->key=k; 104 | (*t)->left=NULL; 105 | (*t)->right=NULL; 106 | (*t)->p=p; 107 | if(p!=NULL) 108 | { 109 | if(p->keyright=*t; 111 | else 112 | p->left=*t; 113 | } 114 | return; 115 | } 116 | else if((*t)->key>k) 117 | { 118 | MyTreeInsert(&((*t)->left),*t,k); 119 | } 120 | else 121 | { 122 | MyTreeInsert(&((*t)->right),*t,k); 123 | } 124 | } 125 | 126 | 127 | //12.3-3 n*n nlogn 128 | // 129 | // 130 | // 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | -------------------------------------------------------------------------------- /IntroductionToAlgorithms/第12章 二叉搜索树/test.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "..\base.h" 3 | #include "12_3_插入和删除.h" 4 | 5 | void test() 6 | { 7 | int a[10]={15,6,18,3,13,17,20,2,4,7}; 8 | BSTree *root =BuildTree(NULL,a,0,10); 9 | InorderTreeWalk(root); 10 | printf("\n"); 11 | 12 | InorderNoRecursion(root); 13 | 14 | printf("\n"); 15 | TreeInsert(&root,30); 16 | TreeInsert(&root,1); 17 | InorderTreeWalk(root); 18 | printf("\n"); 19 | 20 | TreeDelete(&root,1); 21 | TreeDelete(&root,3); 22 | TreeDelete(&root,20); 23 | TreeDelete(&root,30); 24 | InorderTreeWalk(root); 25 | printf("\n"); 26 | 27 | MyTreeInsert(&root,NULL,1); 28 | MyTreeInsert(&root,NULL,3); 29 | MyTreeInsert(&root,NULL,30); 30 | InorderTreeWalk(root); 31 | printf("\n"); 32 | 33 | } 34 | -------------------------------------------------------------------------------- /IntroductionToAlgorithms/第13章 红黑树/13_红黑树.h: -------------------------------------------------------------------------------- 1 | #include "..//base.h" 2 | 3 | 4 | //假设所有NULL都为黑色叶子节点 5 | 6 | enum Color{red,black}; 7 | 8 | typedef struct RBTREE 9 | { 10 | enum Color color; 11 | int key; 12 | struct RBTREE *left; 13 | struct RBTREE *right; 14 | struct RBTREE *p; 15 | } RBTree; 16 | 17 | //红黑书搜索节点 18 | RBTree *TreeSearch(RBTree *root, int k) 19 | { 20 | while (root != NULL && k != root->key) 21 | { 22 | if (kkey) 23 | { 24 | root = root->left; 25 | } 26 | else 27 | { 28 | root = root->right; 29 | } 30 | } 31 | return root; 32 | } 33 | 34 | 35 | 36 | //中序遍历 37 | 38 | void InOrder(RBTree *root) 39 | { 40 | static char _color[2][6]={"red","black"}; 41 | if(root!=NULL) 42 | { 43 | InOrder(root->left); 44 | printf("%d %s ",root->key,_color[root->color]); 45 | InOrder(root->right); 46 | } 47 | } 48 | 49 | 50 | // 51 | // 52 | //旋转 53 | //向左旋转 54 | 55 | void LeftRotate(RBTree **root,RBTree *x) 56 | { 57 | RBTree *y=x->right; 58 | x->right=y->left; 59 | if(y->left!=NULL) 60 | { 61 | y->left->p=x; 62 | } 63 | y->p=x->p; 64 | if(x==*root) 65 | { 66 | *root=y; 67 | } 68 | else if(x==x->p->left) 69 | { 70 | x->p->left=y; 71 | } 72 | else 73 | { 74 | x->p->right=y; 75 | } 76 | y->left=x; 77 | x->p=y; 78 | } 79 | 80 | //练习 13.2-1 向右旋转 81 | 82 | void RightRotate(RBTree **root,RBTree *y) 83 | { 84 | RBTree *x=y->left; 85 | y->left=x->right; 86 | if(x->right!=NULL) 87 | { 88 | x->right->p=y; 89 | } 90 | x->p=y->p; 91 | if(y==*root) 92 | { 93 | *root=x; 94 | } 95 | else if(y==y->p->left) 96 | { 97 | x->p->left=x; 98 | } 99 | else 100 | { 101 | x->p->right=x; 102 | } 103 | x->right=y; 104 | y->p=x; 105 | } 106 | 107 | void RBInsertFixup(RBTree **root,RBTree *z) 108 | { 109 | RBTree *y=NULL; 110 | while(z->p!=NULL&&z->p->color==red) 111 | { 112 | if(z->p==z->p->p->left) 113 | { 114 | y=z->p->p->right; 115 | if(y!=NULL&&y->color==red) 116 | { 117 | z->p->color=black; 118 | y->color=black; 119 | z->p->p->color=red; 120 | z=z->p->p; 121 | } 122 | else if(z==z->p->right) 123 | { 124 | z=z->p; 125 | LeftRotate(root,z); 126 | } 127 | else 128 | { 129 | z->p->color=black; 130 | z->p->p->color=red; 131 | RightRotate(root,z->p->p); 132 | } 133 | } 134 | else 135 | { 136 | 137 | y=z->p->p->left; 138 | if(y!=NULL&&y->color==red) 139 | { 140 | z->p->color=black; 141 | y->color=black; 142 | z->p->p->color=red; 143 | z=z->p->p; 144 | } 145 | else if(z==z->p->left) 146 | { 147 | z=z->p; 148 | LeftRotate(root,z); 149 | } 150 | else 151 | { 152 | z->p->color=black; 153 | z->p->p->color=red; 154 | LeftRotate(root,z->p->p); 155 | } 156 | } 157 | } 158 | (*root)->color=black; 159 | } 160 | 161 | 162 | 163 | 164 | void RBInsert(RBTree **root,int k) 165 | { 166 | RBTree *y=NULL; 167 | RBTree *x=*root; 168 | while(x!=NULL) 169 | { 170 | y=x; 171 | if(kkey) 172 | { 173 | x=x->left; 174 | } 175 | else 176 | { 177 | x=x->right; 178 | } 179 | } 180 | RBTree *node=(RBTree *)malloc(sizeof(RBTree)); 181 | node->key=k; 182 | node->left=NULL; 183 | node->right=NULL; 184 | node->color=red; 185 | node->p=y; 186 | if(y==NULL) 187 | { 188 | node->color=black; 189 | *root=node; 190 | return; 191 | } 192 | else if(kkey) 193 | { 194 | y->left=node; 195 | } 196 | else 197 | { 198 | y->right=node; 199 | } 200 | RBInsertFixup(root,node); 201 | } 202 | 203 | 204 | 205 | 206 | // 练习 13.3-1 那样会破坏性质5.。 207 | // 208 | // 练习 13.3-2 r==red b==black 209 | // 210 | // 38 b 211 | // 31 r 41 b 212 | // 12 b 19 b 213 | // 8r 214 | // 215 | //练习 13.3-4 216 | // 将节点颜色改成红色的只有三处,而且这三处改变的节点都是不可能是nil节点。 217 | // 218 | //练习 13.3-6 219 | // 可以写一个找出父指针的函数代替 220 | 221 | 222 | 223 | void RBTransplant(RBTree **root,RBTree *u,RBTree *v) 224 | { 225 | if(u->p==NULL) 226 | { 227 | *root=v; 228 | } 229 | else if(u==u->p->left) 230 | { 231 | u->p->left=v; 232 | } 233 | else 234 | { 235 | u->p->right=v; 236 | } 237 | if (v != NULL) 238 | v->p = u->p; 239 | } 240 | 241 | 242 | RBTree *RBMinimum(RBTree *x) 243 | { 244 | if(x==NULL) 245 | return NULL; 246 | while(x->left!=NULL) 247 | x=x->right; 248 | return x; 249 | } 250 | 251 | 252 | void RBDeleteFixup(RBTree **root,RBTree *x) 253 | { 254 | RBTree *w=NULL; 255 | while(x!=NULL&&x->color==black) 256 | { 257 | if(x==x->p->left) 258 | { 259 | w=x->p->right; 260 | if(w->color==red) 261 | { 262 | w->color=black; 263 | x->p->color=red; 264 | LeftRotate(root,x->p); 265 | w=x->p->right; 266 | } 267 | if(w->left->color==black&&w->right->color==black) 268 | { 269 | w->color=red; 270 | x=x->p; 271 | } 272 | else if(w->right->color==black) 273 | { 274 | w->left->color=black; 275 | w->color=red; 276 | RightRotate(root,w); 277 | w=x->p->right; 278 | } 279 | w->color=x->p->color; 280 | x->p->color=black; 281 | w->right->color=black; 282 | LeftRotate(root,x->p); 283 | x=*root; 284 | } 285 | else 286 | { 287 | 288 | w=x->p->left; 289 | if(w->color==red) 290 | { 291 | w->color=black; 292 | x->p->color=red; 293 | LeftRotate(root,x->p); 294 | w=x->p->left; 295 | } 296 | if(w->right->color==black&&w->left->color==black) 297 | { 298 | w->color=red; 299 | x=x->p; 300 | } 301 | else if(w->left->color==black) 302 | { 303 | w->right->color=black; 304 | w->color=red; 305 | RightRotate(root,w); 306 | w=x->p->left; 307 | } 308 | w->color=x->p->color; 309 | x->p->color=black; 310 | w->left->color=black; 311 | LeftRotate(root,x->p); 312 | x=*root; 313 | } 314 | } 315 | if (x != NULL) 316 | x->color = black; 317 | } 318 | 319 | 320 | void RBDelete(RBTree **root,int k) 321 | { 322 | RBTree *z = TreeSearch(*root, k); 323 | if (z == NULL) 324 | return; 325 | RBTree *y=z; 326 | RBTree *x=NULL; 327 | enum Color y_color=y->color; 328 | if(z->left==NULL) 329 | { 330 | x=z->right; 331 | RBTransplant(root,z,z->right); 332 | } 333 | else if(z->right==NULL) 334 | { 335 | x=z->left; 336 | RBTransplant(root,z,z->left); 337 | } 338 | else 339 | { 340 | y=RBMinimum(z->right); 341 | y_color=y->color; 342 | x=y->right; 343 | if (y->p==z) 344 | x->p=y; 345 | else 346 | { 347 | RBTransplant(root,y,y->right); 348 | y->right=z->right; 349 | y->right->p=y; 350 | } 351 | RBTransplant(root,z,y); 352 | y->left=z->left; 353 | y->left->p=y; 354 | y->color=z->color; 355 | } 356 | if(y_color==black) 357 | { 358 | RBDeleteFixup(root,x); 359 | } 360 | } 361 | 362 | 363 | 364 | 365 | void test() 366 | { 367 | RBTree *tree = NULL; 368 | RBInsert(&tree, 19); 369 | 370 | RBInsert(&tree, 3); 371 | InOrder(tree); 372 | printf("\n"); 373 | 374 | RBInsert(&tree, 5); 375 | InOrder(tree); 376 | printf("\n"); 377 | 378 | 379 | RBInsert(&tree, 6); 380 | InOrder(tree); 381 | printf("\n"); 382 | 383 | int i; 384 | for (i = 1; i<20; i++) 385 | { 386 | RBInsert(&tree, i); 387 | if (i % 5 == 0) 388 | InOrder(tree); 389 | printf("\n"); 390 | } 391 | 392 | RBDelete(&tree, 1); 393 | 394 | InOrder(tree); 395 | } 396 | -------------------------------------------------------------------------------- /IntroductionToAlgorithms/第15章 动态规划/15_1_钢条切割.h: -------------------------------------------------------------------------------- 1 | #include "..//base.h" 2 | 3 | //递归求解 4 | int CutRod(int p[],int n) 5 | { 6 | if(n==0) 7 | return 0; 8 | int q=-1; 9 | int i; 10 | for(i=1;i<=n;i++) 11 | { 12 | q=MAX(q,p[i]+CutRod(p,n-i)); 13 | } 14 | return q; 15 | } 16 | 17 | //自顶向上方法 备忘录 18 | int MemorizedCutRodAux(int p[],int n,int r[]) 19 | { 20 | if(r[n]>=0) 21 | return r[n]; 22 | int q=-1; 23 | if(n==0) 24 | q=0; 25 | else 26 | { 27 | int i; 28 | for(i=1;i<=n;i++) 29 | { 30 | q=MAX(q,p[i]+MemorizedCutRodAux(p,n-i,r)); 31 | } 32 | } 33 | r[n]=q; 34 | return q; 35 | } 36 | 37 | int MemoizedCutRod(int p[],int n) 38 | { 39 | int i; 40 | int r[100]; 41 | for(i=0;i<=n;i++) 42 | { 43 | r[i]=-1; 44 | } 45 | return MemorizedCutRodAux(p,n,r); 46 | } 47 | 48 | 49 | //自底向上方法 50 | // 51 | int BottomUpRod(int p[],int n) 52 | { 53 | int r[100]={0}; //因不支持变长数组,简单的以合适大小的数组表示。 54 | int i,j,q; 55 | for(i=1;i<=n;i++) 56 | { 57 | q=-1; 58 | for(j=1;j<=i;j++) 59 | { 60 | q=MAX(q,p[j]+r[i-j]); 61 | } 62 | r[i]=q; 63 | } 64 | return r[n]; 65 | } 66 | 67 | 68 | //给出对应切割长度 69 | // 70 | int *ExtendedBottomUpCutRod(int p[],int n,int *t) 71 | { 72 | int *s=(int *)malloc(sizeof(int)*(n+1)); 73 | int r[100]={0}; 74 | int i,j,q; 75 | for(i=1;i<=n;i++) 76 | { 77 | q=-1; 78 | for(j=1;j<=i;j++) 79 | { 80 | if(q0) 98 | { 99 | printf("%d ",*(s+n)); 100 | n=n-s[n]; 101 | } 102 | printf("\n"); 103 | } 104 | 105 | 106 | 107 | void test() 108 | { 109 | int p[] = { 0,1,5,8,9,10,17,17,20,24,30 }; 110 | int pi; 111 | pi = CutRod(p,10); 112 | printf("10: price %d\n ",pi); 113 | 114 | pi = MemoizedCutRod(p,10); 115 | printf("10: price %d\n ",pi); 116 | 117 | pi = BottomUpRod(p,10); 118 | printf("10: price %d\n ",pi); 119 | 120 | 121 | PrintCutRodSolution(p,9); 122 | } 123 | -------------------------------------------------------------------------------- /IntroductionToAlgorithms/第15章 动态规划/15_4_最长公共子序列.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ting1e/Introduction-To-Algorithms/312cc9c51864175581089f329a41c120cc5c591d/IntroductionToAlgorithms/第15章 动态规划/15_4_最长公共子序列.h -------------------------------------------------------------------------------- /IntroductionToAlgorithms/第15章 动态规划/15_5_最优二叉树.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "..//base.h" 3 | 4 | void OptimalBST(double p[], double q[], int n) 5 | { 6 | double e[10][10], w[10][10]; 7 | int root[10][10]; 8 | for (int i = 1; i <= n + 1; i++) 9 | { 10 | e[i][i - 1] = q[i - 1]; 11 | w[i][i - 1] = q[i - 1]; 12 | } 13 | for (int l = 1; l <= n; l++) 14 | { 15 | for (int i = 1; i <= n - l + 1; i++) 16 | { 17 | int j = i + l - 1; 18 | e[i][j] = 100; 19 | w[i][j] = w[i][j-1] + q[j] + p[j]; 20 | for (int r = i; r <= j; r++) 21 | { 22 | double t = e[i][r - 1] + e[r + 1][j] + w[i][j]; 23 | if (t < e[i][j]) 24 | { 25 | e[i][j] = t; 26 | root[i][j] = r; 27 | } 28 | } 29 | } 30 | } 31 | 32 | int x; 33 | 34 | } 35 | 36 | void test() 37 | { 38 | double p[] = { 0,0.15,0.10,0.05,0.10,0.20 }; 39 | double q[] = { 0.05,0.10,0.05,0.05,0.05,0.10 }; 40 | OptimalBST(p, q, 5); 41 | } -------------------------------------------------------------------------------- /IntroductionToAlgorithms/第16章 贪心算法/16_1_活动选择问题.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "..//base.h" 3 | 4 | void RecursizeActivitySelector(int start[], int end[], int k,int n, int ret[], int current) 5 | { 6 | int m = k + 1; 7 | while (m <= n && start[m] < end[k]) 8 | { 9 | m++; 10 | } 11 | if (m <= n) 12 | { 13 | ret[current] = m; 14 | RecursizeActivitySelector(start, end, m, n, ret, current + 1); 15 | } 16 | } 17 | 18 | void GreedyActivitySelector(int s[], int f[],int n,int ret[]) 19 | { 20 | int cur = 0; 21 | int k = 1; 22 | ret[cur++] = 1; 23 | for (int m = 2; m <= n; m++) 24 | { 25 | if (s[m] > f[k]) 26 | { 27 | ret[cur++] = m; 28 | k = m; 29 | } 30 | } 31 | } 32 | 33 | 34 | 35 | 36 | /* 37 | 练习16.1-1 38 | 39 | for i <- 0 to n+1 40 |   do c[i,i] <- 0 41 | for l <- 1 to n+1 42 |    do for i <- 0 to n+1-l 43 |     do j <- i+l 44 | c[i,j] <- 0 45 | for k <- i+1 to j-1 46 | do if f_i <= s_k and f_k <= s_j  // test if activity a_k is in S_{ij} 47 | then q <- c[i,k] + c[k,j] + 1 48 |                      if q > c[i,j]                      then c[i,j] <- q 49 |                           a[i,j] <- k 50 | */ 51 | 52 | 53 | void test() 54 | { 55 | int s[] = { 0,1,3,0,5,3,5,6,8,8,2,12 }; 56 | int f[] = { 0,4,5,6,7,9,9,10,11,12,14,16 }; 57 | int ret[10] = { 0 }; 58 | RecursizeActivitySelector(s,f,0,11,ret,0); 59 | int ret2[10] = { 0 }; 60 | GreedyActivitySelector(s, f, 11, ret2); 61 | 62 | 63 | } -------------------------------------------------------------------------------- /IntroductionToAlgorithms/第16章 贪心算法/16_2_贪心算法原理.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ting1e/Introduction-To-Algorithms/312cc9c51864175581089f329a41c120cc5c591d/IntroductionToAlgorithms/第16章 贪心算法/16_2_贪心算法原理.h -------------------------------------------------------------------------------- /IntroductionToAlgorithms/第16章 贪心算法/16_3_哈夫曼编码.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ting1e/Introduction-To-Algorithms/312cc9c51864175581089f329a41c120cc5c591d/IntroductionToAlgorithms/第16章 贪心算法/16_3_哈夫曼编码.h -------------------------------------------------------------------------------- /IntroductionToAlgorithms/第18章 B树/18_2_B树上的基本操作_18_3_删除.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ting1e/Introduction-To-Algorithms/312cc9c51864175581089f329a41c120cc5c591d/IntroductionToAlgorithms/第18章 B树/18_2_B树上的基本操作_18_3_删除.h -------------------------------------------------------------------------------- /IntroductionToAlgorithms/第19章 斐波那契堆/19_斐波那契堆.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "..//base.h" 3 | 4 | 5 | #define dn 50 6 | typedef struct FIB_NODE 7 | { 8 | int key; 9 | int degree; 10 | int mark; 11 | struct FIB_NODE *left; 12 | struct FIB_NODE *right; 13 | struct FIB_NODE *parent; 14 | struct FIB_NODE *child; 15 | }FibNode; 16 | 17 | typedef struct FIB_HEAP 18 | { 19 | int n; 20 | FibNode *min_node; 21 | }FibHeap; 22 | 23 | FibNode *CreateNode(int x,int ma) 24 | { 25 | FibNode *fib_node = (FibNode *)malloc(sizeof(FibNode)); 26 | fib_node->degree = 0; 27 | fib_node->key = x; 28 | fib_node->mark = ma; 29 | fib_node->parent = NULL; 30 | fib_node->child = NULL; 31 | fib_node->left = fib_node; 32 | fib_node->right = fib_node; 33 | return fib_node; 34 | } 35 | 36 | void FibHeapInsert(FibHeap *fibheap, FibNode *fib_node) 37 | { 38 | 39 | if (fibheap->min_node == NULL) 40 | { 41 | fib_node->left = fib_node; 42 | fib_node->right = fib_node; 43 | fibheap->min_node = fib_node; 44 | } 45 | else 46 | { 47 | fib_node->right = fibheap->min_node; 48 | fib_node->left = fibheap->min_node->left; 49 | fibheap->min_node->left->right = fib_node; 50 | fibheap->min_node->left = fib_node; 51 | if (fib_node->key < fibheap->min_node->key) 52 | fibheap->min_node = fib_node; 53 | } 54 | fib_node->parent = NULL; 55 | fibheap->n++; 56 | } 57 | 58 | FibHeap *MakeFibHeap() 59 | { 60 | FibHeap *fib_heap = (FibHeap*)malloc(sizeof(FibHeap)); 61 | fib_heap->min_node = NULL; 62 | fib_heap->n = 0; 63 | return fib_heap; 64 | } 65 | 66 | FibHeap *FibHeapUnion(FibHeap *heap1, FibHeap *heap2) 67 | { 68 | FibHeap *heap = MakeFibHeap(); 69 | heap->min_node = heap1->min_node; 70 | if ((heap1->min_node == NULL) || ((heap2->min_node != NULL) && (heap2->min_node->key < heap1->min_node->key))) 71 | { 72 | heap->min_node = heap2->min_node; 73 | } 74 | if (heap1->min_node != NULL && heap2->min_node != NULL) 75 | { 76 | heap1->min_node->left->right = heap2->min_node->left; 77 | heap2->min_node->left->right = heap1->min_node->left; 78 | heap1->min_node->left = heap2->min_node; 79 | heap2->min_node->left = heap1->min_node; 80 | } 81 | heap->n = heap1->n + heap2->n; 82 | return heap; 83 | } 84 | 85 | void FibHeapLink(FibHeap *heap, FibNode *y, FibNode *x) 86 | { 87 | y->left->right = y->right; 88 | y->right->left = y->left; 89 | if (x->child == NULL) 90 | { 91 | x->child = y; 92 | y->parent = x; 93 | y->left = y; 94 | y->right = y; 95 | } 96 | else 97 | { 98 | x->child->left->right = y; 99 | y->left = x->child->left; 100 | x->child->left = y; 101 | y->right = x->child; 102 | y->parent = x; 103 | } 104 | x->degree++; 105 | y->mark = 0; 106 | } 107 | void Consolidate(FibHeap *heap) 108 | { 109 | FibNode *a[dn] = { 0 }; 110 | FibNode *x = heap->min_node; 111 | do 112 | { 113 | int d = x->degree; 114 | while (a[d]!=NULL&&dmin_node) 117 | { 118 | heap->min_node = heap->min_node->right; 119 | } 120 | FibNode *y = a[d]; 121 | if (x->key > y->key) 122 | { 123 | FibNode *temp = x; 124 | x = y; 125 | y = temp; 126 | } 127 | FibHeapLink(heap, y, x); 128 | a[d] = NULL; 129 | d++; 130 | } 131 | a[d] = x; 132 | x = x->right; 133 | } while (x != heap->min_node); 134 | heap->min_node = NULL; 135 | 136 | for (int i = 0; i < dn; i++) 137 | { 138 | if (a[i] != NULL) 139 | { 140 | if (heap->min_node == NULL) 141 | { 142 | a[i]->left = a[i]; 143 | a[i]->right = a[i]; 144 | heap->min_node = a[i]; 145 | } 146 | else 147 | { 148 | a[i]->left = heap->min_node->left; 149 | a[i]->right = heap->min_node; 150 | heap->min_node->left->right = a[i]; 151 | heap->min_node->left = a[i]; 152 | if (a[i]->key < heap->min_node->key) 153 | { 154 | heap->min_node = a[i]; 155 | } 156 | } 157 | } 158 | } 159 | 160 | } 161 | 162 | FibNode *FibHeapExtractMin(FibHeap *heap) 163 | { 164 | FibNode *min = heap->min_node; 165 | if (min != NULL) 166 | { 167 | min->left->right = min->child; 168 | min->right->left = min->child->left; 169 | min->child->left->right = min->right; 170 | min->child->left = min->left; 171 | if (min == min->right) 172 | { 173 | heap->min_node = NULL; 174 | } 175 | else 176 | { 177 | heap->min_node = min->right; 178 | Consolidate(heap); 179 | } 180 | heap->n--; 181 | } 182 | return min; 183 | } 184 | 185 | void Cut(FibHeap *heap, FibNode *x, FibNode *y) 186 | { 187 | if (y->degree == 1) 188 | { 189 | y->child = 0; 190 | } 191 | else 192 | { 193 | if (x == y->child) 194 | { 195 | y->child = x->right; 196 | } 197 | x->left->right = x->right; 198 | x->right->left = x->left; 199 | } 200 | y->degree--; 201 | x->mark = 0; 202 | FibHeapInsert(heap, x); 203 | } 204 | 205 | void CascadingCut(FibHeap *heap, FibNode *y) 206 | { 207 | FibNode *z = y->parent; 208 | if (z != NULL) 209 | { 210 | if (y->mark == 0) 211 | { 212 | y->mark = 1; 213 | } 214 | else 215 | { 216 | Cut(heap, y, z); 217 | CascadingCut(heap, z); 218 | } 219 | } 220 | } 221 | 222 | void FibHeapDecreaseKey(FibHeap *heap, FibNode *x, int k) 223 | { 224 | if (k > x->key) 225 | return; 226 | x->key = k; 227 | FibNode *y = x->parent; 228 | if (y != NULL && x->key < y->key) 229 | { 230 | Cut(heap, x, y); 231 | CascadingCut(heap, y); 232 | } 233 | if (x->key < heap->min_node->key) 234 | { 235 | heap->min_node = x; 236 | } 237 | } 238 | 239 | void FibHeapDelete(FibHeap *heap, FibNode *node) 240 | { 241 | FibHeapDecreaseKey(heap, node, INT_MIN); 242 | FibHeapExtractMin(heap); 243 | } 244 | 245 | 246 | void test() 247 | { 248 | FibHeap *heap = MakeFibHeap(); 249 | FibNode *node3 = CreateNode(3, 0); 250 | FibNode *node18 = CreateNode(18, 1); 251 | FibNode *node39 = CreateNode(39, 1); 252 | node18->child = node39; 253 | node39->parent = node18; 254 | FibNode *node52 = CreateNode(52, 0); 255 | FibNode *node38 = CreateNode(38, 0); 256 | FibNode *node41 = CreateNode(41, 0); 257 | node38->child = node41; 258 | 259 | node18->right = node52; 260 | node52->right = node38; 261 | node38->right = node18; 262 | node18->left = node38; 263 | node52->left = node18; 264 | node38->left = node52; 265 | node3->child = node52; 266 | node41->parent = node38; 267 | node39->parent = node18; 268 | 269 | node52->parent = node3; 270 | node38->parent = node3; 271 | node18->parent = node3; 272 | 273 | node3->degree = 3; 274 | node18->degree = 1; 275 | node38->degree = 1; 276 | 277 | FibNode *node17 = CreateNode(17, 0); 278 | FibNode *node30 = CreateNode(30, 0); 279 | node17->child = node30; 280 | node30->parent = node17; 281 | node17->degree = 1; 282 | 283 | FibNode *node46 = CreateNode(46, 0); 284 | FibNode *node24 = CreateNode(24, 0); 285 | FibNode *node26 = CreateNode(26, 1); 286 | FibNode *node35 = CreateNode(35, 0); 287 | node26->child = node35; 288 | node35->parent = node26; 289 | node26->left = node46; 290 | node46->left = node26; 291 | node26->right = node46; 292 | node46->right = node26; 293 | node24->child = node26; 294 | node26->parent = node24; 295 | node46->parent = node24; 296 | node24->degree = 2; 297 | node26->degree=1; 298 | 299 | 300 | FibHeapInsert(heap, node17); 301 | FibHeapInsert(heap, node24); 302 | FibHeapInsert(heap, node3); 303 | FibHeapInsert(heap, CreateNode(23, 0)); 304 | FibHeapInsert(heap, CreateNode(7, 0)); 305 | FibHeapInsert(heap, CreateNode(21, 0)); 306 | heap->n = 15; 307 | FibHeapExtractMin(heap); 308 | FibHeapDecreaseKey(heap, node46, 5); 309 | printf("1234"); 310 | } -------------------------------------------------------------------------------- /IntroductionToAlgorithms/第22章 基本的图算法/22_1_图的表示.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ting1e/Introduction-To-Algorithms/312cc9c51864175581089f329a41c120cc5c591d/IntroductionToAlgorithms/第22章 基本的图算法/22_1_图的表示.h -------------------------------------------------------------------------------- /IntroductionToAlgorithms/第22章 基本的图算法/22_2_广度优先搜索.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ting1e/Introduction-To-Algorithms/312cc9c51864175581089f329a41c120cc5c591d/IntroductionToAlgorithms/第22章 基本的图算法/22_2_广度优先搜索.h -------------------------------------------------------------------------------- /IntroductionToAlgorithms/第22章 基本的图算法/22_3_深度优先搜索.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ting1e/Introduction-To-Algorithms/312cc9c51864175581089f329a41c120cc5c591d/IntroductionToAlgorithms/第22章 基本的图算法/22_3_深度优先搜索.h -------------------------------------------------------------------------------- /IntroductionToAlgorithms/第22章 基本的图算法/22_5_强连通分量.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ting1e/Introduction-To-Algorithms/312cc9c51864175581089f329a41c120cc5c591d/IntroductionToAlgorithms/第22章 基本的图算法/22_5_强连通分量.h -------------------------------------------------------------------------------- /IntroductionToAlgorithms/第23章 最小生成树/23_2_Kruskal算法.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ting1e/Introduction-To-Algorithms/312cc9c51864175581089f329a41c120cc5c591d/IntroductionToAlgorithms/第23章 最小生成树/23_2_Kruskal算法.h -------------------------------------------------------------------------------- /IntroductionToAlgorithms/第23章 最小生成树/23_2_Prim算法.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ting1e/Introduction-To-Algorithms/312cc9c51864175581089f329a41c120cc5c591d/IntroductionToAlgorithms/第23章 最小生成树/23_2_Prim算法.h -------------------------------------------------------------------------------- /IntroductionToAlgorithms/第24章 单源最短路径/24_2_Bellman-Ford算法.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ting1e/Introduction-To-Algorithms/312cc9c51864175581089f329a41c120cc5c591d/IntroductionToAlgorithms/第24章 单源最短路径/24_2_Bellman-Ford算法.h -------------------------------------------------------------------------------- /IntroductionToAlgorithms/第24章 单源最短路径/24_3_Dijkstra算法.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ting1e/Introduction-To-Algorithms/312cc9c51864175581089f329a41c120cc5c591d/IntroductionToAlgorithms/第24章 单源最短路径/24_3_Dijkstra算法.h -------------------------------------------------------------------------------- /IntroductionToAlgorithms/第24章 单源最短路径/松弛操作.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ting1e/Introduction-To-Algorithms/312cc9c51864175581089f329a41c120cc5c591d/IntroductionToAlgorithms/第24章 单源最短路径/松弛操作.h -------------------------------------------------------------------------------- /IntroductionToAlgorithms/第32章 字符串匹配/32_1_朴素字符串匹配算法.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "..//base.h" 3 | 4 | int NaiveStringMatcher(char t[], int n, char p[], int m) 5 | { 6 | int i, j; 7 | for (i = 0; i < n - m; i++) 8 | { 9 | int flag = 0; 10 | for (j = 0; j < m; j++) 11 | { 12 | if (t[i + j] == p[j]) 13 | { 14 | flag++; 15 | } 16 | } 17 | if (flag == m) 18 | break; 19 | } 20 | return i; 21 | } 22 | 23 | void test() 24 | { 25 | char t[] = "acaabc"; 26 | char p[] = "aab"; 27 | int a = NaiveStringMatcher(t, 6, p, 3); 28 | } -------------------------------------------------------------------------------- /IntroductionToAlgorithms/第32章 字符串匹配/32_2_Rabin-Karp算法.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "..//base.h" 3 | 4 | 5 | int pow(int x, int y) 6 | { 7 | int r = 1; 8 | while (y--) 9 | { 10 | r *= x; 11 | } 12 | return r; 13 | } 14 | 15 | 16 | int RabinKarpMatcher(char st[], int n, char sp[], int m, int d, int q) 17 | { 18 | int i, j; 19 | int p = 0, t = 0; 20 | int h = pow(d, m - 1) % q; 21 | for (i = 0; i < m; i++) 22 | { 23 | p = (d*p +sp[i]) % q; 24 | t = (d*t + st[i]) % q; 25 | } 26 | for (int i = 0; i < n - m; i++) 27 | { 28 | if (p == t) 29 | { 30 | int flag = 0; 31 | for (j = 0; j < m; j++) 32 | { 33 | if (st[i + j] == sp[j]) 34 | { 35 | flag++; 36 | } 37 | } 38 | if (flag == m) 39 | break; 40 | } 41 | if (i < n - m) 42 | { 43 | t = (d*(t - st[i] * h) + st[i + m]) % q; 44 | } 45 | } 46 | return i; 47 | } 48 | 49 | 50 | void test() 51 | { 52 | char t[] = "acaabc"; 53 | char p[] = "aab"; 54 | int a = RabinKarpMatcher(t, 6, p, 3,10,13); 55 | } -------------------------------------------------------------------------------- /IntroductionToAlgorithms/第32章 字符串匹配/32_4_Hnuth-Morris-Pratt算法.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "..//base.h" 3 | 4 | void ComputePrefixFunction(int pi[], char p[], int m) 5 | { 6 | pi[0] = 0; 7 | int k = 0; 8 | for (int i = 1; i < m; i++) 9 | { 10 | while (k > 0 && p[k] != p[i]) 11 | { 12 | k = pi[k]; 13 | } 14 | if (p[k] == p[i]) 15 | { 16 | k++; 17 | } 18 | pi[i] = k; 19 | } 20 | } 21 | 22 | int KMPMatcher(char st[], int n, char sp[], int m) 23 | { 24 | int *pi = (int *)malloc(sizeof(int)*m); 25 | ComputePrefixFunction(pi, sp, m); 26 | int q = 0; 27 | for (int i = 0; i < n; i ++ ) 28 | { 29 | while (q > 0 && sp[q] != st[i]) //not match 30 | { 31 | q = pi[q-1]; 32 | } 33 | if (sp[q] == st[i]) 34 | { 35 | q++; 36 | } 37 | if (q == m) 38 | { 39 | return i-m+1; 40 | } 41 | } 42 | return -1; 43 | } 44 | 45 | void test() 46 | { 47 | char t[] = "bacbabababacabcbab"; 48 | char p[] = "ababaca"; 49 | int a = KMPMatcher(t, 15, p, 7); 50 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Introduction-To-Algorithms 2 | 3 | ## 算法导论伪代码的实现及部分习题 4 | 5 | 开始时间 2018-3-3 周六, 6 | --------------------------------------------------------------------------------