关于二叉树的遍历

来源:互联网 发布:聊天软件默默 编辑:程序博客网 时间:2024/06/07 05:05
</pre>先构建树:1.把一个数组的值赋值给一颗二叉树 2.具体代码 <span style="font-family:Helvetica,Tahoma,Arial,sans-serif; font-size:14px; line-height:25.1875px"></span><br style="font-family:Helvetica,Tahoma,Arial,sans-serif; font-size:14px; line-height:25.1875px" /><br style="font-family:Helvetica,Tahoma,Arial,sans-serif; font-size:14px; line-height:25.1875px" /><span style="font-family:Helvetica,Tahoma,Arial,sans-serif; font-size:14px; line-height:25.1875px; color:red">1.树的构建方法</span><span style="font-family:Helvetica,Tahoma,Arial,sans-serif; font-size:14px; line-height:25.1875px"> </span><br style="font-family:Helvetica,Tahoma,Arial,sans-serif; font-size:14px; line-height:25.1875px" /><br style="font-family:Helvetica,Tahoma,Arial,sans-serif; font-size:14px; line-height:25.1875px" /><p><img src="http://dl.iteye.com/upload/picture/pic/90395/70fbf233-89b4-35b6-8582-61431b6fe025.jpg" title="点击查看原始大小图片" class="magplus" width="700" height="525" alt="" style="border:0px; font-family:Helvetica,Tahoma,Arial,sans-serif; font-size:14px; line-height:25.1875px" /></p><p></p><p></p><p></p><p></p><br style="font-family:Helvetica,Tahoma,Arial,sans-serif; font-size:14px; line-height:25.1875px" /><span style="font-family:Helvetica,Tahoma,Arial,sans-serif; font-size:14px; line-height:25.1875px; color:red">2.具体代码</span><span style="font-family:Helvetica,Tahoma,Arial,sans-serif; font-size:14px; line-height:25.1875px"> </span><br style="font-family:Helvetica,Tahoma,Arial,sans-serif; font-size:14px; line-height:25.1875px" /><pre code_snippet_id="1587696" snippet_file_name="blog_20160225_2_2630797" name="code" class="java">import java.util.LinkedList;  import java.util.List;    /**  * 功能:把一个数组的值存入二叉树中,然后进行3种方式的遍历  *   * 参考资料0:数据结构(C语言版)严蔚敏  *   * 参考资料1:http://zhidao.baidu.com/question/81938912.html  *   * 参考资料2:http://cslibrary.stanford.edu/110/BinaryTrees.html#java  *   * @author ocaicai@yeah.net @date: 2011-5-17  *   */  public class BinTreeTraverse2 {        private int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };      private static List<Node> nodeList = null;        /**      * 内部类:节点      *       * @author ocaicai@yeah.net @date: 2011-5-17      *       */      private static class Node {          Node leftChild;          Node rightChild;          int data;            Node(int newData) {              leftChild = null;              rightChild = null;              data = newData;          }      }        public void createBinTree() {          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).leftChild = nodeList                      .get(parentIndex * 2 + 1);              // 右孩子              nodeList.get(parentIndex).rightChild = nodeList                      .get(parentIndex * 2 + 2);          }          // 最后一个父节点:因为最后一个父节点可能没有右孩子,所以单独拿出来处理          int lastParentIndex = array.length / 2 - 1;          // 左孩子          nodeList.get(lastParentIndex).leftChild = nodeList                  .get(lastParentIndex * 2 + 1);          // 右孩子,如果数组的长度为奇数才建立右孩子          if (array.length % 2 == 1) {              nodeList.get(lastParentIndex).rightChild = nodeList                      .get(lastParentIndex * 2 + 2);          }      }        /**      * 先序遍历      *       * 这三种不同的遍历结构都是一样的,只是先后顺序不一样而已      *       * @param node      *            遍历的节点      */      public static void preOrderTraverse(Node node) {          if (node == null)              return;          System.out.print(node.data + " ");          preOrderTraverse(node.leftChild);          preOrderTraverse(node.rightChild);      }        /**      * 中序遍历      *       * 这三种不同的遍历结构都是一样的,只是先后顺序不一样而已      *       * @param node      *            遍历的节点      */      public static void inOrderTraverse(Node node) {          if (node == null)              return;          inOrderTraverse(node.leftChild);          System.out.print(node.data + " ");          inOrderTraverse(node.rightChild);      }        /**      * 后序遍历      *       * 这三种不同的遍历结构都是一样的,只是先后顺序不一样而已      *       * @param node      *            遍历的节点      */      public static void postOrderTraverse(Node node) {          if (node == null)              return;          postOrderTraverse(node.leftChild);          postOrderTraverse(node.rightChild);          System.out.print(node.data + " ");      }        public static void main(String[] args) {          BinTreeTraverse2 binTree = new BinTreeTraverse2();          binTree.createBinTree();          // nodeList中第0个索引处的值即为根节点          Node root = nodeList.get(0);            System.out.println("先序遍历:");          preOrderTraverse(root);          System.out.println();            System.out.println("中序遍历:");          inOrderTraverse(root);          System.out.println();            System.out.println("后序遍历:");          postOrderTraverse(root);      }    }  

以先序遍历为例:


public static void preOrderTraverse(Node node) {          if (node == null)              return;          System.out.print(node.data + " ");          preOrderTraverse(node.leftChild);          preOrderTraverse(node.rightChild);      }  

先打印,再到递归左孩子,一直递归输出1248,8没有左孩子,所以return,结束此处递归,还在方法里面,并没有跳出,因为还要执行下一步,跳到递归递归右孩子(此处需要注意和8皇后问题进行区别,8皇后递归方法和后面的撤销标记的方法都在if条件里面,进行if判断时不符合if语句,则跳出if,所以后面的撤销没有执行到,在回溯到上一个数的时候才跳进来执行)。

8的右孩子没有,所以return,此时跳出方法。然后回溯到4,并跳到递归右孩子,发现有9,执行递归,9的左孩子没有,右孩子也没有,跳出方法。再回溯到2,并跳到递归右孩子,以此类推。



0 0
原创粉丝点击