Beispiel #1
0
        public void RedBlackBSTTest3()
        {
            Alphabet alpha = Alphabet.LowerCase; // ab..z
            Random   rnd   = new Random();
            RedBlackBST <int, char> bst = new RedBlackBST <int, char>();

            int[] keys = new int[32];
            for (int i = 0; i < keys.Length; i++)
            {
                keys[i] = i * 2;
                int index = rnd.Next(alpha.Radix);
                bst[keys[i]] = alpha.ToChar(index);
            }

            int min = keys.Min();
            int max = keys.Max();

            Assert.AreEqual(max, bst.Max);
            Assert.AreEqual(min, bst.Min);

            int ceiling = keys.First(n => n >= min + 1);
            int floor   = keys.Last(n => n <= max - 1);

            Assert.AreEqual(ceiling, bst.Ceiling(min + 1));
            Assert.AreEqual(floor, bst.Floor(max - 1));

            int ith = 3;

            Assert.AreEqual(ith, bst.Rank(ith * 2));
            Assert.AreEqual(keys.Length - ith, bst.Size(ith * 2, (keys.Length - 1) * 2));
        }
Beispiel #2
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();
        }
Beispiel #3
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();
        }
Beispiel #4
0
 /**
  * Unit tests the {@code RedBlackBST} data type.
  *
  * @param args the command-line arguments
  */
 public static void main(String[] args) { 
     RedBlackBST<String, Integer> st = new RedBlackBST<String, Integer>();
     for (int i = 0; !StdIn.isEmpty(); i++) {
         String key = StdIn.readString();
         st.put(key, i);
     }
     for (String s : st.keys())
         StdOut.println(s + " " + st.get(s));
     StdOut.println();
 }
Beispiel #5
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));
    }
Beispiel #6
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();
        }
Beispiel #7
0
        public void RedBlackBSTTest2()
        {
            // testing indexer semantics
            RedBlackBST <int, string> st = new RedBlackBST <int, string>();

            st[99] = null;
            Assert.AreEqual(st.Count, 0);
            st[3] = "abc";
            st[2] = "cde";
            Assert.AreEqual(st[2], "cde");
            st[99] = null;
            st[2]  = null;
            Assert.AreEqual(st.Count, 1);
            st.Delete(3);
            Assert.AreEqual(st.Count, 0);
            try
            {
                string dummyS = st[99];
                Assert.IsNull(dummyS);
            }
            catch (Exception)
            {
                Assert.Fail("it is possible to look up an empty table");
            }

            st[4] = "def";
            Assert.IsNull(st[99]);
            Assert.IsNull(st[3]);
            Assert.IsNull(st[2]);
            st[3] = "101";
            int oldCount = st.Count;

            // delete non-existing key does nothing
            st.Delete(123456);
            Assert.AreEqual(oldCount, st.Count);

            RedBlackBST <int, int> st2 = new RedBlackBST <int, int>();

            st2[3]  = 22;
            st2[2]  = 33;
            st2[99] = 44;
            st2[2]  = 55;
            st2.Delete(3);
            Assert.IsFalse(st2.Contains(3));
            st2[3] = 101;
            Assert.AreEqual(101, st2[3]);
            st2.Delete(99);
            int dummy = st2[99]; // generate null exception
        }
        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();
        }
Beispiel #9
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();
    }
Beispiel #10
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'));
        }
Beispiel #11
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
        }