예제 #1
0
 public void tree_with_one_word_should_match_when_fed_same_word()
 {
     string expected = "Hello";
     IObservable<char> charStream = expected.ToObservable();
     var tree = new TernarySearchTree<Char>(new List<IEnumerable<char>>(new[] {"Hello"}), charStream);
     tree.Subscribe(match => Assert.AreEqual(expected, match));
 }
예제 #2
0
    public static void Main(string[] args)
    {
        var n = new BigInteger(1);
        var t = new TernarySearchTree <char>();

        t.Add("1");

        for (int i = 1; i <= 800; i++)
        {
            n = n * 2;
            t.Add(n.ToString());
        }

        int m = Convert.ToInt32(Console.ReadLine());

        for (int v = 0; v < m; ++v)
        {
            string a = Console.ReadLine();

            var c = 0;
            for (int i = 0; i < a.Length; ++i)
            {
                var matcher = new SequenceMatcher(a, i);
                c += t.Match(matcher).Count();
            }

            Console.WriteLine(c);
        }
    }
예제 #3
0
        public void tree_with_one_word_should_match_when_fed_same_word()
        {
            string             expected   = "Hello";
            IObservable <char> charStream = expected.ToObservable();
            var tree = new TernarySearchTree <Char>(new List <IEnumerable <char> >(new[] { "Hello" }), charStream);

            tree.Subscribe(match => Assert.AreEqual(expected, match));
        }
예제 #4
0
 public void tree_with_several_words_should_match_any_given_word_in_the_tree()
 {
     string expected = "cute";
     IObservable<char> charStream = expected.ToObservable();
     var tree = new TernarySearchTree<char>(new List<IEnumerable<char>>(new[] {"as", "at", "cup", "cute", "he", "i", "us"}),
                                         charStream);
     tree.Subscribe(match => Assert.AreEqual(expected, match));
 }
예제 #5
0
        public void tree_with_several_words_should_match_any_given_word_in_the_tree()
        {
            string             expected   = "cute";
            IObservable <char> charStream = expected.ToObservable();
            var tree = new TernarySearchTree <char>(new List <IEnumerable <char> >(new[] { "as", "at", "cup", "cute", "he", "i", "us" }),
                                                    charStream);

            tree.Subscribe(match => Assert.AreEqual(expected, match));
        }
예제 #6
0
        public static void DoTest()
        {
            string[] words = new string[] { "cute", "cup", "at", "as", "he", "us", "i" };

            TernarySearchTree tree = new TernarySearchTree();

            tree.Insert(words);

            Assert.Equal('c', tree.Root.Value);
            Assert.Equal('h', tree.Root.GetRightChild.Value);
            Assert.Equal('e', tree.Root.GetRightChild.GetMiddleChild.Value);
            Assert.Equal('p', tree.Root.GetMiddleChild.GetMiddleChild.GetLeftChild.Value);
            Assert.Equal('s', tree.Root.GetLeftChild.GetMiddleChild.GetLeftChild.Value);
        }
        public static IEnumerable <TernarySearchTreeNode <T> > BFS <T>(this TernarySearchTree <T> @this)
        {
            Queue <TernarySearchTreeNode <T> > queue = new Queue <TernarySearchTreeNode <T> >();

            queue.EnQueue(@this.Root);
            while (queue.Size != 0)
            {
                var node = queue.DeQueue();
                yield return(node);

                if (node != null)
                {
                    queue.EnQueue(node.Left);
                    queue.EnQueue(node.Equal);
                    queue.EnQueue(node.Right);
                }
            }
        }
        public void TernarySearchTree_Smoke_Test()
        {
            var searchTree = new TernarySearchTree <char>();

            searchTree.Insert("cat".ToCharArray());
            searchTree.Insert("cats".ToCharArray());
            searchTree.Insert("cut".ToCharArray());
            searchTree.Insert("cuts".ToCharArray());
            searchTree.Insert("up".ToCharArray());
            searchTree.Insert("bug".ToCharArray());
            searchTree.Insert("bugs".ToCharArray());

            Assert.IsTrue(searchTree.Contains("cat".ToCharArray()));
            Assert.IsTrue(searchTree.Contains("cut".ToCharArray()));
            Assert.IsFalse(searchTree.Contains("bu".ToCharArray()));

            searchTree.Delete("cuts".ToCharArray());
            Assert.IsFalse(searchTree.Contains("cuts".ToCharArray()));

            var matches = searchTree.StartsWith("u".ToCharArray());

            Assert.IsTrue(matches.Count == 1);

            matches = searchTree.StartsWith("cu".ToCharArray());
            Assert.IsTrue(matches.Count == 1);

            matches = searchTree.StartsWith("bu".ToCharArray());
            Assert.IsTrue(matches.Count == 2);

            matches = searchTree.StartsWith("c".ToCharArray());
            Assert.IsTrue(matches.Count == 3);

            matches = searchTree.StartsWith("ca".ToCharArray());
            Assert.IsTrue(matches.Count == 2);

            searchTree.Delete("cats".ToCharArray());
            searchTree.Delete("up".ToCharArray());
            searchTree.Delete("bug".ToCharArray());
            searchTree.Delete("bugs".ToCharArray());
            searchTree.Delete("cat".ToCharArray());
            searchTree.Delete("cut".ToCharArray());
        }
        public void TernarySearchTree_Test()
        {
            var tree = new TernarySearchTree <char>();

            var testCount = 1000;

            var testStrings = new List <string>();

            while (testCount > 0)
            {
                var testString = randomString(3);
                testStrings.Add(testString);
                testCount--;
            }

            testStrings = new List <string>(testStrings.Distinct());

            foreach (var testString in testStrings)
            {
                tree.Insert(testString.ToArray());
            }

            //IEnumerable test
            Assert.AreEqual(tree.Count, tree.Count());

            foreach (var item in tree)
            {
                var existing = new string(item);
                Assert.IsTrue(testStrings.Contains(existing));
            }

            foreach (var item in testStrings)
            {
                Assert.IsTrue(tree.Contains(item.ToArray()));
            }

            foreach (var testString in testStrings)
            {
                tree.Delete(testString.ToArray());
            }
        }
