0

Суть проблемы такая - есть класс на C#, метод которого нужно отладить. Я в MonoDevelop 7.5(build 1255) ставлю точку останова. Когда я запускаю отладку, программа выполняется и не тормозится там где нужно. Как быть в этом случае?

P.S. точка останова вызывается на методе LeftRotate

ОС: Linux Mint 18.3 64bit

IDE: MonoDevelop 7.5

using System;

namespace RBTrees
{
    class MainClass
    {
        public enum BinSide
        {
            Left,
            Right
        }
        public enum Color
        {
            Black,
            Red
        }
        /// <summary>
        /// Бинарное дерево поиска
        /// </summary>
        public class BinaryTree
        {

            public long? Data { get; private set; }
            public BinaryTree Left { get; set; }
            public BinaryTree Right { get; set; }
            public BinaryTree Parent { get; set; }
            public Color T_Color { get; set; }
            public BinaryTree Root { get; set; } // указатель на корень дерева. Нужен для того, чтобы при поворотах не потерять корень дерева.

            /// <summary>
            /// Вставляет целочисленное значение в дерево
            /// </summary>
            /// <param name="data">Значение, которое добавится в дерево</param>
            public void Insert(long data)
            {
                if (Data == null || Data == data)
                {
                    Data = data;
                    T_Color = Color.Black;
                    return;
                }
                if (Data > data)
                {
                    if (Left == null) Left = new BinaryTree();
                    Insert(data, Left, this);
                    /*if (this.T_Color == Color.Black) Left.T_Color = Color.Red;
                    if (this.T_Color == Color.Red) Left.T_Color = Color.Black;*/

                }
                else
                {
                    if (Right == null) Right = new BinaryTree();
                    Insert(data, Right, this);
                    /*if (this.T_Color == Color.Black) Left.T_Color = Color.Red;
                    if (this.T_Color == Color.Red) Left.T_Color = Color.Black;*/
                }
            }

            /// <summary>
            /// Вставляет значение в определённый узел дерева
            /// </summary>
            /// <param name="data">Значение</param>
            /// <param name="node">Целевой узел для вставки</param>
            /// <param name="parent">Родительский узел</param>
            private void Insert(long data, BinaryTree node, BinaryTree parent)
            {

                if (node.Data == null || node.Data == data)
                {
                    node.Data = data;
                    node.Parent = parent;
                    if (parent.T_Color == Color.Black) node.T_Color = Color.Red;
                    if (parent.T_Color == Color.Red) node.T_Color = Color.Black;
                    return;
                }
                if (node.Data > data)
                {
                    if (node.Left == null) node.Left = new BinaryTree();
                    Insert(data, node.Left, node);
                    if (parent.T_Color == Color.Black) Left.T_Color = Color.Red;
                    if (parent.T_Color == Color.Red) Left.T_Color = Color.Black;

                }
                else
                {
                    if (node.Right == null) node.Right = new BinaryTree();
                    Insert(data, node.Right, node);
                    if (parent.T_Color == Color.Black) Right.T_Color = Color.Red;
                    if (parent.T_Color == Color.Red) Right.T_Color = Color.Black;
                }
            }
            /// <summary>
            /// Уставляет узел в определённый узел дерева
            /// </summary>
            /// <param name="data">Вставляемый узел</param>
            /// <param name="node">Целевой узел</param>
            /// <param name="parent">Родительский узел</param>
            private void Insert(BinaryTree data, BinaryTree node, BinaryTree parent)
            {

                if (node.Data == null || node.Data == data.Data)
                {
                    node.Data = data.Data;
                    node.Left = data.Left;
                    node.Right = data.Right;
                    node.Parent = parent;
                    return;
                }
                if (node.Data > data.Data)
                {
                    if (node.Left == null) node.Left = new BinaryTree();
                    Insert(data, node.Left, node);
                }
                else
                {
                    if (node.Right == null) node.Right = new BinaryTree();
                    Insert(data, node.Right, node);
                }
            }
            /// <summary>
            /// Определяет, в какой ветви для родительского лежит данный узел
            /// </summary>
            /// <param name="node"></param>
            /// <returns></returns>
            private BinSide? MeForParent(BinaryTree node)
            {
                if (node.Parent == null) return null;
                if (node.Parent.Left == node) return BinSide.Left;
                if (node.Parent.Right == node) return BinSide.Right;
                return null;
            }

