用数组来实施树的小程序

public class ArrayTree{
  Object[] array;
  int size;
  public ArrayTree(){
    array=new Object[128];
  }
  public Object getCargo(int i){
    return array[i];
  }
  public void setCargo(int i,Object obj){
    array[i]=obj;
  }
  public int getLeft(int i){return 2*i;}
  public int getRight(int i){return 2*i+1;}
  public int getParent(int i){return i/2;}

  public static void main(String args[]){
    ArrayTree tree=new ArrayTree();
    tree.setCargo(1,"cargo for root");
    tree.setCargo(tree.getLeft(1),"cargo for left");
    tree.setCargo(tree.getRight(1),"cargo for right");
    tree.print(1);
  }
  public void print(int i){
    Object cargo=this.getCargo(i);
    if(cargo==null) return;
    System.out.println(cargo);
    print(getLeft(i));
    print(getRight(i));
  }
}


《探秘JAVA》

你可能感兴趣的:(java)