Example #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;
        }
Example #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));
        }
Example #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);
        }
Example #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);
     }
 }
Example #5
0
 static TreePoint DeleteMinPoint(TreePoint current)
 {
     if (current.Left == null)
     {
         return(current.Right);
     }
     current.Left = DeleteMinPoint(current.Left);
     return(BalanceTree(current));
 }
Example #6
0
        public static void Run()
        {
            TreePoint tree = null;

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

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

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

            OverHeight(point);
            OverHeight(temp);

            return(temp);
        }
Example #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);
        }
Example #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));
        }
Example #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);
 }