コード例 #1
0
        private NodeBTLinked FindElement(int value, out NodeBTLinked parent)
        {
            var current = _head.Value;

            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(NodeBTLinked node, int value)
 {
     if (node.Value > value)
     {
         if (node.Left == null)
         {
             node.Left = new NodeBTLinked(value);
         }
         else
         {
             AddTo(node.Left, value);
         }
     }
     else if (node.Value < value)
     {
         if (node.Right == null)
         {
             node.Right = new NodeBTLinked(value);
         }
         else
         {
             AddTo(node.Right, value);
         }
     }
 }
コード例 #3
0
        private int Width(NodeBTLinked 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);
        }
コード例 #4
0
        private int GetWidth(NodeBTLinked 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);
        }
コード例 #5
0
        private int Height(NodeBTLinked 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.Right);
            }

            count = Math.Max(leftHeight, rightHeight) + 1;
            return(count);
        }
コード例 #6
0
 private int Width(NodeBTLinked 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;
 }
コード例 #7
0
        private int Height(NodeBTLinked 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.Right);
            }

            count = Math.Max(leftHeight, rightHeight) + 1;
            return count;
        }
コード例 #8
0
        private int GetWidth(NodeBTLinked 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;
        }
コード例 #9
0
        private NodeBTLinked FindElement(int value, out NodeBTLinked parent)
        {
            var current = _head.Value;
            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;
        }
コード例 #10
0
 private void AddTo(NodeBTLinked node, int value)
 {
     if (node.Value > value)
     {
         if (node.Left == null)
         {
             node.Left = new NodeBTLinked(value);
         }
         else
         {
             AddTo(node.Left, value);
         }
     }
     else if (node.Value < value)
     {
         if (node.Right == null)
         {
             node.Right = new NodeBTLinked(value);
         }
         else
         {
             AddTo(node.Right, value);
         }
     }
 }