Example #1
0
        static void PrintVertical(Tree tree, int height, List <List <TreeNode> > levels)
        {
            var length    = tree.Max().ToString().Length;
            var maxHeight = height;
            var maxTop    = 0;
            var basicTop  = Console.CursorTop + 1;

            var rightHeight = (int)Math.Pow(2, maxHeight - tree.RightHeight() - 1) - 1;

            foreach (var level in levels)
            {
                var count = 0;
                var shift = (int)Math.Pow(2, height - 1) - 1;

                bool isLeft = false;

                foreach (var item in level.Reverse <TreeNode>())
                {
                    if (item != null)
                    {
                        var top = shift + count * 2 * (shift + 1) - rightHeight + basicTop;
                        maxTop = (top > maxTop) ? top : maxTop;
                        var left = (maxHeight - height) * (length + 1) + 1;
                        Console.SetCursorPosition(left, top);

                        Console.Write(item.Key);
                        if (isLeft && top >= length && left > 1)
                        {
                            Console.SetCursorPosition(left - 1, top);
                            Console.Write('\\');
                            for (int i = 0; i < shift; i++)
                            {
                                top--;
                                Console.SetCursorPosition(left - 2, top);
                                Console.Write('|');
                            }
                        }
                        else if (left > 1)
                        {
                            Console.SetCursorPosition(left - 1, top);
                            Console.Write('/');
                            for (int i = 0; i < shift; i++)
                            {
                                top++;
                                Console.SetCursorPosition(left - 2, top);
                                Console.Write('|');
                            }
                        }
                    }
                    count++;
                    isLeft = !isLeft;
                }
                Console.SetCursorPosition(0, 0);
                height--;
            }
            Console.SetCursorPosition(0, maxTop + 2);
        }
Example #2
0
        static void PrintHorisontal(Tree tree, int height, List <List <TreeNode> > levels)
        {
            var basicTop  = Console.CursorTop + 2;
            var length    = tree.Max().ToString().Length;
            var maxHeight = height;

            foreach (var level in levels)
            {
                var count = 0;
                var shift = length * ((int)Math.Pow(2, height) - 1);

                bool isLeft = true;

                foreach (var item in level)
                {
                    if (item != null)
                    {
                        var left = shift + count * 2 * (shift + length);
                        var top  = 2 * (maxHeight - height) + basicTop;
                        Console.SetCursorPosition(left, top);

                        Console.Write(item.Key);
                        if (isLeft && top > basicTop)
                        {
                            Console.SetCursorPosition(left + length, top - 1);
                            Console.Write('/');
                            for (int i = 0; i < shift; i++)
                            {
                                Console.Write('-');
                            }
                        }
                        else if (top > basicTop)
                        {
                            Console.SetCursorPosition(left - shift - 1, top - 1);
                            for (int i = 0; i < shift; i++)
                            {
                                Console.Write('-');
                            }
                            Console.Write('\\');
                        }
                    }
                    count++;
                    isLeft = !isLeft;
                }
                height--;
            }
            Console.SetCursorPosition(0, maxHeight * 2 + basicTop);
        }