public class BTree {
private int node;
private BTree LChild ;
private BTree RChild ;
private BTree(int node){
this.node = node;
LChild = null;
RChild = null;
}
public BTree getL(){
return LChild;
}
public BTree getR(){
return RChild;
}
public void setL(BTree L){
this.LChild = L;
}
public void setR(BTree R){
this.RChild = R;
}
public int getNode(){
return node;
}
/**
* 根据树高建树
* @param num
* @return
*/
public static BTree InitBTree(int num){
if(num == 0){
return null;
}
BTree root = new BTree(num);
root.setL(InitBTree(num-1));
root.setR(InitBTree(num-1));
return root;
}
/**
* 统计节点个数
* @param node
* @return
*/
public static int getNodeNum(BTree node){
if(node.getL()==null && node.getR()==null)
return 1;
return getNodeNum(node.getL()) + getNodeNum(node.getR()) + 1;
}
public static void main(String[] args){
BTree root2 = InitBTree(3);
System.out.println(getNodeNum(root2));
}
}
JAVA数据结构--根据树高生成完全二叉树(java实现)
猜你喜欢
转载自blog.csdn.net/shuzishij/article/details/86255541
今日推荐
周排行