Exemple #1
0
        static void Main(string[] args)
        {
            //List<Card> trips = new List<Card>()
            //{
            //    new Card() { From = "Мельбурн", To = "Кельн" },
            //    new Card() { From = "Москва", To = "Париж" },
            //    new Card() { From = "Кельн", To = "Москва" },
            //};
            //SortCardAlgorithm sca = new SortCardAlgorithm(trips);

            //var result = sca.SortFirstMethod();

            //foreach(var card in result)
            //{
            //    Console.WriteLine("Город отправления {0} -> город назначения {1}", card.From, card.To);
            //}
            //Console.ReadKey();

            Tries <int> tr = new Tries <int>(new[]
            {
                new Tries <int> .Item()
                {
                    Word = "Москва", Index = 0
                },
                new Tries <int> .Item()
                {
                    Word = "Спб", Index = 1
                },
                new Tries <int> .Item()
                {
                    Word = "Казань", Index = 2
                },
                new Tries <int> .Item()
                {
                    Word = "Пенза", Index = 3
                }
            });

            Console.WriteLine("Найдено слово {0}", tr.Get("пенза").Result == SearchResultTstEnum.Found);
            Console.WriteLine("Найдено слово {0}", tr.Get("пенза1").Result == SearchResultTstEnum.Found);

            Console.ReadKey();
        }
        /// <summary>
        /// сортировка с использование гибридного суффиксного дерева,  TST with R^2
        /// сложность 2 * Sum(L + ln(n)) + Sum(L + ln(n)) ~ NlnN, потребление памяти 4N
        /// быстрее, чем хэширование
        /// </summary>
        /// <returns></returns>
        public List <Card> SortSecondMethod()
        {
            var result = InitResult();

            if (_cards.Count == 1)
            {
                return(result);
            }

            Tries <int> tr = new Tries <int>();

            for (int i = 1; i < _cards.Count; i++)
            {
                var town = _cards[i].From;
                if (tr.Get(town).Result == SearchResultTstEnum.Found)
                {
                    throw new RecursivePathException("В массиве карточек присутствую циклы");
                }
                tr.Put(new Tries <int> .Item()
                {
                    Word = _cards[i].From, Index = i
                });
            }

            var currentTown = _cards[0].To;

            for (int i = 1; i < _cards.Count; i++)
            {
                var elem = tr.Get(currentTown);
                if (elem.Result == SearchResultTstEnum.NotFound)
                {
                    throw new InvalidPathException("В массиве карточек присутствуют разрывы");
                }

                result.Add(new Card()
                {
                    From = _cards[elem.Value].From.Clone() as string, To = _cards[elem.Value].To.Clone() as string
                });
                currentTown = _cards[elem.Value].To;
            }

            return(result);
        }