1.基于二叉查找树实现集合
class BST1Set<E> : ISet<E> where E : IComparable<E>
{
private BST1<E> bst;
public BST1Set()
{
bst = new BST1<E>();
}
public int Count {
get {
return bst.Count; } }
public bool IsEmpty {
get {
return bst.IsEmpty; } }
public void Add(E e)
{
bst.Add(e);
}
public bool Contains(E e)
{
return bst.Contains(e);
}
public void Remove(E e)
{
bst.Remove(e);
}
public int MaxHeigh()
{
return bst.MaxHeigh();
}
}
与基于有序数组实现的集合对比测试:
class Program
{
private static long TestSet(ISet<string> set, List<string> words)
{
Stopwatch t = new Stopwatch();
t.Start();
foreach (string word in words)
set.Add(word);
t.Stop();
return t.ElapsedMilliseconds;
}
static void Main(string[] args)
{
Console.WriteLine("双城记");
List<string> words = TestHelper.ReadFile("测试文件1/双城记.txt");
Console.WriteLine("词汇量总数:" + words.Count);
Console.WriteLine();
Console.WriteLine("(基于二叉查找树实现)集合");
BST1Set<string> bST1Set = new BST1Set<string>();
long t1 = TestSet(bST1Set, words);
Console.WriteLine("不同单词的总数:" + bST1Set.Count);
Console.WriteLine("运行时间:" + t1 + "ms");
Console.WriteLine("数的最大高度:" + bST1Set.MaxHeigh());
Console.WriteLine();
Console.WriteLine("(基于有序数组实现)集合");
SortedArray1Set<string> sortedArray1Set = new SortedArray1Set<string>();
long t2 = TestSet(sortedArray1Set, words);
Console.WriteLine("不同单词的总数:" + sortedArray1Set.Count);
Console.WriteLine("运行时间:" + t2 + "ms"