【数据结构】二叉树前序,中序,后序遍历
2017-07-22 00:00
183 查看
package com.tree; import java.util.Scanner; class Tree { static Tree tre = new Tree(); Tree left = null; Tree right = null; int data; public Tree() { } //前序遍历创建二叉树,遇到空树写入0 public void createTree(Tree node) { Scanner scan = new Scanner(System.in); int tr = scan.nextInt(); node.data = tr; if (tr != 0) { node.left = new Tree(); node.right = new Tree(); createTree(node.left); createTree(node.right); } } // 先序遍历 public void pre(Tree tree) { if (tree.data != 0) { System.out.print(tree.data); } if (tree.left != null) { pre(tree.left); } if (tree.right != null) { pre(tree.right); } } // 中序遍历 public void med(Tree tree) { if (tree.left != null) { pre(tree.left); } if (tree.data != 0) { System.out.print(tree.data); } if (tree.right != null) { pre(tree.right); } } //后序遍历 public void next(Tree tree) { if (tree.left != null) { pre(tree.left); } if (tree.data != 0) { System.out.print(tree.data); } if (tree.right != null) { pre(tree.right); } } } public class erchashu { public static void main(String[] args) { // TODO Auto-generated method stub Tree tree = new Tree(); tree.createTree(Tree.tre); tree.next(Tree.tre); } }
相关文章推荐
- 数据结构(六)——二叉树 前序、中序、后序、层次遍历及非递归实现 查找、统计个数、比较、求深度的递归实现
- 数据结构:二叉树的前序,中序,后序遍历(递归和非递归)
- 二叉树的先序、中序、后序遍历方法(递归与非递归方法)——《数据结构》
- 数据结构_输出二叉树中先序、中序、后序遍历方式中第k个节点的数据
- 数据结构(六)——二叉树 前序、中序、后序、层次遍历及非递归实现 查找、统计个数、比较、求深度的递归实现
- 【数据结构】二叉树前序、中序、后序遍历相互求法
- 数据结构--java实现二叉树的先序、中序、后序、层次遍历及根据先序中序建立二叉树
- 数据结构-二叉树(递归前序、中序、后序遍历;栈实现中序变量;二叉树镜像)
- 数据结构---二叉树的前序、中序、后序遍历的递归和非递归 实现(C++)
- 数据结构二叉树的实现,前序、中序、后序遍历
- 【数据结构】二叉树结点插入和前序、中序、后序遍历的递归实现
- 数据结构 二叉树的先序 中序 后序遍历(linux 下c语言实现)
- 数据结构 ——二叉树 前序、中序、后序、层次遍历及非递归实现 查找、统计个数、比较、求深度的递归实现
- [算法与数据结构] - No.7 二叉树建立及其前序、中序、后序遍历
- 数据结构-二叉树的前序、中序、后序、层次遍历
- 二叉树知识点、先序、中序、后序遍历(数据结构)
- 数据结构基础 后序遍历和中序遍历还原二叉树
- 数据结构_二叉树的先序建立与先序,中序,后序(递归)遍历方式_C语言源代码
- 数据结构-二叉树的前序、中序、后序遍历的递归和非递归实现
- 数据结构二叉树的实现,前序、中序、后序遍历