Skip to content

Commit 0ce37a7

Browse files
committed
3.2.42 Finished
1 parent e8238d2 commit 0ce37a7

File tree

5 files changed

+197
-1
lines changed

5 files changed

+197
-1
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.0</TargetFramework>
6+
<RootNamespace>_3._2._42</RootNamespace>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<ProjectReference Include="..\BinarySearchTree\BinarySearchTree.csproj" />
11+
</ItemGroup>
12+
13+
</Project>

3 Searching/3.2/3.2.42/Program.cs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
using System;
2+
using BinarySearchTree;
3+
4+
namespace _3._2._42
5+
{
6+
class Program
7+
{
8+
static void Main(string[] args)
9+
{
10+
var random = new Random();
11+
for (var n = 100; n <= 10000; n *= 10)
12+
{
13+
Console.WriteLine("n = " + n + " sqrt(n) = " + (int)(Math.Sqrt(n)));
14+
var data = new int[n * n + n];
15+
for (var i = 0; i < data.Length; i++)
16+
{
17+
data[i] = i;
18+
}
19+
20+
Shuffle(data);
21+
22+
var bstHibbard = new BST<int, int>();
23+
var bstNonHibbard = new BSTNonHibbard<int, int>();
24+
25+
// build
26+
for (var i = 0; i < n; i++)
27+
{
28+
bstHibbard.Put(data[i], i);
29+
bstNonHibbard.Put(data[i], i);
30+
}
31+
32+
var avePathHibbardBefore = bstHibbard.AverageInternalPathLength();
33+
var avePathNonHibbardBefore = bstNonHibbard.AverageInternalPathLength();
34+
Console.WriteLine("stage\tHibbard\tNonHibbard");
35+
Console.WriteLine("before\t" + avePathHibbardBefore + "\t" + avePathNonHibbardBefore);
36+
37+
// test
38+
for (var i = n; i < n * n; i++)
39+
{
40+
var rank = random.Next(n);
41+
bstHibbard.Delete(bstHibbard.Select(rank));
42+
bstHibbard.Put(data[i], i);
43+
44+
bstNonHibbard.Delete(bstNonHibbard.Select(rank));
45+
bstNonHibbard.Put(data[i], i);
46+
}
47+
48+
var avePathHibbardAfter = bstHibbard.AverageInternalPathLength();
49+
var avePathNonHibbardAfter = bstNonHibbard.AverageInternalPathLength();
50+
Console.WriteLine("after\t" + avePathHibbardAfter + "\t" + avePathNonHibbardAfter);
51+
Console.WriteLine("diff\t" + (avePathHibbardAfter - avePathHibbardBefore) + "\t" + (avePathNonHibbardAfter - avePathNonHibbardBefore));
52+
Console.WriteLine();
53+
}
54+
}
55+
56+
static void Shuffle<T>(T[] a)
57+
{
58+
var random = new Random();
59+
for (var i = 0; i < a.Length; i++)
60+
{
61+
var r = i + random.Next(a.Length - i);
62+
var temp = a[i];
63+
a[i] = a[r];
64+
a[r] = temp;
65+
}
66+
}
67+
}
68+
}

3 Searching/3.2/BinarySearchTree/BST.cs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,52 @@ protected virtual Node DeleteMax(Node x)
553553
return x;
554554
}
555555

