// Построение идеально сбалансированного дерева public static PointTree IdealTree(int size, PointTree p) { PointTree r; int nl, nr; if (size == 0) { p = null; return(p); } nl = size / 2; nr = size - nl - 1; r = new PointTree(); int typeclass = random.Next(1, 4); switch (typeclass) { case 1: { Place place = new Place(); place.RandomCreate(); Console.WriteLine("The element {0} is adding...", place.ToString()); r = first(place); } break; case 2: { Region region = new Region(); region.RandomCreate(); Console.WriteLine("The element {0} is adding...", region.ToString()); r = first(region); } break; case 3: { City city = new City(); city.RandomCreate(); Console.WriteLine("The element {0} is adding...", city.ToString()); r = first(city); } break; case 4: { Megapolis megapolis = new Megapolis(); megapolis.RandomCreate(); Console.WriteLine("The element {0} is adding...", megapolis.ToString()); r = first(megapolis); } break; } r.left = IdealTree(nl, r.left); r.right = IdealTree(nr, r.right); return(r); }
// Добавление элемента в одноправленный список public static PointOne AddPoint(PointOne beg, int number) { int typeclass = random.Next(1, 4); PointOne point = new PointOne(); switch (typeclass) { case 1: { Place place = new Place(); place.RandomCreate(); Console.WriteLine("The element {0} is adding...", place.ToString()); point = MakePoint(place); } break; case 2: { Region region = new Region(); region.RandomCreate(); Console.WriteLine("The element {0} is adding...", region.ToString()); point = MakePoint(region); } break; case 3: { City city = new City(); city.RandomCreate(); Console.WriteLine("The element {0} is adding...", city.ToString()); point = MakePoint(city); } break; case 4: { Megapolis megapolis = new Megapolis(); megapolis.RandomCreate(); Console.WriteLine("The element {0} is adding...", megapolis.ToString()); point = MakePoint(megapolis); } break; } if (beg == null) { switch (typeclass) { case 1: { Place place = new Place(); place.RandomCreate(); beg = MakePoint(place); } break; case 2: { Region region = new Region(); region.RandomCreate(); beg = MakePoint(region); } break; case 3: { City city = new City(); city.RandomCreate(); beg = MakePoint(city); } break; case 4: { Megapolis megapolis = new Megapolis(); megapolis.RandomCreate(); beg = MakePoint(megapolis); } break; } return(beg); } if (number == 1) { point.next = beg; beg = point; return(beg); } // Вспом. переменная для прохода по списку PointOne p = beg; // Идем по списку до нужного элемента for (int i = 1; i < number - 1 && p != null; i++) { p = p.next; } if (p == null) //элемент не найден { Console.WriteLine("Error! The size of List less than Number"); return(beg); } // Lобавляем новый элемент point.next = p.next; p.next = point; return(beg); }
// Добавление в конец однонаправленного списка public static PointOne MakeListToEnd(int size) { int typeclass = random.Next(1, 4); PointOne beg = new PointOne(); switch (typeclass) { case 1: { Place place = new Place(); place.RandomCreate(); Console.WriteLine("The element {0} is adding...", place.ToString()); beg = MakePoint(place); } break; case 2: { Region region = new Region(); region.RandomCreate(); Console.WriteLine("The element {0} is adding...", region.ToString()); beg = MakePoint(region); } break; case 3: { City city = new City(); city.RandomCreate(); Console.WriteLine("The element {0} is adding...", city.ToString()); beg = MakePoint(city); } break; case 4: { Megapolis megapolis = new Megapolis(); megapolis.RandomCreate(); Console.WriteLine("The element {0} is adding...", megapolis.ToString()); beg = MakePoint(megapolis); } break; } PointOne r = beg; for (int i = 1; i < size; i++) { typeclass = random.Next(1, 4); PointOne point = new PointOne(); switch (typeclass) { case 1: { Place place = new Place(); place.RandomCreate(); Console.WriteLine("The element {0} is adding...", place.ToString()); point = MakePoint(place); } break; case 2: { Region region = new Region(); region.RandomCreate(); Console.WriteLine("The element {0} is adding...", region.ToString()); point = MakePoint(region); } break; case 3: { City city = new City(); city.RandomCreate(); Console.WriteLine("The element {0} is adding...", city.ToString()); point = MakePoint(city); } break; case 4: { Megapolis megapolis = new Megapolis(); megapolis.RandomCreate(); Console.WriteLine("The element {0} is adding...", megapolis.ToString()); point = MakePoint(megapolis); } break; } r.next = point; r = point; } return(beg); }