publicclassTree{publicint value;public Tree left;public Tree right;@Overridepublic String toString(){return"Tree{"+"value="+ value +", left="+ left +", right="+ right +'}';}}
树的存储队列
publicclassQueueTest< T >{private T[] data =(T[])newObject[20];privateint start =0;privateint end =0;publicvoidpush(T newint){if( end - start == data.length ){
T[] datanew =(T[])newObject[ data.length *2];for(int i =0; i < data.length; i++){
datanew[i]= data[( start + i )% data.length ];}
start =0;
end = start + data.length;
data = datanew;}
data[ end % data.length ]= newint;
end++;}public T get(){if( end == start ){return null;}
T result = data[start % data.length];
start++;return result;}}
Test类进行插入并将根打印出来
publicclassTest{publicstaticvoidmain(String[] args){// TODO Auto-generated method stubint[] arr ={1,4,7,2,43,12,66,43,21,87,67,54,32,11,0};
Tree root =newTree();
root.value = arr[0];
QueueTest<Tree> queue =newQueueTest<Tree>();
queue.push(root);for(int i =1; i < arr.length;){
Tree newtree = queue.get();
Tree nodel =newTree();
nodel.value = arr[ i ];
newtree.left = nodel;
queue.push(nodel);if( i+1<arr.length ){
Tree noder =newTree();
noder.value = arr[ i+1];
newtree.right = noder;
queue.push(noder);}else{break;}
i +=2;}
System.out.println(root);}}