博客说明
文章所涉及的资料来自互联网整理和个人总结,意在于个人学习和经验汇总,如有什么地方侵权,请联系本人删除,谢谢!
package cn.guizimo.tree; /** * @author guizimo * @date 2020/8/5 11:09 上午 */ public class ArrTree { public static void main(String[] args) { int[] arr = {1, 2, 3, 4, 5, 6, 7}; ArrBinaryTree arrBinaryTree = new ArrBinaryTree(arr); arrBinaryTree.preOrder(); } } class ArrBinaryTree { private int[] arr; public ArrBinaryTree(int[] arr) { this.arr = arr; } public void preOrder(){ this.preOrder(0); } //前序 public void preOrder(int index) { if (arr == null || arr.length == 0) { System.out.println("数组为空"); } System.out.println(arr[index]); //左递归 if ((index * 2 + 1) < arr.length) { preOrder(2 * index + 1); } //右递归 if ((index * 2 + 2) < arr.length) { preOrder(2 * index + 2); } } //中序 public void preOrder(int index) { if (arr == null || arr.length == 0) { System.out.println("数组为空"); } //左递归 if ((index * 2 + 1) < arr.length) { preOrder(2 * index + 1); } System.out.println(arr[index]); //右递归 if ((index * 2 + 2) < arr.length) { preOrder(2 * index + 2); } } //后序 public void preOrder(int index) { if (arr == null || arr.length == 0) { System.out.println("数组为空"); } //左递归 if ((index * 2 + 1) < arr.length) { preOrder(2 * index + 1); } //右递归 if ((index * 2 + 2) < arr.length) { preOrder(2 * index + 2); } System.out.println(arr[index]); } }
原文链接:https://www.cnblogs.com/guizimo/p/13438811.html