예제 #1
0
        static void FindCities(string city, CityTrie cityDb)
        {
            CityTrie cityDbHelper = cityDb;
            int      i;

            for (i = 0; i < city.Length; i++)
            {
                if (cityDbHelper.Leafs.ContainsKey(city[i]))
                {
                    cityDbHelper = cityDbHelper.Leafs[city[i]];
                }
                else
                {
                    break;
                }
            }

            if (i == city.Length)
            {
                if (cityDbHelper.End)
                {
                    Console.WriteLine(city);
                }

                GetCitiesFromDb(city, cityDbHelper);
            }
        }
예제 #2
0
        static void Main(string[] args)
        {
            CityTrie cityDb = new CityTrie('\0');

            CreateInput(ref cityDb);
            FindCities("set", cityDb);
            FindCities("San", cityDb);
            FindCities("Ne", cityDb);
        }
예제 #3
0
 static string GetCitiesFromDb(string s, CityTrie cityDb)
 {
     if (cityDb.Leafs.Count == 1)
     {
         return(s);
     }
     else
     {
         foreach (var city in cityDb.Leafs)
         {
             return(s + GetCitiesFromDb(s, city.Leafs[c]));
         }
     }
 }
예제 #4
0
        static void AddCityToDataSet(string city, ref CityTrie cityDb)
        {
            CityTrie cityDbHelper = cityDb;

            for (int i = 0; i < city.Length; i++)
            {
                if (!cityDbHelper.Leafs.ContainsKey(city[i]))
                {
                    cityDbHelper.Leafs.Add(city[i], new CityTrie('\0'));
                }

                cityDbHelper = cityDbHelper.Leafs[city[i]];
                if (i == city.Length - 1)
                {
                    cityDbHelper.End = true;
                }
            }
        }
예제 #5
0
        static void CreateInput(ref CityTrie cityDb)
        {
            // input creation
            List <string> cities = new List <string>
            {
                "san francisco",
                "san diego",
                "san antonio",
                "san",
                "new york",
                "new babylon",
                "newly"
            };

            foreach (string city in cities)
            {
                AddCityToDataSet(city, ref cityDb);
            }
        }