public static int i = 1, j = 1, k =1;//编写前序查找方法public HeroNode preOrderSearch(int no){System.out.println("前序遍历"+(i++)+"次");if (this.no == no){return this;}HeroNode heroNode = null;if (this.left != null){heroNode = this.left.preOrderSearch(no);}//不等于空说明在左边找到了if (heroNode != null){return heroNode;}if (this.right != null){heroNode = this.right.preOrderSearch(no);}return heroNode;}//中序遍历查找public HeroNode infixOrderSearch(int no){HeroNode heroNode = null;//先判断当前节点的左子节点是否为空,不为空继续进行中序查找if (this.left != null){heroNode = this.left.infixOrderSearch(no);}if (heroNode != null){return heroNode;}System.out.println("中序遍历"+(j++)+"次");if (this.no == no){return this;}if (this.right != null){heroNode = this.right.infixOrderSearch(no);}return heroNode;}//后序遍历查找public HeroNode postOrderSearch(int no){HeroNode heroNode = null;//判断当前节点的左子节点是否为空,不为空,则递归后序遍历查找if (this.left != null){heroNode = this.left.postOrderSearch(no);}if (heroNode != null){return heroNode;}//判断当前节点的右子节点是否为空,不为空,则递归后序遍历查找if (this.right != null){heroNode = this.right.postOrderSearch(no);}if (heroNode != null){return heroNode;}System.out.println("后序遍历"+(k++)+"次");//左右子树都没有找到,比较当前节点是不是if (this.no == no){return this;}return heroNode;}
 //前序查找public HeroNode preOrederSearch(int no){if (root != null){return root.preOrderSearch(no);}else {return null;}}//中序查找public HeroNode infixOrderSeach(int no){if (root != null){return root.infixOrderSearch(no);}else {return null;}}//后序查找public HeroNode postOrderSeach(int no){if (root != null){return root.postOrderSearch(no);}else {return null;}}

完整代码

