打印二叉樹

import java.util.ArrayList;

import java.util.Comparator;

import java.util.LinkedList;

import java.util.List;

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;

}

/**

* 得到樹深度

* @param root

* @return

*/

public static int getDepth(BinaryTree root){

if(root == null ){

return 0;

}

int leftDepth = getDepth(root.getLeftChild()) +1;

int rightDepth = getDepth(root.getRightChild()) +1;

return leftDepth>rightDepth?leftDepth:rightDepth;

}

/**

* 得到樹節節點最大距離

* @param root

* @return

*/

public static int getMaxDestance(BinaryTree root){

if(root == null ){

return 0;

}

int leftDepth = getDepth(root.getLeftChild()) ;

int rightDepth = getDepth(root.getRightChild()) ;

return leftDepth + rightDepth +1;

}

static List globalv = new ArrayList();

/**

* 查找 子樹中 最大搜索樹

* @param root

* @return

* @throws InterruptedException

*/

public static List getMaxSearchTree(BinaryTree root) throws InterruptedException{

List v = new ArrayList();

if(root==null || root.getLeftChild()==null || root.getRightChild()==null){

return v;

}

List l = getMaxSearchTree(root.getLeftChild());

List r = getMaxSearchTree(root.getRightChild());

/**

* 0。頭結點

* 1。樹節點個數

* 2。最小值

* 3。最大值

*/

if(root.getLeftChild().value<root.value && root.value<root.getRightChild().value){

if(l.size()== 4 && r.size() == 4 && Integer.valueOf(l.get(3).toString())<Integer.valueOf(r.get(2).toString())){

v.add(root);

v.add(Integer.valueOf(l.get(1).toString())+Integer.valueOf(r.get(1).toString()));

v.add(l.get(2));

v.add(r.get(3));

}else{

v.add(root);

v.add(3);

v.add(root.getLeftChild().value);

v.add(root.getRightChild().value);

}

}

if(v.size()>1){

if(globalv.size()==0){

globalv = v;

}else{

if(Integer.valueOf(globalv.get(1).toString())<Integer.valueOf(v.get(1).toString())){

globalv = v;

}

}

}

System.out.println(globalv);

return v;

}

public static int compare(BinaryTree paramT1, BinaryTree paramT2) {

// TODO Auto-generated method stub

return paramT1.value - paramT2.value;

}

}


/**

* 3

* /  \

* 2    9 

* /    / \ 

*4    70   9

 * / \   / \

 *      5   8 80  100

 *       

 *        

 */

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);

BinaryTree.printToRow(root);

BinaryTree.getMaxSearchTree(root);

}

private static BinaryTree bulidTruee(){

return new BinaryTree(3,new BinaryTree(2,new BinaryTree(4,null,null),null),new BinaryTree(9,new BinaryTree(70,new BinaryTree(5,null,null),new BinaryTree(8,null,null)),new BinaryTree(9,new BinaryTree(80,null,null),new BinaryTree(100,null,null))));

// return new BinaryTree(1,new BinaryTree(2,null,null),new BinaryTree(3,new BinaryTree(5,null,null),new BinaryTree(100,null,null)));

}

}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章