Пример #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'));
        }
Пример #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
        }