首页
Web开发
Windows程序
编程语言
数据库
移动开发
系统相关
微信
其他好文
会员
首页
>
其他好文
> 详细
打印二叉树
时间:
2018-04-11 15:38:02
阅读:
151
评论:
0
收藏:
0
[点我收藏+]
标签:
算法
二叉树
import java.util.LinkedList; import java.util.Queue; import java.util.Stack; /** * 1 */ * 2 3 * | | \ * 4 5 6 * / * 7 8 */ class BinaryTree { private int value = 0; private LinkedList<BinaryTree> child = new LinkedList<BinaryTree>(); public BinaryTree(int value,BinaryTree left,BinaryTree right){ this.value = value; this.child.add(left); this.child.add(right); } public BinaryTree(int value){ this.value = value; } public void setValue(int value){ this.value = value; } public int getValue(){ return value; } public BinaryTree getLeftChild(){ return child.getFirst(); } public BinaryTree getRightChild(){ return child.getLast(); } public String toString(){ return String.valueOf(value); } /** * 先序遍历 * @param node */ public static void printByFirst(BinaryTree node){ if(node==null)return; System.out.print(node); while(node.child.peek()!=null){ printByFirst(node.child.poll()); } } /** * 后序遍历 * @param node */ public static void printByLast(BinaryTree node){ if(node==null)return; while(node.child.peek()!=null){ printByLast(node.child.poll()); } System.out.print(node ); } /** * 中序遍历 * @param node */ public static void printByMid(BinaryTree node){ if(node == null) return; printByMid(node.getLeftChild()); System.out.print(node); printByMid(node.getRightChild()); } /** * 按行打印 * @param root * @throws InterruptedException */ public static void printToRow(BinaryTree root){ try { if(root == null) return; Queue<BinaryTree> queue = new LinkedList<BinaryTree>(); BinaryTree tlast = root; BinaryTree nlast = root ; queue.add(root); while(queue.size()>0){ for(int i=0;i<queue.size();i++){ BinaryTree temp = queue.remove(); System.out.print(temp +","); Thread.sleep(100); if(temp.getLeftChild()!=null){ queue.add(temp.getLeftChild()); nlast = temp.getLeftChild(); } if(temp.getRightChild()!=null){ queue.add(temp.getRightChild()); nlast = temp.getRightChild(); } if(temp == tlast){ System.out.println(); Thread.sleep(100); tlast = nlast; } } } } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 顺序打印 * @param node */ public static void printToList(BinaryTree node){ if(node==null)return; Queue<BinaryTree> queue = new LinkedList<BinaryTree>(); queue.add(node); while(queue.size()>0){ for(int i=0;i<queue.size();i++){ BinaryTree temp = queue.remove(); System.out.print(temp+","); if(temp.getLeftChild()!=null) queue.add(temp.getLeftChild()); if(temp.getRightChild() !=null) queue.add(temp.getRightChild()); } } } /**序列化 * */ public static String serialize(BinaryTree node){ StringBuilder ser = new StringBuilder(); if(node==null){ return ser.append("#!").toString(); }else{ ser.append(node+"!"); ser.append(serialize(node.getLeftChild())); ser.append(serialize(node.getRightChild())); return ser.toString(); } } /** * 反序列化 * @param strNode */ public static BinaryTree deserialize(String strNode){ if(strNode==null) return null; String[] values = strNode.split("!"); Stack<BinaryTree> stack = new Stack<BinaryTree>(); BinaryTree root = null ; for(String v:values){ if(stack.isEmpty()){ root = new BinaryTree(Integer.valueOf(v)); stack.push(root); continue; } while(stack.peek().child.size()==2){ stack.pop(); } if(!"#".equals(v)){ BinaryTree node = new BinaryTree(Integer.valueOf(v)); stack.peek().child.add(node); stack.push(node); }else{ stack.peek().child.add(null); } } return root; } } /** * 1 */ * 2 3 * / / \ *4 5 6 * / * 7 8 */ public class PrintTree{ public static void main(String[] args) throws InterruptedException { BinaryTree root = bulidTruee(); System.out.println("序列化"); String str = BinaryTree.serialize(root); System.out.println(str); System.out.println("反序列化"); root = BinaryTree.deserialize(str); System.out.println("序列化"); str = BinaryTree.serialize(root); System.out.println(str); } private static BinaryTree bulidTruee(){ return new BinaryTree(1,new BinaryTree(2,new BinaryTree(4,null,null),null),new BinaryTree(3,new BinaryTree(5,new BinaryTree(7,null,null),new BinaryTree(8,null,null)),new BinaryTree(6,null,null))); } }
打印二叉树
标签:
算法
二叉树
原文地址:http://blog.51cto.com/12336708/2096873
踩
(
0
)
赞
(
0
)
举报
评论
一句话评论(
0
)
登录后才能评论!
分享档案
更多>
2021年07月29日 (22)
2021年07月28日 (40)
2021年07月27日 (32)
2021年07月26日 (79)
2021年07月23日 (29)
2021年07月22日 (30)
2021年07月21日 (42)
2021年07月20日 (16)
2021年07月19日 (90)
2021年07月16日 (35)
周排行
更多
分布式事务
2021-07-29
OpenStack云平台命令行登录账户
2021-07-29
getLastRowNum()与getLastCellNum()/getPhysicalNumberOfRows()与getPhysicalNumberOfCells()
2021-07-29
【K8s概念】CSI 卷克隆
2021-07-29
vue3.0使用ant-design-vue进行按需加载原来这么简单
2021-07-29
stack栈
2021-07-29
抽奖动画 - 大转盘抽奖
2021-07-29
PPT写作技巧
2021-07-29
003-核心技术-IO模型-NIO-基于NIO群聊示例
2021-07-29
Bootstrap组件2
2021-07-29
友情链接
兰亭集智
国之画
百度统计
站长统计
阿里云
chrome插件
新版天听网
关于我们
-
联系我们
-
留言反馈
© 2014
mamicode.com
版权所有 联系我们:gaon5@hotmail.com
迷上了代码!