Ejemplo n.º 1
0
        public Main()
        {
            InitializeComponent();

            _x = 200;
            _y = 10;
            var tree = new BinaryTree();
            int[] array = { 5, 3, 7, 4, 6, 2, 8, 1 };
            tree.Init(array);
            _current = tree.Head;
        }
Ejemplo n.º 2
0
 public void Add(int value)
 {
     if (Head == null)
     {
         Head = new Node(value);
     }
     else
     {
         AddTo(Head, value);
     }
     Size++;
 }
Ejemplo n.º 3
0
        private void DrawTree(Node node)
        {
            var pen = new Pen(Color.Black);
            var graphics = CreateGraphics();

            if (node.Left != null)
            {
                _lineX = _x;
                _lineY = _y;

                _x =(node.Value <= 5)? _x / 2: _x / 2 + 30;
                _y += 50;
                DrawTree(node.Left);
                _x = (node.Value <= 5) ? _x*2 : (_x - 30)*2;
                _y -= 50;
            }

            if (node.Value == 5)
            {
                graphics.DrawLine(pen, 300, 60, _x, _y);
            }

            graphics.DrawLine(pen, _lineX, _lineY, _x, _y);
            graphics.DrawEllipse(pen, _x, _y, 15, 15);
            graphics.DrawString(node.Value.ToString(), new Font("Times New Roman", 12, FontStyle.Regular),
                SystemBrushes.ActiveCaptionText, _x, _y);

            if (node.Right != null)
            {
                _lineX = _x;
                _lineY = _y;

                _x += _x/2;
                _y += 50;
                DrawTree(node.Right);
                _x = _x / 3 * 2;
                _y -= 50;
            }
        }
Ejemplo n.º 4
0
        private int GetWidth(Node node, int level)
        {
            if (node == null)
            {
                return 0;
            }

            if (level == 1)
            {
                return 1;
            }
            else if (level > 1)
            {
                return GetWidth(node.Left, level - 1) + GetWidth(node.Right, level - 1);
            }

            var count = GetWidth(node.Right, level - 1);
            return count;
        }
Ejemplo n.º 5
0
        private Node FindElement(int value, out Node parent)
        {
            var current = Head;
            parent = null;

            while (current != null)
            {
                if (current.Value > value)
                {
                    parent = current;
                    current = current.Left;
                }
                else if (current.Value < value)
                {
                    parent = current;
                    current = current.Right;
                }
                else
                {
                    break;
                }
            }
            return current;
        }
Ejemplo n.º 6
0
 private void AddTo(Node node, int value)
 {
     if (node.Value > value)
     {
         if (node.Left == null)
         {
             node.Left = new Node(value);
         }
         else
         {
             AddTo(node.Left, value);
         }
     }
     else if (node.Value < value)
     {
         if (node.Right == null)
         {
             node.Right = new Node(value);
         }
         else
         {
             AddTo(node.Right, value);
         }
     }
 }
Ejemplo n.º 7
0
 public int Width(Node node)
 {
     var maxWidth = 0;
     var height = Height(node);
     for (var i = 0; i < height; i++)
     {
         var width = GetWidth(node, i);
         if (width > maxWidth)
         {
             maxWidth = width;
         }
     }
     return maxWidth;
 }
Ejemplo n.º 8
0
        public Node Loop(Node node, int width)
        {
            var stack = new Stack<Node>();
            var current = Head;
            var goLeftNext = true;
            stack.Push(current);
            while (stack.Count > 0)
            {
                if (goLeftNext)
                {
                    while (current.Left != null)
                    {
                        stack.Push(current);
                        current = current.Left;
                    }
                }

                if (current.Right != null)
                {
                    current = current.Right;
                    goLeftNext = true;
                }
                else
                {
                    current = stack.Pop();
                    goLeftNext = false;
                }
            }
            return current;
        }
Ejemplo n.º 9
0
        public int Height(Node node)
        {
            var count = 0;
            var leftHeight = 0;
            var rightHeight = 0;
            if (node == null)
            {
                return 0;
            }

            if (node.Left != null)
            {
                leftHeight = Height(node.Left);
            }

            if (node.Right != null)
            {
                rightHeight = Height(node.Right);
            }

            count = Math.Max(leftHeight, rightHeight) + 1;
            return count;
        }