示例#1
0
 public MyTree Postfix(MyTree tree)
 {
     if (tree.leftKnot != null)
     {
         Postfix(tree.leftKnot);
     }
     if (tree.rightKnot != null)
     {
         Postfix(tree.rightKnot);
     }
     Console.Write(tree.value + " ");
     return(tree);
 }
示例#2
0
 public MyTree Prefix(MyTree tree)
 {
     Console.Write(tree.value + " ");
     if (tree.leftKnot != null)
     {
         Prefix(tree.leftKnot);
     }
     if (tree.rightKnot != null)
     {
         Prefix(tree.rightKnot);
     }
     return(tree);
 }
示例#3
0
 public MyTree Infix(MyTree tree)
 {
     if (tree.leftKnot != null)
     {
         Infix(tree.leftKnot);
     }
     Console.Write(tree.value + " ");
     if (tree.rightKnot != null)
     {
         Infix(tree.rightKnot);
     }
     return(tree);
 }
示例#4
0
        static MyTree NRPrefix(MyTree tree)
        {
            MyTree tags = new MyTree();

            tags.leftKnot         = new MyTree();
            tags.leftKnot.parent  = tags;
            tags.rightKnot        = new MyTree();
            tags.rightKnot.parent = tags;
point2:
            tags.value = "0_O";
            Console.Write(tree.value + " ");
point3:
            bool flag = true;

            if (tree.leftKnot != null && tags.leftKnot.value != "0_O")
            {
                tree                  = tree.leftKnot;
                tags                  = tags.leftKnot;
                tags.leftKnot         = new MyTree();
                tags.rightKnot        = new MyTree();
                tags.leftKnot.parent  = tags;
                tags.rightKnot.parent = tags;
                flag                  = false;
            }
            if (!flag)
            {
                goto point2;
            }
            if (tree.rightKnot != null && tags.rightKnot.value != "0_O")
            {
                tree                  = tree.rightKnot;
                tags                  = tags.rightKnot;
                tags.leftKnot         = new MyTree();
                tags.rightKnot        = new MyTree();
                tags.leftKnot.parent  = tags;
                tags.rightKnot.parent = tags;
                flag                  = false;
            }
            if (!flag)
            {
                goto point2;
            }
            if (tree.parent != null)
            {
                tree = tree.parent;
                tags = tags.parent;
                goto point3;
            }
            return(tree);
        }
示例#5
0
 private static int u          = 0; //для  HowLevels
 private void HowFindLevels(MyTree tree)
 {
     if (tree.leftKnot != null)
     {
         u++;
         HowFindLevels(tree.leftKnot);
     }
     if (tree.rightKnot != null)
     {
         u++;
         HowFindLevels(tree.rightKnot);
     }
     HowLevels = Math.Max(HowLevels, u);
     u--;
 }
示例#6
0
        public MyTree Filling()
        {
            Console.WriteLine("Введите количество элементов");
            int    n    = int.Parse(Console.ReadLine());
            MyTree tree = new MyTree();

            WithoutBalanceTree = new MyTree();
            Console.WriteLine("Введите значение корня");
            tree.value = Console.ReadLine();
            CountElements++;
            ppp  = n;
            tree = ForFilling(tree);
            HowFindLevels(tree);
            return(tree);
        }
示例#7
0
        static void Main(string[] args)
        {
            MyTree Tree = new MyTree();

            Console.WriteLine("Выберите:\n1.Рекурсивное построение идеально сбалансированного бинарного дерева\n для N узлов хранящихся в файле");
            Console.WriteLine("2.Задание дерева вручную");
            int n = int.Parse(Console.ReadLine());

            string[] arrayEl = new string[0];
            if (n == 1)
            {
                arrayEl = readingFile();
                Tree    = Tree.IBTFill(arrayEl, arrayEl.Length);
                Tree.Paint1(Tree, arrayEl);
            }
            else
            {
                Tree = Tree.Filling();
                Tree.PaintForHand(Tree);
            }
            Console.WriteLine("Если вы хотите вывести процедуры нерекурсивных обходов нажмите 1");
            int m = int.Parse(Console.ReadLine());

            if (m == 1)
            {
                Console.WriteLine("Инфиксный обход");
                NRInfix(Tree);
                Console.WriteLine();
                Console.WriteLine("Постфиксный обход");
                NRPostfix(Tree);
                Console.WriteLine();
                Console.WriteLine("Префиксный обход");
                NRPrefix(Tree);
                Console.WriteLine();
                Console.WriteLine("Поуровневый обход");
                if (n == 1)
                {
                    Tree.levelFix(Tree, arrayEl);
                }
                else
                {
                    Tree.levelFixForHand(Tree);
                }
            }
            Console.ReadKey();
        }
示例#8
0
        private MyTree ForlevelFix(MyTree Tree, ref string[][] arrayEl)//не работает
        {
            if (Tree.leftKnot != null)
            {
                Lvl++;
                ForlevelFix(Tree.leftKnot, ref arrayEl);
            }
            if (Tree.rightKnot != null)
            {
                Lvl++;
                ForlevelFix(Tree.rightKnot, ref arrayEl);
            }

            Array.Resize(ref arrayEl[Lvl], arrayEl[Lvl].Length + 1);
            arrayEl[Lvl][arrayEl[Lvl].Length - 1] = Tree.value;
            Lvl--;
            return(null);
        }
