顺序存储二叉树概念:
1.通常只考虑完全二叉树
2.第n个元素左子节点为2n+1
3.第n个元素右子节点为2n+2
4.第n个元素父节点为(n-1)/2
顺序存储二叉树遍历
public class ArrayBinaryTree{
public static void main(String[] args) {
int[] a = {1,2,3,4,5,6,7};
ArrayBinaryTree arrayBinaryTree = new ArrayBinaryTree(a);
arrayBinaryTree.preOrder();
}
int[] a;
public ArrayBinaryTree(int[] a) {
this.a = a;
}
public void preOrder() {
this.preOrder(0);
}
public void preOrder(int index) {
if(a != null && a.length > 0)
System.out.print(a[index]+" ");
else {
System.out.println("二叉树为空无法遍历");
}
if(2 * index + 1 < a.length)
preOrder(2*index+1);
if(2 * index + 2 < a.length)
preOrder(2*index+2);
}
}