Ejemplo n.º 1
0
        public void Insert(string insertType, STBase <int, string> st, int count)
        {
            switch (insertType)
            {
            case "asc":
                for (int i = 0; i < count; i++)
                {
                    st.Put(i, "a");
                }
                break;

            case "desc":
                for (int i = count; i > 0; i--)
                {
                    st.Put(i, "a");
                }
                break;

            case "random":
                Random random = new Random();
                for (int i = 0; i < count; i++)
                {
                    int j = random.Next(0, count);
                    st.Put(j, "a");
                }
                break;

            default:
                break;
            }
        }
Ejemplo n.º 2
0
        public STBase <int, string> GetST(int n, string stType)
        {
            STBase <int, string> st = null;

            switch (stType)
            {
            case "binarysearch":
                st = new BinarySearchST <int, string>(n);
                break;

            case "bst":
            case "bstloop":
                st = new BinarySearchTree_Loop <int, string>();
                break;

            case "bstrecur":
                st = new BinarySearchTree_Recur <int, string>();
                break;

            case "hash":
            case "schash":
                st = new SeparateChainingHashST <int, string>();
                break;

            case "lphash":
                st = new LinearProbingHashST <int, string>();
                break;

            default:
                break;
            }
            return(st);
        }
Ejemplo n.º 3
0
        public void Run(STBase <string, int> st)
        {
            string s = "SEARCHEXAMPLE";
            //string s = "SEARCHX";
            var chars = s.ToCharArray();

            for (int i = 0; i < chars.Length; i++)
            {
                st.Put(chars[i].ToString(), i);
            }
            foreach (string key in st.Keys())
            {
                Console.Write("{0}:{1} ", key, st.Get(key));
            }
            Console.WriteLine();
            Console.WriteLine(st.Size());

            //st.Delete("C");
            //var v = st.Get("H");
            //Console.WriteLine(v);

            //for (int i = 0; i < chars.Length; i++)
            //{
            //    string k = chars[i].ToString();
            //    Console.WriteLine("Delete {0}", k);
            //    st.Delete(k);
            //    foreach (string key in st.Keys())
            //    {
            //        Console.Write("{0}:{1} ", key, st.Get(key));
            //    }
            //    Console.WriteLine();
            //}
        }
Ejemplo n.º 4
0
        public void PerfRun(string stType, string insertType)
        {
            System.Console.WriteLine("{0} symbole table, {1} insert order", stType, insertType);
            int count = 1000 * 1000;
            STBase <int, string> st = GetST(count, stType);
            Stopwatch            sw = Stopwatch.StartNew();

            Insert(insertType, st, count);
            System.Console.WriteLine(sw.Elapsed);
        }
Ejemplo n.º 5
0
        public void Run(STBase <string, int> st, string path, int minlen)
        {
            int       wordCount = 0;
            Stopwatch watch     = Stopwatch.StartNew();

            using (Stream stream = new FileStream(path, FileMode.Open))
                using (StreamReader reader = new StreamReader(stream))
                {
                    string   content = reader.ReadToEnd();
                    string[] lines   = content.Split('\n', '\r');
                    foreach (string line in lines)
                    {
                        if (!string.IsNullOrEmpty(line))
                        {
                            string[] words = line.Split(' ');
                            foreach (string word in words)
                            {
                                if (word.Length < minlen)
                                {
                                    continue;
                                }
                                else
                                {
                                    wordCount++;
                                    if (st.Contains(word))
                                    {
                                        st.Put(word, st.Get(word) + 1);
                                    }
                                    else
                                    {
                                        st.Put(word, 1);
                                    }
                                }
                            }
                        }
                    }
                }

            string max = "";

            st.Put(max, 0);
            int keyCount = 0;

            foreach (string key in st.Keys())
            {
                keyCount++;
                if (st.Get(key) > st.Get(max))
                {
                    max = key;
                }
            }
            Console.WriteLine("Words: {0}, Keys: {1}", wordCount, keyCount);
            Console.WriteLine("{0} {1} ({2}ms)", max, st.Get(max), watch.ElapsedMilliseconds);
        }