Skip to content

Commit 72baf22

Browse files
committed
3.2.43 Finished
1 parent 31fd19f commit 72baf22

File tree

6 files changed

+104
-8
lines changed

6 files changed

+104
-8
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp3.1</TargetFramework>
6+
<RootNamespace>_3._2._43</RootNamespace>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<ProjectReference Include="..\BinarySearchTree\BinarySearchTree.csproj" />
11+
</ItemGroup>
12+
13+
</Project>

3 Searching/3.2/3.2.43/Program.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using System;
2+
using BinarySearchTree;
3+
4+
namespace _3._2._43
5+
{
6+
class Program
7+
{
8+
static void Main(string[] args)
9+
{
10+
const int trials = 10;
11+
var putTime = 0L;
12+
var setTime = 0L;
13+
for (var j = 0; j < trials; j++)
14+
{
15+
var random = new Random();
16+
var bst = new BSTTimer<int, int>();
17+
var data = new int[1000000];
18+
for (var i = 0; i < data.Length; i++)
19+
{
20+
data[i] = random.Next();
21+
}
22+
23+
FrequencyCounter.MostFrequentlyKey(bst, data);
24+
putTime += bst.PutTime;
25+
setTime += bst.SetTime;
26+
}
27+
28+
putTime /= trials;
29+
setTime /= trials;
30+
Console.WriteLine("Put: " + putTime + "ms Get: " + setTime + "ms ratio: " + (double)putTime / setTime);
31+
32+
}
33+
}
34+
}

3 Searching/3.2/BinarySearchTree/BST.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,16 @@ protected virtual Node Put(Node x, TKey key, TValue value)
115115
/// </summary>
116116
/// <param name="key">需要查找的键。</param>
117117
/// <returns>找到的值,不存在则返回 <c>default(TValue)</c>。</returns>
118-
public virtual TValue Get(TKey key) => Get(root, key).Value;
118+
public virtual TValue Get(TKey key)
119+
{
120+
var result = Get(root, key);
121+
if (result == null)
122+
{
123+
return default;
124+
}
125+
126+
return result.Value;
127+
}
119128

120129
/// <summary>
121130
/// 递归查找 <paramref name="key"/> 所对应的结点。
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System;
2+
using System.Diagnostics;
3+
4+
namespace BinarySearchTree
5+
{
6+
public class BSTTimer<TKey, TValue> : BST<TKey, TValue> where TKey : IComparable<TKey>
7+
{
8+
public long PutTime { get; set; }
9+
public long SetTime { get; set; }
10+
11+
/// <summary>
12+
/// 向二叉查找树中插入一个键值对。
13+
/// </summary>
14+
/// <param name="key">要插入的键。</param>
15+
/// <param name="value">要插入的值。</param>
16+
public override void Put(TKey key, TValue value)
17+
{
18+
var timer = Stopwatch.StartNew();
19+
base.Put(key, value);
20+
timer.Stop();
21+
PutTime += timer.ElapsedMilliseconds;
22+
}
23+
24+
/// <summary>
25+
/// 获得 <paramref name="key"/> 对应的值,不存在则返回 <c>default(TValue)</c>。
26+
/// </summary>
27+
/// <param name="key">需要查找的键。</param>
28+
/// <returns>找到的值,不存在则返回 <c>default(TValue)</c>。</returns>
29+
public override TValue Get(TKey key)
30+
{
31+
var timer = Stopwatch.StartNew();
32+
var result = base.Get(key);
33+
timer.Stop();
34+
SetTime += timer.ElapsedMilliseconds;
35+
return result;
36+
}
37+
}
38+
}

3 Searching/3.2/BinarySearchTree/FrequencyCounter.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,8 @@ namespace BinarySearchTree
99
/// 计算文本文档中出现次数最高的字符串,
1010
/// 用于测试符号表的 Get 和 Put 方法。
1111
/// </summary>
12-
public class FrequencyCounter
12+
public static class FrequencyCounter
1313
{
14-
/// <summary>
15-
/// 这个类不能被初始化。
16-
/// </summary>
17-
private FrequencyCounter() { }
18-
1914
/// <summary>
2015
/// 计算数组中不重复元素的数量。
2116
/// </summary>

Algorithms 4th Edition.sln

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1037,7 +1037,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "3.2.40", "3 Searching\3.2\3
10371037
EndProject
10381038
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "3.2.41", "3 Searching\3.2\3.2.41\3.2.41.csproj", "{64038379-9705-4127-A25C-BC0391298EE0}"
10391039
EndProject
1040-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "3.2.42", "3 Searching\3.2\3.2.42\3.2.42.csproj", "{BAD1523D-5DE5-4117-9D57-C7A1F4A2A29A}"
1040+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "3.2.42", "3 Searching\3.2\3.2.42\3.2.42.csproj", "{BAD1523D-5DE5-4117-9D57-C7A1F4A2A29A}"
1041+
EndProject
1042+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "3.2.43", "3 Searching\3.2\3.2.43\3.2.43.csproj", "{C17CA5CF-1E52-4330-8B1A-CB91AE537FCF}"
10411043
EndProject
10421044
Global
10431045
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -2853,6 +2855,10 @@ Global
28532855
{BAD1523D-5DE5-4117-9D57-C7A1F4A2A29A}.Debug|Any CPU.Build.0 = Debug|Any CPU
28542856
{BAD1523D-5DE5-4117-9D57-C7A1F4A2A29A}.Release|Any CPU.ActiveCfg = Release|Any CPU
28552857
{BAD1523D-5DE5-4117-9D57-C7A1F4A2A29A}.Release|Any CPU.Build.0 = Release|Any CPU
2858+
{C17CA5CF-1E52-4330-8B1A-CB91AE537FCF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
2859+
{C17CA5CF-1E52-4330-8B1A-CB91AE537FCF}.Debug|Any CPU.Build.0 = Debug|Any CPU
2860+
{C17CA5CF-1E52-4330-8B1A-CB91AE537FCF}.Release|Any CPU.ActiveCfg = Release|Any CPU
2861+
{C17CA5CF-1E52-4330-8B1A-CB91AE537FCF}.Release|Any CPU.Build.0 = Release|Any CPU
28562862
EndGlobalSection
28572863
GlobalSection(SolutionProperties) = preSolution
28582864
HideSolutionNode = FALSE
@@ -3368,6 +3374,7 @@ Global
33683374
{28E56928-AA66-464E-B791-A72889D4C624} = {69512490-D514-490D-9088-D514FC5C770A}
33693375
{64038379-9705-4127-A25C-BC0391298EE0} = {69512490-D514-490D-9088-D514FC5C770A}
33703376
{BAD1523D-5DE5-4117-9D57-C7A1F4A2A29A} = {69512490-D514-490D-9088-D514FC5C770A}
3377+
{C17CA5CF-1E52-4330-8B1A-CB91AE537FCF} = {69512490-D514-490D-9088-D514FC5C770A}
33713378
EndGlobalSection
33723379
GlobalSection(ExtensibilityGlobals) = postSolution
33733380
SolutionGuid = {EE55CF8D-6F35-464D-B95B-ED85644AE41C}

0 commit comments

Comments
 (0)