Skip to content

Commit 42c1f22

Browse files
committed
3.2.37 Finished
1 parent 5c06d02 commit 42c1f22

File tree

6 files changed

+95
-13
lines changed

6 files changed

+95
-13
lines changed

3 Searching/3.2/3.2.28/Program.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,19 @@ static void Main(string[] args)
1212
var bstCached = BuildTree<BSTCached<int, int>>();
1313

1414
var watch = Stopwatch.StartNew();
15-
for (int i = 0; i < 100000; i++)
15+
for (var i = 0; i < 1000000; i++)
1616
{
1717
bstCached.Put(20, i);
1818
}
1919
watch.Stop();
2020
Console.WriteLine("bstCached: " + watch.ElapsedMilliseconds + " ms");
2121

2222
watch.Restart();
23-
for (int i = 0; i < 100000; i++)
23+
for (var i = 0; i < 1000000; i++)
2424
{
2525
bst.Put(20, i);
2626
}
27+
watch.Stop();
2728
Console.WriteLine("bst:" + watch.ElapsedMilliseconds + " ms");
2829
}
2930

@@ -36,10 +37,13 @@ static void Main(string[] args)
3637
bst.Put(2, 2);
3738
bst.Put(1, 1);
3839
bst.Put(12, 12);
40+
bst.Put(18, 18);
3941
bst.Put(15, 15);
42+
bst.Put(16, 16);
4043
bst.Put(11, 11);
4144
bst.Put(8, 8);
4245
bst.Put(14, 14);
46+
bst.Put(20, 20);
4347
return bst;
4448
}
4549
}
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.0</TargetFramework>
6+
<RootNamespace>_3._2._37</RootNamespace>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<ProjectReference Include="..\BinarySearchTree\BinarySearchTree.csproj" />
11+
</ItemGroup>
12+
13+
</Project>

3 Searching/3.2/3.2.37/Program.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System;
2+
using BinarySearchTree;
3+
4+
namespace _3._2._37
5+
{
6+
class Program
7+
{
8+
static void Main(string[] args)
9+
{
10+
var bst = new BST<int, int>();
11+
bst.Put(4, 4);
12+
bst.Put(3, 3);
13+
bst.Put(9, 9);
14+
bst.Put(2, 2);
15+
bst.Put(1, 1);
16+
bst.Put(11, 11);
17+
bst.Put(12, 12);
18+
bst.Put(8, 8);
19+
20+
Console.WriteLine(bst);
21+
Console.WriteLine();
22+
Console.WriteLine("PrintLevel(4): ");
23+
bst.PrintLevel(4);
24+
Console.WriteLine();
25+
Console.WriteLine("PrintLevel(9): ");
26+
bst.PrintLevel(9);
27+
}
28+
}
29+
}

3 Searching/3.2/BinarySearchTree/BST.cs

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,15 +115,15 @@ 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);
118+
public virtual TValue Get(TKey key) => Get(root, key).Value;
119119

120120
/// <summary>
121-
/// 递归查找 <paramref name="key"/> 所对应的值
121+
/// 递归查找 <paramref name="key"/> 所对应的结点
122122
/// </summary>
123123
/// <param name="x">要查找的根结点。</param>
124124
/// <param name="key">要查找的键。</param>
125-
/// <returns>如果存在则返回对应的值,否则返回 <c>default(TValue)</c>。</returns>
126-
protected virtual TValue Get(Node x, TKey key)
125+
/// <returns>如果存在则返回对应的结点,否则返回 <c>default(TValue)</c>。</returns>
126+
protected virtual Node Get(Node x, TKey key)
127127
{
128128
if (key == null)
129129
throw new ArgumentNullException("calls get() with a null key");
@@ -135,7 +135,7 @@ protected virtual TValue Get(Node x, TKey key)
135135
else if (cmp > 0)
136136
return Get(x.Right, key);
137137
else
138-
return x.Value;
138+
return x;
139139
}
140140

141141
/// <summary>
@@ -553,6 +553,34 @@ protected virtual Node DeleteMax(Node x)
553553
return x;
554554
}
555555

556+
/// <summary>
557+
/// 按照层级顺序打印以 <paramref name="key"/> 为根的子树。
558+
/// </summary>
559+
/// <param name="key">作为根结点的键值。</param>
560+
public void PrintLevel(TKey key)
561+
{
562+
PrintLevel(Get(root, key));
563+
}
564+
565+
/// <summary>
566+
/// 按照层级顺序打印以 <paramref name="x"/> 为根的子树。
567+
/// </summary>
568+
/// <param name="x">根结点。</param>
569+
private void PrintLevel(Node x)
570+
{
571+
var queue = new Queue<Node>();
572+
queue.Enqueue(x);
573+
while (queue.Count > 0)
574+
{
575+
var node = queue.Dequeue();
576+
if (node.Left != null)
577+
queue.Enqueue(node.Left);
578+
if (node.Right != null)
579+
queue.Enqueue(node.Right);
580+
Console.Write(node.Key + ", ");
581+
}
582+
}
583+
556584
/// <summary>
557585
/// 将二叉树按照树形输出。
558586
/// </summary>

