Пример #1
0
        /// <summary>
        /// returns a 2D char array of the binary tree
        /// </summary>
        /// <param name="head">root to print from</param>
        /// <param name="treeHeight">depth to print tree</param>
        /// <param name="nodePrintLength">max space allocated for printing node's data</param>
        /// <returns></returns>
        public static char[,] GetCharArray(IPrintableBinaryNode head, int treeHeight, int nodePrintLength)
        {
            //if print length is odd number everything can be centered properly (easier math)
            if (nodePrintLength % 2 == 0)
            {
                nodePrintLength++;
            }

            int printHeight = treeHeight + 1;             // plus 1 for copyright
            int printWidth  = GetWidth(treeHeight, nodePrintLength);

            //increase width if needed to fix copyright
            const string copyRight = "Tree display provided by Anthony Corbin";

            printWidth = Math.Max(printWidth, copyRight.Length);             // must be at least the length of the copyright

            //allocating required space
            char[,] screen = new char[printHeight, printWidth];
            for (int i = 0; i < screen.GetLength(0); i++)
            {
                for (int j = 0; j < screen.GetLength(1); j++)
                {
                    screen[i, j] = SpacePlaceholder;
                }
            }

            //print tree starting in the middle of the screen
            PrintToArray(screen, head, treeHeight, new Coord(printWidth / 2.0, 0), nodePrintLength);

            //print copyright
            InsertStringToCharArray(screen, new Coord(0, printHeight - 1), copyRight);
            return(ReplacePlaceHolder(Trim(screen)));
        }
Пример #2
0
 //determines the height of a binary tree
 private static int GetHeight(IPrintableBinaryNode theGuy)
 {
     if (theGuy == null)
     {
         return(0);
     }
     return(1 + Math.Max(GetHeight(theGuy.GetLeft()), GetHeight(theGuy.GetRight())));
 }
Пример #3
0
        /// <summary>
        /// outputs GetString to console
        /// </summary>
        /// <param name="head">root node to print from</param>
        /// <param name="treeHeight">depth to print tree</param>
        /// <param name="nodePrintLength">max space allocated for node data</param>
        public static void Print(IPrintableBinaryNode head, int treeHeight, int nodePrintLength)
        {
            // get printout
            char[,] screen = GetCharArray(head, treeHeight, nodePrintLength);

            //print
            char[] tmep = new char[1];
            for (int i = 0; i < screen.GetLength(0); i++)
            {
                for (int j = 0; j < screen.GetLength(1); j++)
                {
                    char toPrint = screen[i, j] == SpacePlaceholder ? ' ' : screen[i, j];                   // replace uninitialized with ' '
                    Console.Write(toPrint);
                }
                Console.Write("\n");
            }
        }
Пример #4
0
        /// <summary>
        /// returns a string of the given binary tree
        /// </summary>
        /// <param name="head">root node to print from</param>
        /// <param name="treeHeight">depth to print tree</param>
        /// <param name="nodePrintLength">max space allocated for node data</param>
        /// <returns></returns>
        public static string GetString(IPrintableBinaryNode head, int treeHeight, int nodePrintLength)
        {
            // get printout
            char[,] screen = GetCharArray(head, treeHeight, nodePrintLength);

            //convert into string
            StringBuilder ret = new StringBuilder();

            for (int i = 0; i < screen.GetLength(0); i++)
            {
                for (int j = 0; j < screen.GetLength(1); j++)
                {
                    ret.Append(screen[i, j]);
                }
                ret.Append("\n");
            }
            return(ret.ToString());
        }
Пример #5
0
        //prints tree to char[][] starting at pos
        private static void PrintToArray(char[,] screen, IPrintableBinaryNode node, int height, Coord pos, int dataLength)
        {
            if (node == null)
            {
                return;
            }

            //print node data
            string data = node.GetString();

            InsertStringToCharArrayCentered(screen, pos, data);
            //should traverse
            if (height == 1)
            {
                return;
            }

            //trust the math
            int lastWidth   = GetWidth(height - 1, dataLength);
            int numOfDashes = (lastWidth - 1) / 2 - ((dataLength - 1) / 2 - 1);

            int offset = (dataLength - 1) / 2;

            //printing the pretty
            for (int i = 1; i <= numOfDashes; i++)
            {
                char printL = i == numOfDashes ? LeftDownPlaceholder : SideWaysPlaceholder;                 // character to be printed
                char printR = i == numOfDashes ? RightDownPlaceholder : SideWaysPlaceholder;                // character to be printed
                offset += 1;
                if (node.GetLeft() != null)
                {
                    InsertStringToCharArray(screen, pos.Add(-offset, 0), printL);                                         // PRINT!!
                }
                if (node.GetRight() != null)
                {
                    InsertStringToCharArray(screen, pos.Add(offset, 0), printR);                                          // PRINT!!
                }
            }
            //recourse for each branch with position at the end of each branch +1 in the y
            PrintToArray(screen, node.GetLeft(), height - 1, pos.Add(-offset, 1), dataLength);
            PrintToArray(screen, node.GetRight(), height - 1, pos.Add(offset, 1), dataLength);
        }
Пример #6
0
 /// <summary>
 /// returns a 2D char array of the binary tree
 /// </summary>
 /// <param name="head">root to print from</param>
 /// <param name="nodePrintLength">max space allocated for printing node's data</param>
 /// <returns></returns>
 public static char[,] GetCharArray(IPrintableBinaryNode head, int nodePrintLength)
 {
     return(GetCharArray(head, GetHeight(head), nodePrintLength));
 }
Пример #7
0
 /// <summary>
 /// outputs GetString to console
 /// </summary>
 /// <param name="head">root node to print from</param>
 /// <param name="nodePrintLength">max space allocated for node data</param>
 /// <returns></returns>
 public static void Print(IPrintableBinaryNode head, int nodePrintLength)
 {
     Print(head, GetHeight(head), nodePrintLength);
 }
Пример #8
0
 /// <summary>
 /// returns a string of the given binary tree
 /// </summary>
 /// <param name="head">root node to print from</param>
 /// <param name="nodePrintLength">max space allocated for node data</param>
 /// <returns></returns>
 public static string GetString(IPrintableBinaryNode head, int nodePrintLength)
 {
     return(GetString(head, GetHeight(head), nodePrintLength));
 }