class TOne{
public int m;
public TOne tLeft;
public TOne tRight;
public TOne(){
}
public void transverse(){
System.out.println("value "+m);
if(tLeft!=null){
System.out.println("Left tree ");
tLeft.transverse();
}
if(tRight!=null){
System.out.println("Right tree ");
tRight.transverse();
}
}
public boolean insertValue(int vale){
if(m==0){
m=vale;
return true;
}else if(m!=0){
if(vale<m){
if(tLeft==null){
tLeft=new TOne();
}
return tLeft.insertValue(vale);
}else if(vale>m){
if(tRight==null){
tRight=new TOne();
}
return tRight.insertValue(vale);
}else {
System.out.println(" value equal.");
return false;
}
}
return false;
}
public boolean search(int val){
if(m==val){
return true;
}else if(tLeft!=null){
return tLeft.search(val);
}else if(tRight!=null){
return tRight.search(val);
}else{
return false;
}
}
public static void main(String arr[]){
TOne one=new TOne();
System.out.println(""+one.insertValue(10));
System.out.println(""+one.insertValue(20));
System.out.println(""+one.insertValue(16));
System.out.println(""+one.insertValue(12));
System.out.println("");
one.transverse();
System.out.println("search 10 : "+one.search(10));
System.out.println("search 20 : "+one.search(20));
System.out.println("search 16 : "+one.search(16));
System.out.println("search 4 : "+one.search(4));
System.out.println("search 4 : "+one.size());
}
public int size(){
int size=1;
if(tLeft!=null && tRight!=null){
int leftSize=tLeft.getSize(size);
int rightSize=tRight.getSize(size);
if(leftSize>rightSize){
return leftSize;
}else if(leftSize<rightSize){
return rightSize;
}else if(leftSize==rightSize){
return leftSize;
}
return leftSize;
}else if(tLeft!=null && tRight==null){
return tLeft.getSize(size);
}else if(tLeft==null && tRight!=null){
return tRight.getSize(size);
}else{
return size;
}
}
public int getSize(int sizze){
if(tLeft==null && tRight == null){
sizze+=1;
return sizze;
}else if(tLeft!=null && tRight==null){
sizze+=1;
return tLeft.getSize(sizze);
}else if(tLeft==null && tRight!=null){
sizze+=1;
return tRight.getSize(sizze);
}else{
sizze+=1;
int leftSize=tLeft.getSize(sizze);
int rightSize=tRight.getSize(sizze);
if(leftSize>rightSize){
return leftSize;
}else if(leftSize<rightSize){
return rightSize;
}else if(leftSize==rightSize){
return leftSize;
}
return leftSize;
}
}
}
public int m;
public TOne tLeft;
public TOne tRight;
public TOne(){
}
public void transverse(){
System.out.println("value "+m);
if(tLeft!=null){
System.out.println("Left tree ");
tLeft.transverse();
}
if(tRight!=null){
System.out.println("Right tree ");
tRight.transverse();
}
}
public boolean insertValue(int vale){
if(m==0){
m=vale;
return true;
}else if(m!=0){
if(vale<m){
if(tLeft==null){
tLeft=new TOne();
}
return tLeft.insertValue(vale);
}else if(vale>m){
if(tRight==null){
tRight=new TOne();
}
return tRight.insertValue(vale);
}else {
System.out.println(" value equal.");
return false;
}
}
return false;
}
public boolean search(int val){
if(m==val){
return true;
}else if(tLeft!=null){
return tLeft.search(val);
}else if(tRight!=null){
return tRight.search(val);
}else{
return false;
}
}
public static void main(String arr[]){
TOne one=new TOne();
System.out.println(""+one.insertValue(10));
System.out.println(""+one.insertValue(20));
System.out.println(""+one.insertValue(16));
System.out.println(""+one.insertValue(12));
System.out.println("");
one.transverse();
System.out.println("search 10 : "+one.search(10));
System.out.println("search 20 : "+one.search(20));
System.out.println("search 16 : "+one.search(16));
System.out.println("search 4 : "+one.search(4));
System.out.println("search 4 : "+one.size());
}
public int size(){
int size=1;
if(tLeft!=null && tRight!=null){
int leftSize=tLeft.getSize(size);
int rightSize=tRight.getSize(size);
if(leftSize>rightSize){
return leftSize;
}else if(leftSize<rightSize){
return rightSize;
}else if(leftSize==rightSize){
return leftSize;
}
return leftSize;
}else if(tLeft!=null && tRight==null){
return tLeft.getSize(size);
}else if(tLeft==null && tRight!=null){
return tRight.getSize(size);
}else{
return size;
}
}
public int getSize(int sizze){
if(tLeft==null && tRight == null){
sizze+=1;
return sizze;
}else if(tLeft!=null && tRight==null){
sizze+=1;
return tLeft.getSize(sizze);
}else if(tLeft==null && tRight!=null){
sizze+=1;
return tRight.getSize(sizze);
}else{
sizze+=1;
int leftSize=tLeft.getSize(sizze);
int rightSize=tRight.getSize(sizze);
if(leftSize>rightSize){
return leftSize;
}else if(leftSize<rightSize){
return rightSize;
}else if(leftSize==rightSize){
return leftSize;
}
return leftSize;
}
}
}