码迷,mamicode.com
首页 > 其他好文 > 详细

打印二叉树

时间: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!