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