package com.data.struct.tree.binaryTree;
public class ArrayBinTree<T extends Comparable<T>> {
/* 树高度 */
private int deepth = 0;
/* 存储树结构数据 */
private Object[] arr ;
/* 数组大小 */
private int size = 0;
public ArrayBinTree(int dpth,T data){
this.deepth = dpth;
init(data);
}
private void init(T data){
this.size = (int)Math.pow(2, this.deepth)-1;
this.arr = new Object[this.size];
this.arr[0]=data;
}
public int[] getNodeIndexBydepth(int h){
int len=(int)Math.pow(2, h-1);
int[] tmparr = new int[len];
int start=0;
if(h>1){
start =(int)Math.pow(2, h-1)-1;
}
for(int i=0;i<len;i++){
tmparr[i]=start;
start++;
}
//for(int i=0;i<len;i++){
//System.out.print(tmparr[i]);System.out.print(",");
//}
//System.out.println("-----------");
//
return tmparr;
}
/**
* 在父节点添加子节点
* @param parentIndex
* @param data
* @param isLeft
*/
public int addNode(int parentIndex,T data,boolean isLeft){
int index=0;
if(parentIndex<0){
throw new IllegalArgumentException("不合法的父节点");
}
if(((parentIndex)*2+2)>=arr.length){
throw new IndexOutOfBoundsException("数组越界!");
}
if(isLeft){
index=parentIndex*2+1;
this.arr[parentIndex*2+1]=data;
}else{
index=parentIndex*2+2;
this.arr[(parentIndex)*2+2]=data;
}
return index;
}
/**
* 判断书是否为空
* @return
*/
public boolean isEmpty(){
if(arr[0]==null){
return true;
}else{
return false;
}
}
/**
* 根据父节点去取子节点信息
* @param parentIndex
* @param isLeft
* @return
*/
@SuppressWarnings({"unchecked"})
public T getSubNode(int parentIndex,boolean isLeft){
if(parentIndex<0){
throw new IllegalArgumentException("不合法的父节点");
}
if(((parentIndex)*2+2)>=arr.length){
throw new IndexOutOfBoundsException("数组越界!");
}
if(isLeft){
parentIndex = parentIndex*2+1;
}else{
parentIndex = parentIndex*2+2;
}
return (T)this.arr[parentIndex];
}
/**
* 返回根节点
*/
@SuppressWarnings( { "unchecked" })
public T getRootNode(){
return (T)this.arr[0];
}
/**
* 根据子节点获取父节点信息
* @param index
* @return
*/
@SuppressWarnings({"unchecked"})
public T getParentNode(int index){
if(index>=this.arr.length){
throw new IndexOutOfBoundsException("数组越界!");
}
index = index/2;
return (T)this.arr[index];
}
/**
* 树的大小
* @return
*/
public int getTreeSize(){
return this.arr.length;
}
/**
* 根据名称查找节点索引
* @param obj
* @return
*/
public int getNodeIndex(T obj){
for(int i=0;i<arr.length;i++){
if(this.arr[i]!=null && this.arr[i].equals(obj)){
return i;
}
}
return -1;
}
public Object[] getNodesByDpth(int h){
int tmparr[]=getNodeIndexBydepth(h);
Object[] arr2=new Object[tmparr.length];
int j=0;
for(int i:tmparr){
arr2[j]=this.arr[i];
j++;
}
return arr2;
}
/**
*
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ArrayBinTree<String> tree = new ArrayBinTree<String>(20, "家庭");
int index=0;
int index2=0;
index=tree.addNode(0, "男", true);
index2=tree.addNode(0, "女", false);
tree.addNode(index, "爷爷他弟", false);
index=tree.addNode(index, "爷爷", true);
tree.addNode(index, "爸爸他弟", false);
index=tree.addNode(index, "爸爸", true);
tree.addNode(index, "儿子", true);
tree.addNode(tree.getNodeIndex("女"), "奶奶他妹", false);
index2=tree.addNode(index2, "奶奶", true);
tree.addNode(tree.getNodeIndex("奶奶"), "妈妈他妹", false);
index2=tree.addNode(index2, "妈妈", true);
index2=tree.addNode(index2, "女儿", true);
for(int d=0;d<5;d++){
System.out.print("第"+(d+1)+"层:");
Object[] arr2=tree.getNodesByDpth(d+1);
for(Object obj:arr2){
if(obj==null){
continue;
}
System.out.print((String)obj+",");
}
System.out.println();
}
}
}