예제 #1
0
        private NodeBT FindElement(int value, out NodeBT 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);
        }
예제 #2
0
 private void AddTo(NodeBT node, int value)
 {
     if (node.Value > value)
     {
         if (node.Left == null)
         {
             node.Left = new NodeBT(value);
         }
         else
         {
             AddTo(node.Left, value);
         }
     }
     else if (node.Value < value)
     {
         if (node.Right == null)
         {
             node.Right = new NodeBT(value);
         }
         else
         {
             AddTo(node.Right, value);
         }
     }
 }
예제 #3
0
 public void Add(int value)
 {
     if (_head == null)
     {
         _head = new NodeBT(value);
     }
     else
     {
         AddTo(_head, value);
     }
     Size++;
 }
예제 #4
0
 public void Add(int value)
 {
     if (_head == null)
     {
         _head = new NodeBT(value);
     }
     else
     {
         AddTo(_head, value);
     }
     Size++;
 }
예제 #5
0
        private int Width(NodeBT node)
        {
            var maxWidth = 0;
            var height   = Height(node);

            for (var i = 1; i <= height; i++)
            {
                var width = GetWidth(node, i);
                if (width > maxWidth)
                {
                    maxWidth = width;
                }
            }
            return(maxWidth);
        }
예제 #6
0
        private int GetWidth(NodeBT 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);
        }
예제 #7
0
        private int Height(NodeBT 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);
        }
예제 #8
0
        public void Del(int value)
        {
            NodeBT parent;

            var current = FindElement(value, out parent);
            if (current == null)
            {
                return;
            }
            Size--;

            if (current.Right == null)
            {
                if (parent == null)
                {
                    _head = current.Left;
                }
                else
                {
                    if (parent.Value > current.Value)
                    {
                        parent.Left = current.Left;
                    }
                    else if (parent.Value < current.Value)
                    {
                        parent.Right = current.Left;
                    }
                }
            }
            else if (current.Right.Left == null)
            {
                current.Right.Left = current.Left;

                if (parent == null)
                {
                    _head = current.Right;
                }
                else
                {
                    if (parent.Value > current.Value)
                    {
                        parent.Left = current.Right;
                    }
                    else if (parent.Value < current.Value)
                    {
                        parent.Right = current.Right;
                    }
                }
            }
            else
            {
                var leftMost = current.Left;
                var leftMostParent = leftMost.Left;

                while (leftMost.Left != null)
                {
                    leftMostParent = leftMost;
                    leftMost = leftMost.Left;
                }

                leftMostParent.Left = leftMost.Right;
                leftMost.Left = current.Left;
                leftMost.Right = current.Right;

                if (parent == null)
                {
                    _head = leftMost;
                }
                else
                {
                    if (parent.Value > current.Value)
                    {
                        parent.Left = leftMost;
                    }
                    else if (parent.Value < current.Value)
                    {
                        parent.Right = leftMost;
                    }
                }
            }
        }
예제 #9
0
 private int Width(NodeBT node)
 {
     var maxWidth = 0;
     var height = Height(node);
     for (var i = 1; i <= height; i++)
     {
         var width = GetWidth(node, i);
         if (width > maxWidth)
         {
             maxWidth = width;
         }
     }
     return maxWidth;
 }
예제 #10
0
        private int Height(NodeBT 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;
        }
예제 #11
0
 public void Clear()
 {
     _head = null;
     Size = 0;
 }
예제 #12
0
        private int GetWidth(NodeBT 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;
        }
예제 #13
0
        private NodeBT FindElement(int value, out NodeBT 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;
        }
예제 #14
0
 private void AddTo(NodeBT node, int value)
 {
     if (node.Value > value)
     {
         if (node.Left == null)
         {
             node.Left = new NodeBT(value);
         }
         else
         {
             AddTo(node.Left, value);
         }
     }
     else if (node.Value < value)
     {
         if (node.Right == null)
         {
             node.Right = new NodeBT(value);
         }
         else
         {
             AddTo(node.Right, value);
         }
     }
 }
예제 #15
0
        public void Del(int value)
        {
            NodeBT parent;

            var current = FindElement(value, out parent);

            if (current == null)
            {
                return;
            }
            Size--;

            if (current.Right == null)
            {
                if (parent == null)
                {
                    _head = current.Left;
                }
                else
                {
                    if (parent.Value > current.Value)
                    {
                        parent.Left = current.Left;
                    }
                    else if (parent.Value < current.Value)
                    {
                        parent.Right = current.Left;
                    }
                }
            }
            else if (current.Right.Left == null)
            {
                current.Right.Left = current.Left;

                if (parent == null)
                {
                    _head = current.Right;
                }
                else
                {
                    if (parent.Value > current.Value)
                    {
                        parent.Left = current.Right;
                    }
                    else if (parent.Value < current.Value)
                    {
                        parent.Right = current.Right;
                    }
                }
            }
            else
            {
                var leftMost       = current.Left;
                var leftMostParent = leftMost.Left;

                while (leftMost.Left != null)
                {
                    leftMostParent = leftMost;
                    leftMost       = leftMost.Left;
                }

                leftMostParent.Left = leftMost.Right;
                leftMost.Left       = current.Left;
                leftMost.Right      = current.Right;

                if (parent == null)
                {
                    _head = leftMost;
                }
                else
                {
                    if (parent.Value > current.Value)
                    {
                        parent.Left = leftMost;
                    }
                    else if (parent.Value < current.Value)
                    {
                        parent.Right = leftMost;
                    }
                }
            }
        }
예제 #16
0
 public void Clear()
 {
     _head = null;
     Size  = 0;
 }