Exemplo n.º 1
0
        public void RedBlackBSTPut()
        {
            var bst = new RedBlackBST <char, int>();
            var idx = 0;

            foreach (var c in "SEARCHXMPL".ToCharArray())
            {
                bst.Put(c, ++idx);
            }

            //the interface doesn't really expose if the tree is balanced, so I'll
            //just check things are ranked where I expect them to be
            Assert.AreEqual('A', bst.Select(0));
            Assert.AreEqual('E', bst.Select(2));
            Assert.AreEqual('R', bst.Select(7));
            Assert.AreEqual('X', bst.Select(9));

            Assert.AreEqual(1, bst.Get('S'));
            Assert.AreEqual(10, bst.Get('L'));
        }
Exemplo n.º 2
0
        public void RedBlackBSTTest1()
        {
            // testing Get/Put semantics
            RedBlackBST <string, int> st = new RedBlackBST <string, int>();

            Assert.IsTrue(st.IsEmpty);

            // making sure we can delete all from ST
            st.Put("asd", 355);
            st.Put("dsd", 25);
            st.Put("esd", 15);
            while (st.Count > 0)
            {
                string k = st.Min;
                st.Delete(k);
            }

            string[] keys = { "to", "be", "or", "not", "to", "be", "is", "quest" };
            for (int i = 0; i < keys.Length; i++)
            {
                st.Put(keys[i], i);
            }
            Assert.IsTrue(!(st.IsEmpty) && (st.Count == 6));

            string key = "not";

            Assert.IsTrue(st.Contains(key));
            st.Delete(key);
            Assert.IsFalse(st.Contains(key));
            Assert.IsNull(st.Get(key));

            object value = st.Get("is");

            Assert.AreEqual(6, value);
            Assert.AreEqual("quest", st.Select(3));

            value = st.Get("world");
            Assert.IsNull(value);

            int dummy = (int)st.Get("hello"); // generate null exception
        }
Exemplo n.º 3
0
    public static void Main(string[] args)
    {
        RedBlackBST <int, int> bst = new RedBlackBST <int, int> (com);

        bst.Put(7, 7);
        bst.Put(6, 6);
        bst.Put(3, 3);
        bst.Put(5, 5);
        bst.Put(4, 4);
        bst.Print();
        Console.WriteLine(bst.Get(4));
    }
Exemplo n.º 4
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 redBlackBST = new RedBlackBST <string, string>();


            foreach (var keyValue in keyValues)
            {
                var splittedKeyValue = keyValue.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                var key   = splittedKeyValue[0];
                var value = splittedKeyValue[1];
                redBlackBST.Put(key, value);
            }
            // print results
            foreach (var item in redBlackBST.Keys())
            {
                Console.WriteLine(redBlackBST.Get(item));
            }
            Console.WriteLine(redBlackBST.Max());
            Console.WriteLine(redBlackBST.Min());
            Console.ReadLine();
        }
Exemplo n.º 5
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 redBlackBST = new RedBlackBST<string,string>();

            foreach (var keyValue in keyValues)
            {
                var splittedKeyValue = keyValue.Split(new[] {' '}, StringSplitOptions.RemoveEmptyEntries);
                var key = splittedKeyValue[0];
                var value = splittedKeyValue[1];
                redBlackBST.Put(key,value);
            }
            // print results
            foreach (var item in redBlackBST.Keys())
            {
                Console.WriteLine(redBlackBST.Get(item));
            }
            Console.WriteLine(redBlackBST.Max());
            Console.WriteLine(redBlackBST.Min());
            Console.ReadLine();
        }