二叉树排序(前序、中序、后序)
来源:互联网 发布:淘宝上的台式机 编辑:程序博客网 时间:2024/06/04 23:21
可以先查看递归思想1递归思想2
package cn.com.tree;/** * 二叉树类 * */public class Tree {public String data;//数据public Tree leftChild;public Tree rightChild;public Tree(String data) {super();this.data = data;}public Tree( String data, Tree leftChild, Tree rightChild, boolean visited) {super();this.data = data;this.leftChild = leftChild;this.rightChild = rightChild;}}
package cn.com.tree;public class Test2 {private Tree tree = null;public Test2(){tree = new Tree("A"); //构建一个父节点}/** * 创建一个树 * A * B C * DE FG * @param tree */public void createTree(Tree tree){Tree tB = new Tree("B");Tree tC = new Tree("C");Tree tD = new Tree("D");Tree tE = new Tree("E");Tree tF = new Tree("F");Tree tG = new Tree("G");tree.leftChild = tB;tree.rightChild = tC;tree.leftChild.rightChild = tE;tree.leftChild.leftChild = tD;tree.rightChild.rightChild = tG;tree.rightChild.leftChild = tF;}/** * 获取访问的数据 */public void getData(Tree tree){System.out.print(tree.data+"-");}/** * 前序遍历 * @param tree */public void preVisit(Tree tree){if(tree==null){return;}getData(tree);preVisit(tree.leftChild);preVisit(tree.rightChild);}/** * 中序遍历 * @param tree */public void innnerVisit(Tree tree){if(tree==null){return;}innnerVisit(tree.leftChild);getData(tree);innnerVisit(tree.rightChild);}/** * 后序遍历 * @param tree */public void lastVisit(Tree tree){if(tree==null){return;}lastVisit(tree.leftChild);lastVisit(tree.rightChild);getData(tree);}/** * 测试 * @param args */public static void main(String[] args){Test2 test = new Test2();test.createTree(test.tree);System.out.println("先序遍历");test.preVisit(test.tree);System.out.println("中序遍历");test.innnerVisit(test.tree);System.out.println("后序遍历");test.lastVisit(test.tree);}}
测试结果:
先序遍历A-B-D-E-C-F-G-中序遍历D-B-E-A-F-C-G-后序遍历D-E-B-F-G-C-A-
阅读全文
0 0
- 二叉树排序(前序、中序、后序)
- 二叉树遍历(前序,中序,后序)
- 二叉树遍历方式(前、中、后序)
- 二叉树的遍历(前、中、后序)
- 构造排序二叉树并输出前序遍历、中序遍历、后序遍历
- java 实现排序二叉树的插入和前序,中序,后序遍历
- 二叉排序数(创建,插入,删除,查找及前序、中序、后序遍历)
- 二叉树中,什么是前序,中序,后序
- 二叉树·中序+前/后序遍历建树+前/中/后序遍历(链表)
- 前序创建二叉树+ 前序/中序/后序遍历二叉树
- 二叉树的前序、中序、后序遍历
- 二叉树的遍历(前序,中序,后序)
- 二叉树的遍历:前序、中序、后序
- 二叉树的前序、中序、后序遍历
- 二叉树遍历(前序,中序,后序)
- 二叉树遍历(前序,中序,后序)
- 二叉树的前序、中序、后序遍历
- 二叉树中的前序,中序,后序
- oracle 学习笔记
- ISCSI网络存储服务器
- A Secret
- ajax对json的使用
- CaoHaha's staff
- 二叉树排序(前序、中序、后序)
- tensorflow gpu 0.12.1 版本与python对应关系及下载地址
- 第二十天总结
- 多线程快速入门
- Subsequence Count
- (二)SSM中mybatis一对一查询
- 服务器开发之简单的TCP回射服务器(0):程序说明
- Palindrome Function
- instanceof的使用: 对象与构造函数在原型链上是否有关系