学习数据结构和算法的日常Demo
何为顺序存储二叉树
具体要求
存储特点
代码实现
public class ArrayBinaryTree {
private int[] arr;
public ArrayBinaryTree(int[] arr) {
this.arr = arr;
}
// 前序遍历
// index:数组下标
public void preOrder(int index) {
// 如果数组为空,或者length为0
if (arr == null || arr.length == 0) {
System.out.println("数组为空!");
}
System.out.print(arr[index] + " ");
// 向左递归
if ((index * 2 + 1) < arr.length) {
preOrder(2 * index + 1);
}
// 向右递归
if ((index * 2 + 2) < arr.length) {
preOrder(index * 2 + 2);
}
}
// 中序遍历
// index:数组下标
public void infixOrder(int index) {
// 如果数组为空,或者length为0
if (arr == null || arr.length == 0) {
System.out.println("数组为空!");
}
// 向左递归
if ((index * 2 + 1) < arr.length) {
infixOrder(2 * index + 1);
}
System.out.print(arr[index] + " ");
// 向右递归
if ((index * 2 + 2) < arr.length) {
infixOrder(index * 2 + 2);
}
}
// 后序遍历
// index:数组下标
public void postOrder(int index) {
// 如果数组为空,或者length为0
if (arr == null || arr.length == 0) {
System.out.println("数组为空!");
}
// 向左递归
if ((index * 2 + 1) < arr.length) {
postOrder(2 * index + 1);
}
// 向右递归
if ((index * 2 + 2) < arr.length) {
postOrder(index * 2 + 2);
}
System.out.print(arr[index] + " ");
}
}
// 测试类
public class ArrayBinaryTreeDemo {
public static void main(String args[]) {
int arr[] = {1, 2, 3, 4, 5, 6, 7};
ArrayBinaryTree tree = new ArrayBinaryTree(arr);
/*
1
2 3
4 5 6 7
*/
tree.preOrder(0); // 首元素相当于根节点
System.out.println("前序遍历");
tree.infixOrder(0);
System.out.println("中序遍历");
tree.postOrder(0);
System.out.println("后序遍历");
}
}