java版本_二叉树(前序遍历,中序遍历,后续遍历)
来源:互联网 发布:mac 打开finder快捷键 编辑:程序博客网 时间:2024/04/28 04:11
import java.util.LinkedList;import java.util.List;/** * 参考文章:http://ocaicai.iteye.com/blog/1047397 * 功能:把一个数组的值存入二叉树中,然后进行3种方式的遍历 * * 参考资料0:数据结构(C语言版)严蔚敏 * * 参考资料1:http://zhidao.baidu.com/question/81938912.html * * 参考资料2:http://cslibrary.stanford.edu/110/BinaryTrees.html#java * * * */public class BinTreeTraverse2 {private int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };private static List<Node> nodeList = null;/** * 内部类:节点* * */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);}}/** * 先序遍历 * * 这三种不同的遍历结构都是一样的,只是先后顺序不一样而已 * * 遍历的节点 */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);System.out.println();System.out.println("总结:上述三种方式不断的把根徘徊在左和右之间");}}还可以参考另外一篇文章:http://blog.csdn.net/wzy_1988/article/details/16943473
0 0
- java版本_二叉树(前序遍历,中序遍历,后续遍历)
- 二叉树遍历,已知前序遍历与中序遍历,求后续遍历 使用java(递归方式)实现
- c++ 二叉树的构建 前序遍历 中序遍历 后续遍历 层次遍历
- 二叉树--已知前序遍历和中序遍历,输出后续遍历
- 二叉树的基本操作,前序遍历,后续遍历,中序遍历
- 编程实现二叉树的建立,前序遍历,中序遍历和后续遍历
- leetcode | 二叉树的前序遍历、中序遍历、后续遍历的非递归实现
- 由前序遍历和中序遍历重建二叉树,再实现后续遍历
- python实现二叉树,前序遍历,中序遍历,后续遍历
- 二叉树遍历(Java)---前序遍历,中序遍历,后序遍历
- 根据前序遍历和后续遍历建立二叉树
- 二叉树的前序,中序及后续遍历
- 二叉树的前序、中序、后续遍历
- 二叉树的前序遍历、中序遍历、后续遍历 (包括递归、非递归,共六种)
- java实现二叉树的构建以及三种遍历方法(先序遍历,中序遍历,后续遍历)
- java操作二叉树:构建二叉树;前序、中序、后续、层次遍历
- UVA 11234 ( 二叉树 前序 后续遍历方法)
- C++编程练习(8)----“二叉树的建立以及二叉树的三种遍历方式“(前序遍历、中序遍历、后续遍历)
- 进程的入口函数、环境变量和当前目录
- 人月神话读书笔记
- iOS 各种开源接口API
- ceph存储 ceph集群loop使用示例
- WordXML格式解析
- java版本_二叉树(前序遍历,中序遍历,后续遍历)
- 网络连接中的心跳
- LR:接口性能测试中的应用服务器监控数据指标分析(Linx内存管理 和 性能学习)
- 一.关于Vmware的网络连接
- 常见Java面试题 – 第四部分:迭代(iteration)和递归(recursion)
- Sqoop的安装与测试
- 部署与管理ZooKeeper(转)
- Could not find a method XXX(View) in the activity class
- 网络基本功系列:细说路由(下)