556+
/// <summary>
557+
/// 获取二叉树的内部平均路径长度。
558+
/// </summary>
559+
/// <returns>内部平均路径长度。</returns>
560+
public int AverageInternalPathLength() => InternalPath() / Size() + 1;
561+
562+
/// <summary>
563+
/// 二叉树的内部路径长度。
564+
/// </summary>
565+
/// <returns>二叉树的内部路径长度(所有结点深度之和)。</returns>
566+
private int InternalPath()
567+
{
568+
var internalPath = 0;
569+
var nowLayer = new Queue<Node>();
570+
var nextLayer = new Queue<Node>();
571+
nextLayer.Enqueue(root);
572+
573+
var depth = 0;
574+
while (nextLayer.Count > 0)
575+
{
576+
var temp = nowLayer;
577+
nowLayer = nextLayer;
578+
nextLayer = temp;
579+
580+
while (nowLayer.Count > 0)
581+
{
582+
var node = nowLayer.Dequeue();
583+
if (node.Left != null)
584+
{
585+
nextLayer.Enqueue(node.Left);
586+
}
587+
588+
if (node.Right != null)
589+
{
590+
nextLayer.Enqueue(node.Right);
591+
}
592+
593+
internalPath += depth;
594+
}
595+
596+
depth++;
597+
}
598+
599+
return internalPath;
600+
}
601+
556602
/// <summary>
557603
/// 按照层级顺序打印以 <paramref name="key"/> 为根的子树。
558604
/// </summary>
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
using System;
2+
3+
namespace BinarySearchTree
4+
{
5+
public class BSTNonHibbard<TKey, TValue> : BST<TKey, TValue> where TKey : IComparable<TKey>
6+
{
7+
private readonly Random _random = new Random();
8+
9+
/// <summary>
10+
/// 从二叉查找树中删除键为 <paramref name="key"/> 的结点。
11+
/// </summary>
12+
/// <param name="x">要删除的结点的二叉查找树。</param>
13+
/// <param name="key">要删除的键。</param>
14+
/// <returns>删除结点后的二叉查找树。</returns>
15+
protected override Node Delete(Node x, TKey key)
16+
{
17+
if (x == null)
18+
{
19+
return null;
20+
}
21+
22+
var cmp = key.CompareTo(x.Key);
23+
if (cmp < 0)
24+
{
25+
x.Left = Delete(x.Left, key);
26+
}
27+
else if (cmp > 0)
28+
{
29+
x.Right = Delete(x.Right, key);
30+
}
31+
else
32+
{
33+
if (x.Right == null)
34+
{
35+
return x.Left;
36+
}
37+
38+
if (x.Left == null)
39+
{
40+
return x.Right;
41+
}
42+
var t = x;
43+
if (_random.NextDouble() < 0.5)
44+
{
45+
x = Min(t.Right);
46+
x.Right = DeleteMin(t.Right);
47+
x.Left = t.Left;
48+
}
49+
else
50+
{
51+
x = Max(t.Left);
52+
x.Left = DeleteMax(t.Left);
53+
x.Right = t.Right;
54+
}
55+
}
56+
x.Size = Size(x.Left) + Size(x.Right) + 1;
57+
return x;
58+
}
59+
60+
61+
}
62+
}

Algorithms 4th Edition.sln

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1035,7 +1035,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "3.2.39", "3 Searching\3.2\3
10351035
EndProject
10361036
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "3.2.40", "3 Searching\3.2\3.2.40\3.2.40.csproj", "{28E56928-AA66-464E-B791-A72889D4C624}"
10371037
EndProject
1038-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "3.2.41", "3 Searching\3.2\3.2.41\3.2.41.csproj", "{64038379-9705-4127-A25C-BC0391298EE0}"
1038+
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}"
1039+
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}"
10391041
EndProject
10401042
Global
10411043
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -2847,6 +2849,10 @@ Global
28472849
{64038379-9705-4127-A25C-BC0391298EE0}.Debug|Any CPU.Build.0 = Debug|Any CPU
28482850
{64038379-9705-4127-A25C-BC0391298EE0}.Release|Any CPU.ActiveCfg = Release|Any CPU
28492851
{64038379-9705-4127-A25C-BC0391298EE0}.Release|Any CPU.Build.0 = Release|Any CPU
2852+
{BAD1523D-5DE5-4117-9D57-C7A1F4A2A29A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
2853+
{BAD1523D-5DE5-4117-9D57-C7A1F4A2A29A}.Debug|Any CPU.Build.0 = Debug|Any CPU
2854+
{BAD1523D-5DE5-4117-9D57-C7A1F4A2A29A}.Release|Any CPU.ActiveCfg = Release|Any CPU
2855+
{BAD1523D-5DE5-4117-9D57-C7A1F4A2A29A}.Release|Any CPU.Build.0 = Release|Any CPU
28502856
EndGlobalSection
28512857
GlobalSection(SolutionProperties) = preSolution
28522858
HideSolutionNode = FALSE
@@ -3361,6 +3367,7 @@ Global
33613367
{2428EF52-8CE9-48F9-95DD-707B1E5F8DF5} = {69512490-D514-490D-9088-D514FC5C770A}
33623368
{28E56928-AA66-464E-B791-A72889D4C624} = {69512490-D514-490D-9088-D514FC5C770A}
33633369
{64038379-9705-4127-A25C-BC0391298EE0} = {69512490-D514-490D-9088-D514FC5C770A}
3370+
{BAD1523D-5DE5-4117-9D57-C7A1F4A2A29A} = {69512490-D514-490D-9088-D514FC5C770A}
33643371
EndGlobalSection
33653372
GlobalSection(ExtensibilityGlobals) = postSolution
33663373
SolutionGuid = {EE55CF8D-6F35-464D-B95B-ED85644AE41C}

0 commit comments

Comments
 (0)