打印二叉樹
阿新 • • 發佈:2018-04-11
算法 二叉樹 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)));
}
}
打印二叉樹