Ejemplo n.º 1
0
        public void TestRedBlackBst() {
            var bst = new RedBlackBST<string, string>();
            bst.Put("A", "A");
            bst.Put("B", "B");
            bst.Put("C", "C");
            bst.Put("D", "D");
            bst.Put("E", "E");
            bst.Put("F", "F");
            bst.Put("G", "G");
            bst.Put("H", "H");
            bst.Put("I", "I");
            bst.Put("J", "J");

            Assert.IsFalse(bst.IsEmpty);
            Assert.AreEqual(10, bst.Count);
            Assert.AreEqual(3, bst.Height);

            StdOut.WriteLine(bst.LevelOrder());

            //bst.DeleteMax();
            //StdOut.WriteLine(bst.LevelOrder());

            while (bst.Count > 1) {
                bst.DeleteMin();
                StdOut.WriteLine(bst.LevelOrder()); 
            }

            bst.DeleteMin();
        }
Ejemplo 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
        }
Ejemplo n.º 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 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();
        }
Ejemplo 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();
        }
Ejemplo n.º 5
0
    void RedBlackBSTTest()
    {
        RedBlackBST <string, int> bst = new RedBlackBST <string, int>();

        for (int i = 0; i < k.Length; i++)
        {
            bst.Put(k[i], v[i]);
        }
        bst.Display();
        //Debug.Log("删除最小键");
        //bst.DelMin();
        //bst.Display();
        //Debug.Log("删除最大键");
        //bst.DelMax();
        //bst.Display();
        Debug.Log("删除d,4");
        bst.DelNode("d");
        bst.Display();
    }
Ejemplo n.º 6
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'));
        }