直观地打印二叉树
来源:互联网 发布:手机流量开关软件 编辑:程序博客网 时间:2024/06/06 08:41
在写二叉树时经常希望把它可视化,本文实现了相对直观地打印二叉树。
主要思路:
- 空节点使用 n (null)表示
- 通过节点的层数计算间隔
效果图如下:
代码实现:
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
- 直观地打印二叉树
- 直观地打印二叉树
- 直观地打印二叉树
- 二叉树问题---较为直观的打印二叉树
- 二叉树反序列化与直观打印
- java实现二叉树(打印直观,易于理解)
- 一个遍历二叉树的直观方法
- 【二叉树】二叉树打印
- 分层打印二叉树
- ZigZag打印二叉树
- 分层打印二叉树
- 横向打印二叉树
- 用凹入表打印二叉树
- 完美打印二叉树
- 逐行打印二叉树
- 之字形打印二叉树
- 层次打印二叉树
- 之字形打印二叉树
- 解决-Dmaven.multiModuleProjectDirectory system property is not set. Check $M2_HOME environment variabl
- 复杂链表的复制
- Android 热修复 Tinker接入及源码浅析最精彩没有之一
- 访问网络文件共享服务
- POJ 1163 The Triangle
- 直观地打印二叉树
- django中使用ckeditor
- 类与对象
- 【问题解决】MYSQL 服务无法启动,错误日志:InnoDB: .\ibdata1 must be writable
- 169. Majority Element
- Mac OS X 安装 Nginx
- jsp连接mysql数据库,实现含验证码的用户登录
- Laravel中用Redis来做任务队列---出现重载问题
- C++无法被继承的类实现以及单例模式的类的实现