Exemple #1
0
        static void OverHeight(TreePoint point) //Пересчёт высот деревьев
        {
            int lHeight = point.Left != null ? point.Left.Height : 0,
                pHeight = point.Right != null ? point.Right.Height : 0;

            point.Height = (lHeight > pHeight ? pHeight : lHeight) + 1;
        }
Exemple #2
0
        static TreePoint DeletePoint(TreePoint current, TreePoint point)
        {
            if (current == null)
            {
                return(null);
            }
            if (point < current)
            {
                current.Left = DeletePoint(current.Left, point);
            }
            else if (point > current)
            {
                current.Right = DeletePoint(current.Right, point);
            }
            else
            {
                TreePoint left  = current.Left,
                          right = current.Right;

                if (left == null)
                {
                    return(left);
                }

                TreePoint min = right.Min;
                min.Right = DeleteMinPoint(right);

                min.Left = left;

                return(BalanceTree(min));
            }
            return(BalanceTree(current));
        }
Exemple #3
0
        static int BF(TreePoint point)
        {
            int lHeight = point.Left != null ? point.Left.Height : 0,
                pHeight = point.Right != null ? point.Right.Height : 0;

            return(pHeight - lHeight);
        }
Exemple #4
0
 static void PrintTree(TreePoint point, int shift = 0)
 {
     if (point != null)
     {
         PrintTree(point.Left, shift + 3);
         Output.Text(GenerateShiftedText(point, shift));
         PrintTree(point.Right, shift + 3);
     }
 }
Exemple #5
0
 static TreePoint DeleteMinPoint(TreePoint current)
 {
     if (current.Left == null)
     {
         return(current.Right);
     }
     current.Left = DeleteMinPoint(current.Left);
     return(BalanceTree(current));
 }
Exemple #6
0
        public static void Run()
        {
            TreePoint tree = null;

            for (int i = 1; i <= 7; i++)
            {
                tree = InsertPoint(tree, MakeTreePoint());
            }

            PrintTree(tree);
        }
Exemple #7
0
        static TreePoint LeftRotation(TreePoint point) //Малый левый поворот
        {
            TreePoint temp = point.Right;

            point.Right = temp.Left;
            temp.Left   = point;

            OverHeight(point);
            OverHeight(temp);

            return(temp);
        }
Exemple #8
0
        static string GenerateShiftedText(TreePoint point, int shift)
        {
            string[] tempStr = point.ToString().Split(new char[] { '\n' });

            string result = "";

            for (int i = 0; i < tempStr.Length; i++)
            {
                result += $"{new string(' ', shift)}{tempStr[i]}\n";
            }

            return(result);
        }
Exemple #9
0
        static TreePoint InsertPoint(TreePoint current, TreePoint point)
        {
            if (current == null)
            {
                return(point);
            }
            if (point < current)
            {
                current.Left = InsertPoint(current.Left, point);
            }
            else
            {
                current.Right = InsertPoint(current.Right, point);
            }

            return(BalanceTree(current));
        }
Exemple #10
0
 static TreePoint BalanceTree(TreePoint tree)
 {
     OverHeight(tree);
     if (BF(tree) == 2)
     {
         if (BF(tree.Right) < 0)
         {
             tree.Right = RightRotation(tree.Right);
         }
         return(LeftRotation(tree));
     }
     if (BF(tree) == -2)
     {
         if (BF(tree.Left) > 0)
         {
             tree.Left = LeftRotation(tree.Left);
         }
         return(RightRotation(tree));
     }
     return(tree);
 }