示例#1
0
        public void SeparateChainingHashPut()
        {
            //force lots of collisions by picking a small modulo for the hashing function
            var bst = new SeparateChainingHashST <string, int>(5);

            for (char i = 'a'; i <= 'z'; i++)
            {
                bst.Put(new string(new char[] { i, i, i }), i);
            }
            Assert.AreEqual(110, bst.Get("nnn"));
        }
示例#2
0
        static void Main(string[] args)
        {
            ST <string, string> st;

            st = new SequentialSearchST <string, string>();
            st.Put("a", "1");
            st.Put("b", "2");
            int size = st.Size();

            st.Delete("a");
            size = st.Size();

            SortedST <string, string> sortedSt;

            sortedSt = new BST <string, string>();
            sortedSt.Put("r", "1");
            sortedSt.Put("b", "2");
            sortedSt.Put("h", "3");
            sortedSt.Put("t", "4");
            sortedSt.Delete("b");

            st = new SeparateChainingHashST <string, string>();
            st.Put("f", "1");
            st.Put("a", "3");
            st.Put("d", "4");
            st.Put("h", "9");
            st.Put("k", "8");
            var v = st.Get("h");

            st = new LinearProbingHashST <string, string>();
            st.Put("f", "1");
            st.Put("a", "3");
            st.Put("d", "4");
            st.Put("h", "9");
            st.Put("u", "8");
            v = st.Get("u");

            Console.ReadKey();
        }
示例#3
0
        public void Run()
        {
            Console.WriteLine("Choose file:");   // Prompt
            Console.WriteLine("1 - tinyST.txt"); // Prompt
            Console.WriteLine("or quit");        // Prompt

            var fileNumber = Console.ReadLine();
            var fieName    = string.Empty;

            switch (fileNumber)
            {
            case "1":
                fieName = "tinyST.txt";
                break;

            case "quit":
                return;

            default:
                return;
            }


            var @in       = new In($"Files\\Searching\\{fieName}");
            var keyValues = @in.ReadAllLines();

            //var list = words.Select(word => new StringComparable(word)).ToList();

            //var listComparable = list.Cast<IComparable>().ToList();
            //var arrayComparable = list.Cast<IComparable>().ToArray();
            //var listStrings = words.ToList();


            var st = new SeparateChainingHashST <string, string>();


            foreach (var keyValue in keyValues)
            {
                var splittedKeyValue = keyValue.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                var key   = splittedKeyValue[0];
                var value = splittedKeyValue[1];
                st.Put(key, value);
            }
            // print results
            foreach (var item in st.Keys())
            {
                Console.WriteLine(item + " " + st.Get(item));
            }
            Console.ReadLine();
        }
        public void SeparateChainingHashSTTest()
        {
            SeparateChainingHashST<IComparable, string> st = new SeparateChainingHashST<IComparable, string>();
            string[] searchExample = new[] { "S", "E", "A", "R", "C", "H", "E", "X", "A", "M", "P", "L" };
            foreach (string val in searchExample)
            {
                st.Put(val, val);
            }

            foreach (string s in searchExample)
            {
                Assert.AreEqual(st.Get(s), s);
            }
        }
示例#5
0
        public void TestSeparateChainingHashST()
        {
            SeparateChainingHashST<string, int> st = new SeparateChainingHashST<string, int>();

            int amounts = 20;
            string[] strs = new string[amounts];
            Random rand = new Random();
            for (int i = 0; i < amounts; i++)
            {
                strs[i] = Convert.ToChar(rand.Next(97, 122)).ToString();
                st.Put(strs[i], i);
            }
            StringBuilder sb0 = new StringBuilder();
            foreach (var str in strs)
                sb0.Append(str + " ");
            Debug.WriteLine(sb0.ToString());

            StringBuilder sb = new StringBuilder();
            foreach (var item in st.Keys())
                sb.Append(item + "-" + st.Get(item) + " ");
            Debug.WriteLine(sb.ToString());

            Assert.AreEqual(strs.Distinct().Count(), st.Keys().Count());
        }