3 Searching/3.2/BinarySearchTree/BSTCached.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ public override TValue Get(TKey key)
2222
return _cache.Value;
2323
}
2424

25-
return base.Get(root, key);
25+
return Get(root, key).Value;
2626
}
2727

2828
/// <inheritdoc />
29-
protected override TValue Get(Node x, TKey key)
29+
protected override Node Get(Node x, TKey key)
3030
{
3131
if (key == null)
3232
{
@@ -35,7 +35,7 @@ protected override TValue Get(Node x, TKey key)
3535

3636
if (x == null)
3737
{
38-
return default;
38+
return null;
3939
}
4040
var cmp = key.CompareTo(x.Key);
4141
if (cmp < 0)
@@ -49,7 +49,7 @@ protected override TValue Get(Node x, TKey key)
4949
else
5050
{
5151
_cache = x;
52-
return x.Value;
52+
return x;
5353
}
5454
}
5555

@@ -70,6 +70,7 @@ public override void Put(TKey key, TValue value)
7070
if (_cache != null && _cache.Key.CompareTo(key) == 0)
7171
{
7272
_cache.Value = value;
73+
return;
7374
}
7475
root = Put(root, key, value);
7576
}

Algorithms 4th Edition.sln

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1023,9 +1023,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "3.2.33", "3 Searching\3.2\3
10231023
EndProject
10241024
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "3.2.34", "3 Searching\3.2\3.2.34\3.2.34.csproj", "{E2CFD797-005D-41F2-AFCF-212FB5352432}"
10251025
EndProject
1026-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "3.2.35", "3 Searching\3.2\3.2.35\3.2.35.csproj", "{97B6B0DD-EB46-45C3-A917-47D05783243C}"
1026+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "3.2.35", "3 Searching\3.2\3.2.35\3.2.35.csproj", "{97B6B0DD-EB46-45C3-A917-47D05783243C}"
10271027
EndProject
1028-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "3.2.36", "3 Searching\3.2\3.2.36\3.2.36.csproj", "{0F47B81D-DD65-4EEA-9414-AB575E96A382}"
1028+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "3.2.36", "3 Searching\3.2\3.2.36\3.2.36.csproj", "{0F47B81D-DD65-4EEA-9414-AB575E96A382}"
1029+
EndProject
1030+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "3.2.37", "3 Searching\3.2\3.2.37\3.2.37.csproj", "{919A1928-E309-4F31-97C5-AAEAE7F9E546}"
10291031
EndProject
10301032
Global
10311033
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -2817,6 +2819,10 @@ Global
28172819
{0F47B81D-DD65-4EEA-9414-AB575E96A382}.Debug|Any CPU.Build.0 = Debug|Any CPU
28182820
{0F47B81D-DD65-4EEA-9414-AB575E96A382}.Release|Any CPU.ActiveCfg = Release|Any CPU
28192821
{0F47B81D-DD65-4EEA-9414-AB575E96A382}.Release|Any CPU.Build.0 = Release|Any CPU
2822+
{919A1928-E309-4F31-97C5-AAEAE7F9E546}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
2823+
{919A1928-E309-4F31-97C5-AAEAE7F9E546}.Debug|Any CPU.Build.0 = Debug|Any CPU
2824+
{919A1928-E309-4F31-97C5-AAEAE7F9E546}.Release|Any CPU.ActiveCfg = Release|Any CPU
2825+
{919A1928-E309-4F31-97C5-AAEAE7F9E546}.Release|Any CPU.Build.0 = Release|Any CPU
28202826
EndGlobalSection
28212827
GlobalSection(SolutionProperties) = preSolution
28222828
HideSolutionNode = FALSE
@@ -3326,6 +3332,7 @@ Global
33263332
{E2CFD797-005D-41F2-AFCF-212FB5352432} = {136F3860-08E9-4C37-BC04-679085D442B4}
33273333
{97B6B0DD-EB46-45C3-A917-47D05783243C} = {136F3860-08E9-4C37-BC04-679085D442B4}
33283334
{0F47B81D-DD65-4EEA-9414-AB575E96A382} = {136F3860-08E9-4C37-BC04-679085D442B4}
3335+
{919A1928-E309-4F31-97C5-AAEAE7F9E546} = {136F3860-08E9-4C37-BC04-679085D442B4}
33293336
EndGlobalSection
33303337
GlobalSection(ExtensibilityGlobals) = postSolution
33313338
SolutionGuid = {EE55CF8D-6F35-464D-B95B-ED85644AE41C}

0 commit comments

Comments
 (0)