예제 #10
0
 public void Setup()
 {
     sampleTree = new TernarySearchTree();
     sampleKeys.ForEach(key => sampleTree.Insert(key));
 }
 public static IEnumerable <TR> BFS <TI, TR>(this TernarySearchTree <TI> @this, Func <TernarySearchTreeNode <TI>, TR> selectFunc) where TR : class
 {
     return(@this.BFS().Select(x => x != null ? selectFunc.Invoke(x) : null));
 }
 public void TearDown()
 {
     this.tree = null;
 }
 public void SetUp()
 {
     this.tree = new TernarySearchTree <int>();
 }
        public void TernarySearchTree_Smoke_Test()
        {
            var tree = new TernarySearchTree <char>();

            tree.Insert("cat".ToCharArray());

            //IEnumerable test
            Assert.AreEqual(tree.Count, tree.Count());

            tree.Insert("cats".ToCharArray());

            //IEnumerable test
            Assert.AreEqual(tree.Count, tree.Count());

            tree.Insert("cut".ToCharArray());

            //IEnumerable test
            Assert.AreEqual(tree.Count, tree.Count());

            tree.Insert("cuts".ToCharArray());

            //IEnumerable test
            Assert.AreEqual(tree.Count, tree.Count());

            tree.Insert("up".ToCharArray());

            //IEnumerable test
            Assert.AreEqual(tree.Count, tree.Count());

            tree.Insert("bug".ToCharArray());

            //IEnumerable test
            Assert.AreEqual(tree.Count, tree.Count());

            tree.Insert("bugs".ToCharArray());

            //IEnumerable test
            Assert.AreEqual(tree.Count, tree.Count());

            //IEnumerable test
            Assert.AreEqual(tree.Count, tree.Count());

            Assert.IsTrue(tree.Contains("cat".ToCharArray()));
            Assert.IsTrue(tree.Contains("cut".ToCharArray()));
            Assert.IsFalse(tree.Contains("bu".ToCharArray()));
            Assert.IsTrue(tree.ContainsPrefix("bu".ToCharArray()));

            tree.Delete("cuts".ToCharArray());
            Assert.IsFalse(tree.Contains("cuts".ToCharArray()));

            var matches = tree.StartsWith("u".ToCharArray());

            Assert.IsTrue(matches.Count == 1);

            //IEnumerable test
            Assert.AreEqual(tree.Count, tree.Count());

            matches = tree.StartsWith("cu".ToCharArray());
            Assert.IsTrue(matches.Count == 1);

            matches = tree.StartsWith("bu".ToCharArray());
            Assert.IsTrue(matches.Count == 2);

            matches = tree.StartsWith("c".ToCharArray());
            Assert.IsTrue(matches.Count == 3);

            matches = tree.StartsWith("ca".ToCharArray());
            Assert.IsTrue(matches.Count == 2);

            tree.Delete("cats".ToCharArray());

            //IEnumerable test
            Assert.AreEqual(tree.Count, tree.Count());


            tree.Delete("up".ToCharArray());

            //IEnumerable test
            Assert.AreEqual(tree.Count, tree.Count());

            tree.Delete("bug".ToCharArray());

            //IEnumerable test
            Assert.AreEqual(tree.Count, tree.Count());

            tree.Delete("bugs".ToCharArray());

            //IEnumerable test
            Assert.AreEqual(tree.Count, tree.Count());

            tree.Delete("cat".ToCharArray());

            //IEnumerable test
            Assert.AreEqual(tree.Count, tree.Count());

            tree.Delete("cut".ToCharArray());

            //IEnumerable test
            Assert.AreEqual(tree.Count, tree.Count());
        }