package tree;public class BinaryTreeDemo {public static void main(String[] args) {//先需要创建一颗二叉树BinaryTree binaryTree = new BinaryTree();//创建需要的节点HeroNode root = new HeroNode(1, "宋江");HeroNode node2 = new HeroNode(2, "吴用");HeroNode node3 = new HeroNode(3, "卢俊义");HeroNode node4 = new HeroNode(4, "林冲");HeroNode node5 = new HeroNode(5, "关胜");//说明,先手动创建该二叉树,后面学习递归方式创建二叉树binaryTree.setRoot(root);root.setLeft(node2);root.setRight(node3);node3.setRight(node4);node3.setLeft(node5);//测试
//        System.out.println("前序遍历");
//        binaryTree.preOrder();
//        System.out.println("中序遍历");
//        binaryTree.infixOrder();
//        System.out.println("后序遍历");
//        binaryTree.postOrder();//测试查找//前序遍历查找System.out.println("前序遍历查找:~~~~");HeroNode heroNode1 = binaryTree.preOrederSearch(5);if (heroNode1 != null){System.out.println("找到节点:" + heroNode1.toString());}else {System.out.println("没有找到");}//        //中序遍历查找System.out.println("中序遍历查找:~~~~");HeroNode heroNode2 = binaryTree.infixOrderSeach(5);if (heroNode2 != null){System.out.println("找到节点:" + heroNode2.toString());}else {System.out.println("没有找到");}//后序遍历查找System.out.println("后序遍历查找:~~~~");HeroNode heroNode3 = binaryTree.postOrderSeach(5);if (heroNode3 != null){System.out.println("找到节点:" + heroNode3.toString());}else {System.out.println("没有找到");}}
}class BinaryTree{private HeroNode root;public void setRoot(HeroNode root){this.root = root;}//前序遍历public void preOrder(){if (this.root != null){this.root.preOrder();}else {System.out.println("二叉树为空无法遍历");}}//中序遍历public void infixOrder(){if (this.root != null){this.root.infixOrder();}else {System.out.println("二叉树为空无法遍历");}}//后序遍历public void postOrder(){if (this.root != null){this.root.postOrder();}else {System.out.println("二叉树为空无法遍历");}}//前序查找public HeroNode preOrederSearch(int no){if (root != null){return root.preOrderSearch(no);}else {return null;}}//中序查找public HeroNode infixOrderSeach(int no){if (root != null){return root.infixOrderSearch(no);}else {return null;}}//后序查找public HeroNode postOrderSeach(int no){if (root != null){return root.postOrderSearch(no);}else {return null;}}}
class HeroNode{private int no;private String name;private HeroNode left;//默认nullprivate HeroNode right;//默认null;public HeroNode(int no, String name) {this.no = no;this.name = name;}public int getNo() {return no;}public void setNo(int no) {this.no = no;}public String getName() {return name;}public void setName(String name) {this.name = name;}public HeroNode getLeft() {return left;}public void setLeft(HeroNode left) {this.left = left;}public HeroNode getRight() {return right;}public void setRight(HeroNode right) {this.right = right;}@Overridepublic String toString() {return "HeroNode{" +"no=" + no +", name='" + name + '\'' +'}';}//编写前序遍历方法public void preOrder(){System.out.println(this);//先输出父节点//递归向左子树前序遍历if (this.left != null){this.left.preOrder();}//递归向右子树前序遍历if (this.right != null){this.right.preOrder();}}//编写中序遍历方法public void infixOrder(){//递归向左子树前序遍历if (this.left != null){this.left.infixOrder();}System.out.println(this);//输出父节点//递归向右子树前序遍历if (this.right != null){this.right.infixOrder();}}//编写后序遍历方法public void postOrder(){if (this.left != null){this.left.postOrder();}if (this.right != null){this.right.postOrder();}System.out.println(this);}public static int i = 1, j = 1, k =1;//编写前序查找方法public HeroNode preOrderSearch(int no){System.out.println("前序遍历"+(i++)+"次");if (this.no == no){return this;}HeroNode heroNode = null;if (this.left != null){heroNode = this.left.preOrderSearch(no);}//不等于空说明在左边找到了if (heroNode != null){return heroNode;}if (this.right != null){heroNode = this.right.preOrderSearch(no);}return heroNode;}//中序遍历查找public HeroNode infixOrderSearch(int no){HeroNode heroNode = null;//先判断当前节点的左子节点是否为空,不为空继续进行中序查找if (this.left != null){heroNode = this.left.infixOrderSearch(no);}if (heroNode != null){return heroNode;}System.out.println("中序遍历"+(j++)+"次");if (this.no == no){return this;}if (this.right != null){heroNode = this.right.infixOrderSearch(no);}return heroNode;}//后序遍历查找public HeroNode postOrderSearch(int no){HeroNode heroNode = null;//判断当前节点的左子节点是否为空,不为空,则递归后序遍历查找if (this.left != null){heroNode = this.left.postOrderSearch(no);}if (heroNode != null){return heroNode;}//判断当前节点的右子节点是否为空,不为空,则递归后序遍历查找if (this.right != null){heroNode = this.right.postOrderSearch(no);}if (heroNode != null){return heroNode;}System.out.println("后序遍历"+(k++)+"次");//左右子树都没有找到,比较当前节点是不是if (this.no == no){return this;}return heroNode;}
}

后序遍历查找最快

数据结构 - 二叉树(前序中序后序查找)相关推荐

  1. 二叉树前序遍历,后序遍历

    求前序遍历 Description 给出一棵二叉树的中序和后序遍历,求它的前序遍历.(树结点用不同的大写字母表示,长度小于等于26.) Input 本问题有多组测试数据,每组测试数据有两行,每行都是由 ...

  2. 二叉树前序遍历与后序遍历

    二叉树 前序遍历 递归 借助栈进行排序 先将根节点压栈 栈不为空,如果存在根节点,先右后左. 弹栈打印.直至栈为空 package com.vitamin.tree;import java.util. ...

  3. 【二叉树Java】二叉树遍历前序中序后序遍历的非递归写法

    本文主要介绍二叉树前序中序后序遍历的非递归写法 在探讨如何写出二叉树的前序中序后序遍历代码之前,我们先来明确一个问题,前序中序后序遍历根据什么区分? 二叉树的前序中序后序遍历,是相较根节点说的.最先遍 ...

  4. 二叉树中前序 中序 后序的互推

    最近开始复习数据结构,就从二叉树开始吧 1.复习前序 中序 后序 结构: 前序:根(左子树)(右子树) 中序:(左子树)根 (右子树) 后序:(左子树)(右子树)根 2. 前序+中序->后序 由 ...

  5. 用前序中序创建二叉树(用中序后序创建二叉树)

    定义二叉树结点 比如就拿这个二叉树 前序中序创建 因为前序遍历的顺序是 根 , 左 ,右. 中序的遍历是 左 根 右. 我们会很不好想,但我们可以用前序和中序把上面那个二叉树的遍历一边 前序遍历:AB ...

  6. 二叉树前序中序,后序中序,公共最近祖先的实现

    二叉树前序中序,后序中序,公共最近祖先的实现 注释中详细介绍了算法,故不再赘述. 无论是前序还是后序,一个节点的左子树和右子树都是可以看做是分开的,有一定规律可循,故可用递归进行实现. #includ ...

  7. 二叉树的深度(前序 中序 后序 递归非递归搜素)、广度、搜索 C++

    a b c 使用 1 2 3 表示 /* 描述:二叉树的深度(前序 中序 后序 递归非递归搜素).广度.搜索 作者:jz 日期:20140819 */ #include<stdio.h> ...

  8. 前序中序、中序后序以及前序后序构造二叉树

    文章目录 前序中序 中序后序 前序后序 定义的树节点如下, class TreeNode {int val;TreeNode left;TreeNode right;TreeNode(int x) { ...

  9. 序列化和反序列化二叉树 -----前序,中序,后序,层序

    目录 一.序列化和反序列化 1.什么是序列化和反序列化 二.前序遍历 1.序列化 1.问题分析 2.代码实现 2.反序列化 1.问题分析 2.代码实现 三.后序遍历 1.序列化 1.思路分析 2.代码 ...

最新文章

  1. python使用教程pandas-Python之Pandas使用教程
  2. java平台设计zhe_基于java平台的网上评教系统的设计与实现
  3. Qt5:Qt中图片的翻转,旋转,缩放,扭曲操作
  4. 用css3和jQuery制作精美的表单
  5. 总结Android开发中必备的代码Review清单
  6. curviloft插件怎么用_完结篇——你想要的逆天插件系列这里都有
  7. 【云图】如何制作全国×××查询系统?
  8. 【Java每日一题】20161228
  9. 计算机考研英语一和英语二的区别,2018考研英语一与英语二翻译有什么区别?怎么提高?...
  10. 前端笔记—第4篇CSS基础知识2
  11. 逻辑代码自动生成相关技术概述
  12. gg修改器怎么能让服务器检测不到,gg修改器怎么绕过检测 | 手游网游页游攻略大全...
  13. bat文件打开一闪就没了_电脑上想要删除的顽固文件一直删除不了,一条命令帮你解决问题...
  14. java ssh 404,SSH框架上的404异常
  15. 【北京-亚运村】这7家公司推荐给你
  16. Mac 自动代理切换
  17. 爱思服务器可以下小组件,iOS14 小组件添加/删除方法教程
  18. html加入3d模型的,3dmax怎么给模型添加材质
  19. 戏剧专业毕业论文题目
  20. css 所有选择器 实例与总结

热门文章

  1. jenkins Auth fail验证失败
  2. 记一次 IIS 7.0 身份验证相关的问题解决
  3. 使用Nant构建入门
  4. SQL Server 兼容模式
  5. Java学习笔记13-1——SpringMVC
  6. 判断一个数是偶数还是奇数
  7. web前端表格css三个t的使用(thead,tbody,tfoot)
  8. nginx 上传 文件超时设置_Nginx在高并发下的性能优化点!有这篇就够了!
  9. python爬虫知乎图片_Python爬虫入门教程 25-100 知乎文章图片爬取器之一
  10. 详细解析Linux /etc/passwd文件