java算法9~二叉树前序、中序、后序遍历
来源:互联网 发布:魔钢歼灭者淘宝价格 编辑:程序博客网 时间:2024/06/10 07:36
1、定义:前序、中序、后序的概念
前序遍历:
1.访问根节点
2.前序遍历左子树
3.前序遍历右子树
中序遍历:
1.中序遍历左子树
2.访问根节点
3.中序遍历右子树
后序遍历:
1.后序遍历左子树
2.后序遍历右子树
3.访问根节点
2、算法图示
前序输出:C A B E F D H G
中序输出:B A F E C H D G
后序输出:B F E A H G D C
3、算法实现
稍后更新
定义节点:
package erchashu_bianli;/** * * @author zhengchao */public class Node { public int value; public Node left; public Node right; public Node(int value){ this.value = value; } }
定义三个遍历的方法:
package erchashu_bianli;/** * * @author zhengchao */public class OrderUtil { public static void preOrder(Node node){ if(node != null){ System.out.print(node.value);//先中间,直接输出 preOrder(node.left); //再左边 preOrder(node.right); //最后左边 } } public static void middleOrder(Node node){ if(node != null){ middleOrder(node.left); //先左边 System.out.print(node.value); //再中间直接输出 middleOrder(node.right); //最后右边 } } public static void lastOrder(Node node){ if(node != null){ lastOrder(node.left); //先左边 lastOrder(node.right); //再右边 System.out.print(node.value); //最后输出中间 } } }测试案例:首先需要构造二叉树的数据结构,再进行遍历
1
2 3
4 5 6 7
8 9
package erchashu_bianli;import java.util.*;/** * * @author zhengchao */public class TestNode { private int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; private static List<Node> nodeList = null; //有没有发现初始化构建这个二叉树的数据结构是最难的 public void createErChaTree() { nodeList = new LinkedList<Node>(); // 将一个数组的值依次转换为Node节点 for (int nodeIndex = 0; nodeIndex < array.length; nodeIndex++) { nodeList.add(new Node(array[nodeIndex])); } // 对前lastParentIndex-1个父节点按照父节点与孩子节点的数字关系建立二叉树 for (int parentIndex = 0; parentIndex < array.length / 2 - 1; parentIndex++) { // 左孩子 nodeList.get(parentIndex).left = nodeList.get(parentIndex * 2 + 1); // 右孩子 nodeList.get(parentIndex).right = nodeList.get(parentIndex * 2 + 2); } // 最后一个父节点:因为最后一个父节点可能没有右孩子,所以单独拿出来处理 int lastParentIndex = array.length / 2 - 1; // 左孩子 nodeList.get(lastParentIndex).left = nodeList.get(lastParentIndex * 2 + 1); // 右孩子,如果数组的长度为奇数才建立右孩子 if (array.length % 2 == 1) { nodeList.get(lastParentIndex).right = nodeList.get(lastParentIndex * 2 + 2); } } public static void main(String[] args){ TestNode binTree = new TestNode(); binTree.createErChaTree(); Node root = nodeList.get(0); System.out.println("先序遍历:"); OrderUtil.preOrder(root); System.out.println(); System.out.println("中序遍历:"); OrderUtil.middleOrder(root); System.out.println(); System.out.println("后序遍历:"); OrderUtil.lastOrder(root); } }
输出结果:
run:先序遍历:124895367中序遍历:849251637后序遍历:894526731成功构建 (总时间: 0 秒)
1 0
- java算法9~二叉树前序、中序、后序遍历
- 4. 二叉树前序、中序、后序递归遍历算法,二叉树前序非递归遍历算法
- 根据二叉树前序遍历和中序遍历序列求解后序遍历的算法
- 二叉树前序遍历、中序遍历和后序遍历
- 用Java实现二叉树前序遍历、中序遍历和后序遍历。
- 二叉树前序、中序、后序遍历
- 二叉树前序、中序、后序遍历
- 二叉树前序,中序,后序遍历
- 二叉树前序、中序、后序遍历
- 二叉树前序、中序、后序遍历
- java二叉树的增、删、查、前序遍历、中序遍历和后序遍历 算法 源代码
- 先序、中序、后序遍历二叉树 算法
- 二叉树先序、中序、后序遍历算法的实现
- 用java实现二叉树非递归的前序,中序,后序遍历算法
- Java实现二叉树,以及先序、中序、后序遍历算法的实现
- 二叉树前序遍历 中序遍历 后续遍历 算法实现
- 二叉树遍历(Java)---前序遍历,中序遍历,后序遍历
- 二叉树的前序遍历、中序遍历和后序遍历及其算法
- 操作系统学习笔记--操作系统基本概念
- 杂项设备的实现
- 彩票小程序蓝色4位,红色2位
- 51nod 1281 山峰和旗子【二分+dp】好题~
- 下拉框之间的联动效果
- java算法9~二叉树前序、中序、后序遍历
- MySQL添加用户、删除用户与授权
- 语句复制(SBR)的缺陷列举
- 关于使用ueditor时产生的问题
- zookeeper 安装配置
- 润乾报表连接domino数据库的方法
- if-else小练习(成绩划分)
- mybatis,hibernate 的差异
- ios 应用之间的跳转