public static int Height(PointTree point) { if (point == null) { return(0); } // Находим высоту правой и левой ветки, и из них берем максимальную return(1 + Math.Max(Height(point.left), Height(point.right))); }
static public int CountElements(PointTree p) { if (p == null || p.left == null && p.right == null) { return(1); } int left = p.left != null?CountElements(p.left) : 0; int right = p.right != null?CountElements(p.right) : 0; return(left + right + 1); }
static void ShowMenu(PointTree tree) { while (true) { Console.WriteLine(); int min = 1; int max = 4; Console.WriteLine(@"Выберите действие:"); Console.WriteLine("1) Создание сбалансированного дерева"); Console.WriteLine("2) Печать дерева"); Console.WriteLine("3) Добавить элемент в сбалансированное дерево"); Console.WriteLine("4) Выход из программы"); int method = InputInt(min, max); Console.Clear(); switch (method) { case 1: Console.WriteLine("Введите число элементов дерева от 0 до 50:"); tree = PointTree.IdealTree(InputInt(0, 50)); Console.WriteLine("Дерево создано"); break; case 2: if (tree == null) { Console.WriteLine("Дерево пустое"); break; } PointTree.Print(tree); break; case 3: Console.WriteLine("Введите элемент для добавления от -2000 до 2000:"); tree = PointTree.Add(tree, InputInt(-2000, 2000)); Console.WriteLine("Элемент добавлен"); break; case 4: return; } } }
public static void Print(PointTree p, int l = 0) { if (p != null) { // Переход к левому поддереву Print(p.left, l + 3); for (int i = 0; i < l; i++) { Console.Write(" "); } Console.WriteLine(p.data); // Переход к правому поддереву Print(p.right, l + 3); } }
public static PointTree IdealTree(int size) { PointTree p; int nl, nr; if (size == 0) { return(null); } nl = size / 2; nr = size - nl - 1; p = new PointTree(rand.Next(-2000, 2000)); p.left = IdealTree(nl); p.right = IdealTree(nr); return(p); }
public static PointTree Add(PointTree root, double num) { if (root == null) { return(new PointTree(num)); } bool isExist = num == root.data; if (isExist) { Console.WriteLine("Объект с таким числом уже есть в дереве: добавление невозможно"); return(root); } if (Height(root.left) < Height(root.right)) { root.left = Add(root.left, num); } else if (Height(root.left) > Height(root.right)) { root.right = Add(root.right, num); } else { if (CountElements(root.left) < CountElements(root.right)) { root.left = Add(root.left, num); } else { root.right = Add(root.right, num); } } return(root); }
public PointTree(double num) { data = num; left = null; right = null; }
static void Main(string[] args) { PointTree tree = null; ShowMenu(tree); }