关于二叉树的遍历
来源:互联网 发布:聊天软件默默 编辑:程序博客网 时间: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
- 关于二叉树的遍历
- 关于二叉树的遍历
- 关于二叉树的遍历
- 关于二叉树的遍历
- 关于二叉树的遍历
- 关于二叉树的遍历
- 关于二叉树遍历
- 关于二叉树遍历的算法
- 关于二叉树遍历的问题
- 关于数据结构中二叉树的遍历
- 关于二叉树的结构与遍历
- 关于二叉树的前序中序后序遍历
- 关于二叉树遍历的一些思考
- 关于二叉树的多种遍历算法
- 关于二叉树的非递归遍历
- 关于分层遍历二叉树
- 【二叉树】关于二叉树的后续遍历遍历以及栈
- 关于二叉树的非递归遍历的算法疑惑
- iOS学习之移除Main.storyboard
- JAVASE基础 Item -- IO流之字节流
- Android问题解决途径
- GCD 实现同步锁
- android 自定义选项卡tabhoust
- 关于二叉树的遍历
- Java网络编程精解笔记1:入门
- I/O复用——poll系统调用
- Java基础:Day10笔记内容 (多态概述、抽象类)
- sql server查询技巧
- excel工作量清单计算开始、结束时间,自动跳过节假日
- iOS之XCode在Interface Builder通过16进制选择选择颜色
- 二十岁出头的时候上,你一无所有,你拥有一切。
- jvm频繁GC问题查找记录