public void Add_Multiple_Entries()
        {
            const int NrOfItems = 5;
            var       bsc       = new BinarySearchSt <int, string>();

            for (int i = 0; i < NrOfItems; i++)
            {
                bsc.Add(i, i.ToString());
            }

            Assert.That(bsc.Count, Is.EqualTo(NrOfItems));
        }
        public void Range()
        {
            var bsc = new BinarySearchSt <string, int>();

            bsc.Add("b", 1);
            bsc.Add("c", 2);
            bsc.Add("a", 3);
            bsc.Add("f", 4);

            Assert.That(
                bsc.Range("b", "g"),
                Is.EquivalentTo(new[] { "b", "c", "f" })
                );
        }
        public void Get_Index_Of_Entry()
        {
            var bsc = new BinarySearchSt <string, int>();

            bsc.Add("b", 1);
            bsc.Add("c", 2);
            bsc.Add("a", 3);
            bsc.Add("f", 4);

            Assert.That(bsc.Rank("a"), Is.EqualTo(0));
            Assert.That(bsc.Rank("b"), Is.EqualTo(1));
            Assert.That(bsc.Rank("c"), Is.EqualTo(2));
            Assert.That(bsc.Rank("f"), Is.EqualTo(3));
        }
        public static void BinarySearchStDemo()
        {
            BinarySearchSt <char, int> bsst = new BinarySearchSt <char, int>(5);

            //char ch = 'b';
            //int num = 1;

            //for (int i = 0; i < bsst.Capacity; i++)
            //{
            //    Console.WriteLine(ch + " " + num);
            //    bsst.Add(ch++, num++);

            //}

            bsst.Add('c', 2);
            bsst.Add('f', 5);
            bsst.Add('b', 1);
            bsst.Add('d', 3);
            bsst.Add('e', 4);

            Console.WriteLine(bsst.Count);
            Console.WriteLine("Min, should be b: " + bsst.Min());
            Console.WriteLine("Max, should be f: " + bsst.Max());

            bsst.RemoveMin();
            bsst.RemoveMax();

            Console.WriteLine("Min, should be c: " + bsst.Min());
            Console.WriteLine("Max, should be e: " + bsst.Max());

            bsst.Add('b', 1);
            bsst.Add('f', 5);

            Console.WriteLine("Select index 2, should be d: " + bsst.Select(2));
            Console.WriteLine("Select index 4, should be f: " + bsst.Select(4));

            Console.WriteLine("Ceiling b, should be b: " + bsst.Ceiling('b'));
            Console.WriteLine("Ceiling a, should be b: " + bsst.Ceiling('a'));
            Console.WriteLine("Ceiling g, should be null: " + bsst.Ceiling('g'));
            Console.WriteLine("Ceiling d, should be d: " + bsst.Ceiling('d'));

            Console.WriteLine("Floor e, should be e: " + bsst.Floor('e'));
            Console.WriteLine("Floor a, should be null: " + bsst.Floor('a'));
            Console.WriteLine("Floor g, should be f: " + bsst.Floor('g'));

            Console.WriteLine("Range b & f: ");
            var range = bsst.Range('b', 'f');

            //Console.WriteLine("Range a & f: ");
            //array = bsst.Range('a', 'f');

            //Console.WriteLine("Range e & h: ");
            //array = bsst.Range('e', 'h');

            //Console.WriteLine("Range b & c: ");
            //array = bsst.Range('b', 'c');

            //Console.WriteLine("Range g & h: ");
            //array = bsst.Range('g', 'h');

            Console.WriteLine("Range a & g: ");
            range = bsst.Range('a', 'g');

            foreach (var item in range)
            {
                Console.Write(item + " ");
            }
        }
    /// <summary>
    /// 找到同时存在于字典和输入文件的单词并统计频率,
    /// 分别按照频率降序和字典升序输出。
    /// </summary>
    /// <param name="filename">输入文件。</param>
    /// <param name="dictionaryFile">字典文件。</param>
    /// <param name="minLength">最小长度。</param>
    public static void LookUpDictionary(string filename, string dictionaryFile, int minLength)
    {
        // 初始化字典
        var sr         = new StreamReader(File.OpenRead(dictionaryFile));
        var words      = sr.ReadToEnd().Split(new[] { ' ', '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
        var dictionary = new BinarySearchSt <string, int>();

        for (var i = 0; i < words.Length; i++)
        {
            if (words[i].Length > minLength)
            {
                dictionary.Put(words[i], i);
            }
        }
        sr.Close();

        // 读入单词
        var srFile = new StreamReader(File.OpenRead(filename));
        var inputs = srFile.ReadToEnd().Split(new[] { ' ', '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);

        srFile.Close();

        var stDictionary = new BinarySearchSt <int, string>();
        var stFrequency  = new BinarySearchSt <string, int>();

        foreach (var s in inputs)
        {
            if (stFrequency.Contains(s))
            {
                stFrequency.Put(s, stFrequency.Get(s) + 1);
            }
            else if (dictionary.Contains(s))
            {
                stFrequency.Put(s, 1);
                stDictionary.Put(dictionary.Get(s), s);
            }
        }

        // 输出字典序
        Console.WriteLine("Alphabet");
        foreach (var i in stDictionary.Keys())
        {
            var s = stDictionary.Get(i);
            Console.WriteLine(s + "\t" + stFrequency.Get(s));
        }

        // 频率序
        Console.WriteLine("Frequency");
        var n = stFrequency.Size();

        for (var i = 0; i < n; i++)
        {
            var max = "";
            stFrequency.Put(max, 0);
            foreach (var s in stFrequency.Keys())
            {
                if (stFrequency.Get(s) > stFrequency.Get(max))
                {
                    max = s;
                }
            }
            Console.WriteLine(max + "\t" + stFrequency.Get(max));
            stFrequency.Delete(max);
        }
    }
示例#6
0
    /// <summary>
    /// 测试方法,测试 BinarySearchST。
    /// </summary>
    /// <param name="st">用于测试的符号表。</param>
    public static void Test(BinarySearchSt <string, int> st)
    {
        var test = "S E A R C H E X A M P L E";
        var keys = test.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
        var n    = keys.Length;

        for (var i = 0; i < n; i++)
        {
            st.Put(keys[i], i);
        }

        Console.WriteLine("size = " + st.Size());
        Console.WriteLine("min = " + st.Min());
        Console.WriteLine("max = " + st.Max());
        Console.WriteLine();

        Console.WriteLine("Testing Keys()");
        Console.WriteLine("-----------------------------------");
        foreach (var s in st.Keys())
        {
            Console.WriteLine(s + " " + st.Get(s));
        }
        Console.WriteLine();

        Console.WriteLine("Testing Select()");
        Console.WriteLine("-----------------------------------");
        for (var i = 0; i < st.Size(); i++) // 循环条件不能有 '='
        {
            Console.WriteLine(i + " " + st.Select(i));
        }
        Console.WriteLine();

        Console.WriteLine("key Rank Floor Ceil");
        Console.WriteLine("-----------------------------------");
        for (var i = 'A'; i <= 'Z'; i++)
        {
            var s = i + "";
            Console.WriteLine($"{s} {st.Rank(s)} {st.Floor(s)} {st.Ceiling(s)}");
        }
        Console.WriteLine();

        for (var i = 0; i < st.Size() / 2; i++)
        {
            st.DeleteMin();
        }
        Console.WriteLine("After deleting the smallest " + st.Size() / 2 + " keys");
        Console.WriteLine("-----------------------------------");
        foreach (var s in st.Keys())
        {
            Console.WriteLine(s + " " + st.Get(s));
        }
        Console.WriteLine();

        while (!st.IsEmpty())
        {
            st.Delete(st.Select(st.Size() / 2));
        }
        Console.WriteLine("After deleting the remaining keys");
        Console.WriteLine("-----------------------------------");
        // 异常处理
        try
        {
            foreach (var s in st.Keys())
            {
                Console.WriteLine(s + " " + st.Get(s));
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("Exception: " + ex.Message);
        }
        Console.WriteLine();

        Console.WriteLine("After adding back N keys");
        Console.WriteLine("-----------------------------------");
        for (var i = 0; i < n; i++)
        {
            st.Put(keys[i], i);
        }
        foreach (var s in st.Keys())
        {
            Console.WriteLine(s + " " + st.Get(s));
        }
        Console.WriteLine();
    }