            /// <summary>
            /// Удаляет узел из дерева
            /// </summary>
            /// <param name="node">Удаляемый узел</param>
            public void Remove(BinaryTree node)
            {
                if (node == null) return;
                var me = MeForParent(node);
                //Если у узла нет дочерних элементов, его можно смело удалять
                if (node.Left == null && node.Right == null)
                {
                    if (me == BinSide.Left)
                    {
                        node.Parent.Left = null;
                    }
                    else
                    {
                        node.Parent.Right = null;
                    }
                    return;
                }
                //Если нет левого дочернего, то правый дочерний становится на место удаляемого
                if (node.Left == null)
                {
                    if (me == BinSide.Left)
                    {
                        node.Parent.Left = node.Right;
                    }
                    else
                    {
                        node.Parent.Right = node.Right;
                    }

                    node.Right.Parent = node.Parent;
                    return;
                }
                //Если нет правого дочернего, то левый дочерний становится на место удаляемого
                if (node.Right == null)
                {
                    if (me == BinSide.Left)
                    {
                        node.Parent.Left = node.Left;
                    }
                    else
                    {
                        node.Parent.Right = node.Left;
                    }

                    node.Left.Parent = node.Parent;
                    return;
                }

                //Если присутствуют оба дочерних узла
                //то правый ставим на место удаляемого
                //а левый вставляем в правый

                if (me == BinSide.Left)
                {
                    node.Parent.Left = node.Right;
                }
                if (me == BinSide.Right)
                {
                    node.Parent.Right = node.Right;
                }
                if (me == null)
                {
                    var bufLeft = node.Left;
                    var bufRightLeft = node.Right.Left;
                    var bufRightRight = node.Right.Right;
                    node.Data = node.Right.Data;
                    node.Right = bufRightRight;
                    node.Left = bufRightLeft;
                    Insert(bufLeft, node, node);
                }
                else
                {
                    node.Right.Parent = node.Parent;
                    Insert(node.Left, node.Right, node.Right);
                }
            }
            /// <summary>
            /// Удаляет значение из дерева
            /// </summary>
            /// <param name="data">Удаляемое значение</param>
            public void Remove(long data)
            {
                var removeNode = Find(data);
                if (removeNode != null)
                {
                    Remove(removeNode);
                }
            }
            /// <summary>
            /// Ищет узел с заданным значением
            /// </summary>
            /// <param name="data">Значение для поиска</param>
            /// <returns></returns>
            public BinaryTree Find(long data)
            {
                if (Data == data) return this;
                if (Data > data)
                {
                    return Find(data, Left);
                }
                return Find(data, Right);
            }
            /// <summary>
            /// Ищет значение в определённом узле
            /// </summary>
            /// <param name="data">Значение для поиска</param>
            /// <param name="node">Узел для поиска</param>
            /// <returns></returns>
            public BinaryTree Find(long data, BinaryTree node)
            {
                if (node == null) return null;

                if (node.Data == data) return node;
                if (node.Data > data)
                {
                    return Find(data, node.Left);
                }
                return Find(data, node.Right);
            }

            /// <summary>
            /// Количество элементов в дереве
            /// </summary>
            /// <returns></returns>
            public long CountElements()
            {
                return CountElements(this);
            }
            /// <summary>
            /// Количество элементов в определённом узле
            /// </summary>
            /// <param name="node">Узел для подсчета</param>
            /// <returns></returns>
            private long CountElements(BinaryTree node)
            {
                long count = 1;
                if (node.Right != null)
                {
                    count += CountElements(node.Right);
                }
                if (node.Left != null)
                {
                    count += CountElements(node.Left);
                }
                return count;
            }
            public void RotateLeft(BinaryTree node, BinaryTree parent)
            {
                BinaryTree _a = new BinaryTree();
                BinaryTree _b_dLeft = new BinaryTree();
                BinaryTree _b = new BinaryTree();
                _b = node.Right;
                _b_dLeft = node.Right.Left;
                _a = node;
                node = _b;
                node.Left = _a;
                node.Left.Right = _b_dLeft;
                parent.Left = node;
            }
            private void RotateRight(BinaryTree node)
            {

            }
        }

        public class BinaryTreeExtensions
        {
            public static void Print(BinaryTree node)
            {
                if (node != null)
                {
                    if (node.Parent == null)
                    {
                        Console.Write("ROOT:{0}", node.Data);
                        if (node.T_Color == Color.Red) Console.WriteLine(" Red");
                        else Console.WriteLine(" Black");
                    }
                    else
                    {
                        if (node.Parent.Left == node)
                        {
                            Console.Write("Left for {1}  --> {0}", node.Data, node.Parent.Data);
                            if (node.T_Color == Color.Red) Console.WriteLine(" Red");
                            else Console.WriteLine(" Black");
                        }

                        if (node.Parent.Right == node)
                        {
                            Console.Write("Right for {1} --> {0}", node.Data, node.Parent.Data);
                            if (node.T_Color == Color.Red) Console.WriteLine(" Red");
                            else Console.WriteLine(" Black");
                        }
                    }
                    if (node.Left != null)
                    {
                        Print(node.Left);
                    }
                    if (node.Right != null)
                    {
                        Print(node.Right);
                    }
                }
            }
        }
        public static void Main(string[] args)
        {
            var tree = new BinaryTree();
            /*tree.Insert(20);
            tree.Insert(40);
            tree.Insert(10);
            tree.Insert(30);
            tree.Insert(80);
            tree.Insert(29);
            tree.Insert(31);
            tree.Insert(32);
            tree.Insert(70);*/
            tree.Insert(10);
            tree.Insert(20);
            tree.Insert(30);
            tree.RotateLeft(tree.Right, tree);
            tree.Insert(40);
            tree.Insert(50);
            tree.Insert(60);
            tree.Insert(70);

            BinaryTreeExtensions.Print(tree);
            /*tree.Remove(40);
            BinaryTreeExtensions.Print(tree);
            tree.Remove(20);
            BinaryTreeExtensions.Print(tree);*/
        }
    }
}
4
  • вы указали все, кроме главного - кода
    – tym32167
    23 окт 2018 в 16:45
  • Может просто программа не доходит до точки останова? 23 окт 2018 в 17:17
  • Нет. Она доходит до неё. Маркер тому тот факт, что 30 пропадает из дерева - элемент теряется в ходе, видимо, неверной работы метода. 23 окт 2018 в 17:25
  • Более того, установка точки останова на другое место так же не принесла плодов. 23 окт 2018 в 17:26

1 ответ 1

0

Пока проблема решилась установкой маркера в зоне Main. Странное поведение... Или я просто привык что Visual Studio тормозится на точках поставленных на конкретных методах...

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.