package jiegou.tree;
import jdk.packager.services.userjvmoptions.PreferencesUserJvmOptions;
import java.util.ArrayList;
public class ArrayTreeDemo {
public static void main(String[] args) {
int[] arr = {1,2,3,4,5,6,7};
ArrayBinaryTree arrayBinaryTree = new ArrayBinaryTree(arr);
arrayBinaryTree.preOrder();
}
}
class ArrayBinaryTree
{
private int[] arr;// 存储数组
public ArrayBinaryTree(int[] arr)
{
this.arr = arr;
}
public void preOrder(){
this.preOrder(0);
}
// 完成顺序存储
// index 数组的下标
public void preOrder(int index)
{
if(arr == null || arr.length == 0) {
System.out.println("数数为空 不能按二叉树遍历");
return;
}
// 根
System.out.println(arr[index]);
// 左
if(index * 2 +1 < arr.length ){
preOrder(index*2+1);
}
// 右
if(index * 2 +2 < arr.length){
preOrder(index*2 + 2);
}
}
}