直观地打印二叉树

来源:互联网 发布:手机流量开关软件 编辑:程序博客网 时间:2024/06/06 08:41

在写二叉树时经常希望把它可视化,本文实现了相对直观地打印二叉树。

主要思路:

  1. 空节点使用 n (null)表示
  2. 通过节点的层数计算间隔

效果图如下:

这里写图片描述

代码实现:

using System;using System.Collections.Generic;namespace PrintBT {    public class BinaryTree {        public class Node {            public int data;            public Node left;            public Node right;        }        static bool IsAllElementsNull(Queue<Node> queue) {            foreach (Node node in queue) {                if (null != node)                    return false;            }            return true;        }        public static int GetDepth(Node root) {            if (root == null)                return 0;            int depth1 = GetDepth(root.right);            int depth2 = GetDepth(root.left);            if (depth1 > depth2)                return depth1 + 1;            else                return depth2 + 1;        }        public static void PrintSpaces(int num) {            for (int i = 0; i < num; ++i)                Console.Write(" ");        }        public static void PrintNumPre(int curLevel, int maxLevel) {            int pow = maxLevel - curLevel - 1;            if (pow >= 0) {                int num = 3 * (int)Math.Pow(2, pow);                PrintSpaces(num);            }            else                PrintSpaces(1);        }        public static void PrintSymbolPre(int curLevel, int maxLevel) {            int pow = maxLevel - curLevel - 2;            if (pow >= 0) {                int num = 3 * (int)Math.Pow(2, pow);                PrintSpaces(num + 1);            }            else                PrintSpaces(2);        }        public static void PrintNumLR(int curLevel, int maxLevel) {            int pow = maxLevel - curLevel;            int num = 3;            if (pow > 0) {                num = 3 * (int)Math.Pow(2, pow) - 1;            }            PrintSpaces(num);        }        public static void PrintNumRL(int curLevel, int maxLevel) {            int pow = maxLevel - curLevel;            int num = 1;            if (pow > 0) {                num = 3 * (int)Math.Pow(2, pow) - 1;            }            PrintSpaces(num);        }        public static void PrintSymbolLR(int curLevel, int maxLevel) {            int pow = maxLevel - curLevel - 1;            int num = 3;            if (pow > 0) {                num = 3 * (int)Math.Pow(2, pow) - 1;            }            num = num - 2;            PrintSpaces(num);        }        public static void PrintSymbolRL(int curLevel, int maxLevel) {            int pow = maxLevel - curLevel - 1;            int num = 1;            if (pow > 0) {                num = 3 * (int)Math.Pow(2, pow) - 1;            }            num = num + 2;            PrintSpaces(num);        }        public static void PrintLeft() {            Console.Write("/");        }        public static void PrintRight() {            Console.Write("\\");        }        public static void PrintQueue(Queue<Node> queueR, Queue<Node> queueW, int level, int maxLevel) {            if (queueR.Count == 0 || IsAllElementsNull(queueR))                return;            level++;            Node curNode;            int index = 0;            Console.ForegroundColor = ConsoleColor.Yellow;            PrintNumPre(level, maxLevel);            Queue<Node> queueT = new Queue<Node>();            while (queueR.Count > 0) {                curNode = queueR.Dequeue();                queueT.Enqueue(curNode);                ++index;                if (null != curNode) {                    queueW.Enqueue(curNode.left);                    queueW.Enqueue(curNode.right);                }                else {                    queueW.Enqueue(null);                    queueW.Enqueue(null);                }                if (null != curNode) {                    Console.Write(curNode.data);                }                else {                    ConsoleColor curColor = Console.ForegroundColor;                    Console.ForegroundColor = ConsoleColor.Gray;                    Console.Write("n");                    Console.ForegroundColor = curColor;                }                if (index % 2 == 0) {                    Console.ForegroundColor = ConsoleColor.Yellow;                    PrintNumRL(level, maxLevel);                }                else {                    Console.ForegroundColor = ConsoleColor.Green;                    PrintNumLR(level, maxLevel);                }            }            if (!IsAllElementsNull(queueW)) {                Console.Write("\n");                PrintSymbolPre(level, maxLevel);                while (queueT.Count > 0) {                    curNode = queueT.Dequeue();                    PrintLeft();                    PrintSymbolLR(level, maxLevel);                    PrintRight();                    PrintSymbolRL(level, maxLevel);                }            }            Console.Write("\n");            PrintQueue(queueW, queueR, level, maxLevel);        }        public static void PrintBinaryTree(Node root) {            if (null == root)                return;            Queue<Node> queue1 = new Queue<Node>();            Queue<Node> queue2 = new Queue<Node>();            int maxLevel = GetDepth(root);            queue1.Enqueue(root);            PrintQueue(queue1, queue2, 0, maxLevel);        }        static void Main(string[] args) {            Node root1 = new Node();            root1.data = 1;            root1.left = new Node();            root1.left.data = 2;            root1.right = new Node();            root1.right.data = 3;            root1.left.left = new Node();            root1.left.left.data = 4;            root1.left.right = new Node();            root1.left.right.data = 5;            root1.right.left = new Node();            root1.right.left.data = 6;            root1.right.right = new Node();            root1.right.right.data = 7;            root1.left.left.left = new Node();            root1.left.left.left.data = 8;            root1.right.right.right = new Node();            root1.right.right.right.data = 9;            PrintBinaryTree(root1);            Node root2 = new Node();            root2.data = 1;            root2.right = new Node();            root2.right.data = 2;            root2.right.right = new Node();            root2.right.right.data = 3;            root2.right.right.right = new Node();            root2.right.right.right.data = 4;            root2.right.right.right.right = new Node();            root2.right.right.right.right.data = 5;            Console.WriteLine("\n");            PrintBinaryTree(root2);            Console.ReadKey();        }    }}

如有错误,欢迎指出。

email:dxmdxm1992#gmail.com

blog: csdn magicdavid.top

0 0
原创粉丝点击