예제 #1
0
        static void Main(string[] args)
        {
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("Балансировка бинарного дерева при добавлении в него узла");
            Console.ResetColor();
            Console.Write("Введите значение верхнего узла: ");
            int n = InputNumber();
            IdealBalanceBinaryThree <int> three = new IdealBalanceBinaryThree <int>(n);

            Console.Write("Сколько элементов добавить в дерево: ");
            int q = InputNumber();

            for (int i = 0; i < q; i++)
            {
                three.Add(i);
            }
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("Печать дерева:");
            Console.ResetColor();
            IdealBalanceBinaryThree <int> .ShowTree(three);

            Console.WriteLine("Количество элементов в дереве: {0}", three.Count);
            Console.WriteLine($"Количество элементов в правом поддереве: {three.SeachCountForRight(three)}");
            Console.WriteLine($"Количество элементов в правом поддереве: {three.SeachCountForLeft(three)}");
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("Доп.функция - высота дерева равна: {0}", three.SearchHeight(three));
            Console.ResetColor();
            Console.ReadLine();
        }
        public int SearchHeight(IdealBalanceBinaryThree <T> p)
        {
            int high = 0;

            Run(p);
            high = 1 + (int)Math.Log(height, 2);
            return(high);
        }
 //печать дерева
 public static void ShowTree(IdealBalanceBinaryThree <T> p)
 {
     if (p != null)
     {
         ShowTree(p.left);
         Console.WriteLine(p.data + " ");
         ShowTree(p.right);
     }
 }
 int Run(IdealBalanceBinaryThree <T> p)
 {
     if (p != null)
     {
         height++;
         Run(p.left);
         Run(p.right);
     }
     return(height);
 }
        //поиск количества узлов левого поддерева
        public int SeachCountForLeft(IdealBalanceBinaryThree <T> p)
        {
            int count = -1;

            if (p != null)
            {
                count++;
                count += SearchCount(p.left);
            }
            return(count);
        }
        //добавление элемента к конечному узлу
        public IdealBalanceBinaryThree <T> AddToSubThree(T item)
        {
            IdealBalanceBinaryThree <T> begin = this;
            IdealBalanceBinaryThree <T> temp  = new IdealBalanceBinaryThree <T>(item);

            if (begin.right == null && begin.left != null)
            {
                begin.right = temp;
            }
            else
            {
                begin.left = temp;
            }
            return(begin);
        }
        public IdealBalanceBinaryThree <T> Add(T item)
        {
            IdealBalanceBinaryThree <T> begin = this;
            IdealBalanceBinaryThree <T> temp  = begin;

            if (begin.right == null && begin.left == null)
            {
                begin.right = new IdealBalanceBinaryThree <T>(item);
                return(begin);
            }
            if (begin.right == null && begin.left != null)
            {
                begin.right = new IdealBalanceBinaryThree <T>(item);
                return(begin);
            }
            if (begin.right != null && begin.left == null)
            {
                begin.left = new IdealBalanceBinaryThree <T>(item);
                return(begin);
            }
            if (begin.right != null && begin.left != null)
            {
                if (begin.SeachCountForRight(begin) == begin.SeachCountForLeft(begin))
                {
                    temp = begin.right;
                    temp.Add(item);
                    return(begin);
                }
                if (begin.SeachCountForRight(begin) > begin.SeachCountForLeft(begin))
                {
                    temp = begin.left;
                    temp.Add(item);
                    return(begin);
                }
                if (begin.SeachCountForRight(begin) < begin.SeachCountForLeft(begin))
                {
                    temp = begin.right;
                    temp.Add(item);
                    return(begin);
                }
            }
            return(begin);
        }
 //конструктор с начальным информационным полем
 public IdealBalanceBinaryThree(T FirstItem)
 {
     data  = FirstItem;
     right = null;
     left  = null;
 }
 //дефолтный конструктор
 public IdealBalanceBinaryThree()
 {
     data  = default;
     right = null;
     left  = null;
 }