package com.tree.bitree;
public class BinTree {
static abstract class Visitor{
void visit(Object obj) {
System.out.print(obj+" ");
}
}
protected Node root;
private int size;
private static class Node{
int data;
Node right;
Node left;
public Node(int data){
this.data= data;
}
public String toString() {
return "number="+data;
}
}
public void createBinTree(int nums[]) {
root=reCreateTree(nums,0);
}
public Node reCreateTree(int[] nums,int index) {
if(nums[index]!=0) {
size++;
Node rootNode=new Node(nums[index]);
//rootNode.data=nums[index];
if((index+1)*2<=nums.length) {
rootNode.left = (Node)reCreateTree(nums,(index+1)*2-1);
if((index+1)*2+1<=nums.length) { rootNode.right = (Node) reCreateTree(nums,(index+1)*2); }
}
return rootNode;
}
return null;
}
public void createFullBintree(int numCount) {
root=reCreateFullBintree(1,numCount);
}
private Node reCreateFullBintree(int index,int numCount) {
size++;
Node rootNode=new Node(index);
if(index*2<=numCount) {
rootNode.left=(Node)reCreateFullBintree(index*2,numCount);
if(index*2+1<=numCount) rootNode.right=(Node)reCreateFullBintree(index*2+1,numCount);
}
return (Node)rootNode;
}
public int Size() {
return size;
}
public int getLift() {
Node e=root;
while(e.right !=null) {
e=e.right;
}
return e.data;
}
public void preOrder(Visitor v) {
preOrder(v,root);
}
private void preOrder(Visitor v,Node Root) {
if(Root!=null){
v.visit(Root.data);
preOrder(v,Root.left);
preOrder(v,Root.right);
}
}
public void infexOrder(Visitor v) {
inOrder(v,root);
}
private void inOrder(Visitor v,Node Root) {
if(Root != null) {
inOrder(v,Root.left);
v.visit(Root.data);
inOrder(v,Root.right);
}
}
public void postOrder(Visitor v) {
postOrder(v,root);
}
private void postOrder(Visitor v,Node Root) {
if(Root != null) {
postOrder(v,Root.left);
postOrder(v,Root.right);
v.visit(Root);
}
}
public void postFOrder(Visitor v) {
postFOrder(v,root);
}
private void postFOrder(Visitor v,Node Root) {
if(Root != null) {
postFOrder(v,Root.right);
postFOrder(v,Root.left);
v.visit(Root);
}
}
public void outPutData(){
disPlay(root);
}
public void disPlay(Node Root) {
if(Root!=null)
{
System.out.print(Root.data);
System.out.print(" ");
disPlay(Root.left);
disPlay(Root.right);
}
}
public static void main(String[] args) {
BinTree bintree=new BinTree();
bintree.createFullBintree(15);
System.out.println("SIze:"+bintree.Size());
System.out.println (bintree.getLift());
bintree.preOrder(new Visitor() {});
System.out.println();
bintree.infexOrder(new Visitor() {});
System.out.println();
bintree.postOrder(new Visitor() {});
System.out.println();
bintree.postFOrder(new Visitor() {});
System.out.println();
bintree.outPutData();
System.out.println("");
bintree=new BinTree();
int[] nums=new int[] { 1, 2, 3, 4, 0, 0, 5, 0, 6, 0, 0, 0, 0, 7, 8 };
bintree.createBinTree(nums);
System.out.println("SIze: "+bintree.Size());
System.out.println(bintree.getLift());
bintree.preOrder(new Visitor() {});
System.out.println();
bintree.infexOrder(new Visitor() {});
System.out.println();
bintree.postOrder(new Visitor() {});
System.out.println();
bintree.postFOrder(new Visitor() {});
System.out.println();
}
}