Пример #1
0
        public static int Height(TreeMember knot)
        {
            if (knot == null)
            {
                return(0);
            }
            var leftH  = Height(knot.Left);
            var rightH = Height(knot.Right);

            return(Math.Max(leftH, rightH) + 1);
        }
Пример #2
0
        private int minHeight(TreeMember knot)
        {
            if (knot == null)
            {
                return(0);
            }
            var leftH  = minHeight(knot.Left);
            var rightH = minHeight(knot.Right);

            return(Math.Min(leftH, rightH) + 1);
        }
Пример #3
0
        private void AddElementRecursively(ref TreeMember current, int value)
        {
            if (current == null)
            {
                current = new TreeMember {
                    Value = value
                };
                return;
            }

            var leftH  = minHeight(current.Left);
            var rightH = minHeight(current.Right);

            if (leftH <= rightH)
            {
                AddElementRecursively(ref current.Left, value);
            }
            else
            {
                AddElementRecursively(ref current.Right, value);
            }
        }