示例#9
0
 public MyTree DisbalanseTree(MyTree tree)
 {
     if (tree.leftKnot != null)
     {
         if (tree.leftKnot.value == " ")
         {
             DisbalanseTree(tree.leftKnot);
         }
     }
     if (tree.rightKnot != null)
     {
         if (tree.rightKnot.value == " ")
         {
             DisbalanseTree(tree.rightKnot);
         }
     }
     tree = null;
     return(tree);
 }
示例#10
0
 public void levelFixForHand(MyTree Tree)
 {
     mas = new string[HowLevels + 1][];
     for (int i = 0; i < mas.Length; i++)
     {
         mas[i] = new string[0];
     }
     Lvl = 0;
     ForlevelFix(Tree, ref mas);
     for (int i = 0; i < mas.Length; i++)
     {
         for (int j = 0; j < mas[i].Length; j++)
         {
             if (mas[i][j] != " ")
             {
                 Console.Write(mas[i][j] + " ");
             }
         }
     }
 }
示例#11
0
 public void levelFix(MyTree Tree, string[] ArrayElements)
 {
     mas = new string[countLevels(ArrayElements)][];
     for (int i = 0; i < mas.Length; i++)
     {
         mas[i] = new string[0];
     }
     Lvl = 0;
     ForlevelFix(Tree, ref mas);
     for (int i = 0; i < mas.Length; i++)
     {
         for (int j = 0; j < mas[i].Length; j++)
         {
             if (mas[i][j] != " ")
             {
                 Console.Write(mas[i][j] + " ");
             }
         }
     }
 }
示例#12
0
 public MyTree MethodForBalance(MyTree tree)
 {
     if (tree.leftKnot != null)
     {
         ForBalance++;
         MethodForBalance(tree.leftKnot);
     }
     if (tree.rightKnot == null && tree.leftKnot == null && ForBalance < HowLevels)
     {
         tree.rightKnot       = new MyTree(tree);
         tree.rightKnot.value = " ";
         tree.leftKnot        = new MyTree(tree);
         tree.leftKnot.value  = " ";
         ForBalance++;
         MethodForBalance(tree.leftKnot);
         ForBalance++;
         MethodForBalance(tree.rightKnot);
     }
     if (tree.rightKnot == null && tree.leftKnot != null)
     {
         tree.rightKnot       = new MyTree();
         tree.rightKnot.value = " ";
         ForBalance++;
         MethodForBalance(tree.rightKnot);
     }
     if (tree.leftKnot == null && tree.rightKnot != null)
     {
         tree.leftKnot       = new MyTree();
         tree.leftKnot.value = " ";
         ForBalance++;
         MethodForBalance(tree.leftKnot);
     }
     if (tree.rightKnot != null)
     {
         ForBalance++;
         MethodForBalance(tree.rightKnot);
     }
     ForBalance--;
     return(tree);
 }
示例#13
0
        public MyTree IBTFill(string[] arrayEl, int Count)
        {
            level++;
            MyTree Tree       = new MyTree();
            int    countLeft  = Count / 2;
            int    countRight = Count - countLeft - 1;

            Tree.value = arrayEl[k];
            k++;
            if (countLeft > 0)
            {
                Tree.leftKnot        = IBTFill(arrayEl, countLeft);
                Tree.leftKnot.parent = Tree;
            }
            if (countRight > 0)
            {
                Tree.rightKnot        = IBTFill(arrayEl, countRight);
                Tree.rightKnot.parent = Tree;
            }
            level--;
            return(Tree);
        }
示例#14
0
        private MyTree ForFilling(MyTree Tree)
        {
point1:
            if (ppp > 1)
            {
                Console.WriteLine("Выберите заполенение левого(введите 1) или правого(введите 2) листа");
                Console.WriteLine("Чтобы вернуться на 1 лист назад нажимте ноль");
                int r = int.Parse(Console.ReadLine());
                if (r != 0)
                {
                    ppp--;
                    switch (r)
                    {
                    case 1:
                        Console.WriteLine("Введите значение левого узла");
                        Tree.leftKnot        = new MyTree();
                        Tree.leftKnot.parent = Tree;
                        Tree.leftKnot.value  = Console.ReadLine();
                        CountElements++;
                        Tree.leftKnot = ForFilling(Tree.leftKnot);
                        break;

                    case 2:
                        Console.WriteLine("Введите значение правого узла");
                        Tree.rightKnot        = new MyTree();
                        Tree.rightKnot.parent = Tree;
                        Tree.rightKnot.value  = Console.ReadLine();
                        CountElements++;
                        Tree.rightKnot = ForFilling(Tree.rightKnot);
                        break;
                    }
                }
                if (ppp > 1 && r != 0)
                {
                    goto point1;
                }
            }
            return(Tree);
        }
示例#15
0
 public MyTree(MyTree par)
 {
     parent = par;
 }