【java】【二叉树】【前序、中序、后序查找】

package com.study.tree;

/**
* 前序\中序\后序查找主要关注的还是顺序。
* 什么顺序先检查 根节点(node)视为具体 前 - 中 - 后序的查找算法得因由。
* 所以,只要有了 顺序 的思路,就不难理解。
* 唯一需要关注的是,前、中、后序的算法效率。
* 目前测试的结果是:后序最优。
* 但碍于目前理解尚浅,不懂的还很多,只好残念留之。
**/
public class BinaryTreeDemo1 {

	public static void main(String[] args) {
		BinaryTree1 binaryTree = new BinaryTree1();
		HeroNode1 root= new HeroNode1(1, "宋江");
		HeroNode1 node2 = new HeroNode1(2, "吴用");
		HeroNode1 node3 = new HeroNode1(3, "卢俊义");
		HeroNode1 node4 = new HeroNode1(4, "林冲");
		HeroNode1 node5 = new HeroNode1(5, "关胜");
		
		root.setLeft(node2);
		root.setRight(node3);
		node3.setLeft(node5);
		node3.setRight(node4);
		binaryTree.setRoot(root);
		
		System.out.println(binaryTree.preSearch(5));
		System.out.println( "前序搜索结束~~~~~~" );
		
		System.out.println( binaryTree.infixSearch(5) );
		System.out.println( "中序搜索结束~~~~~~" );
		
		System.out.println( binaryTree.postSearch(5) );
		System.out.println( "后序搜索结束~~~~~~" );
	}

}

class BinaryTree1{
	private HeroNode1 node;
	
	public void setRoot( HeroNode1 root ) {
		this.node = root;
	}
	
	public void preOrder() {
		if( node != null ) {
			this.node.preOrder(); 
		}else{
			System.out.println( "二叉树为空,无法遍历" );
		}
	}
	
	public void infixOrder() {
		if( node !=null ) {
			this.node.infixOrder();
		}else{
			System.out.println( "二叉树为空,无法遍历" );
		}
	}
	
	public void postOrder() {
		if( node !=null ) {
			this.node.postOrder();
		}else{
			System.out.println( "二叉树为空,无法遍历" );
		}
	}
	
	public HeroNode1 preSearch(int num) {
		return this.node.preSearch(num);
	}
	
	public HeroNode1 infixSearch( int num ) {
		return this.node.infixSearch(num);
	}
	
	public HeroNode1 postSearch( int num ) {
		return this.node.postSearch(num);
	}
	
	
}

class HeroNode1{
	private int num;
	private String name;
	private HeroNode1 left;
	private HeroNode1 right;
	
	public HeroNode1(int num, String name) {
		this.num = num;
		this.name = name;
	}
	
	public int getNum() {
		return num;
	}
	public void setNum(int num) {
		this.num = num;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public HeroNode1 getLeft() {
		return left;
	}
	public void setLeft(HeroNode1 left) {
		this.left = left;
	}
	public HeroNode1 getRight() {
		return right;
	}
	public void setRight(HeroNode1 right) {
		this.right = right;
	}
	@Override
	public String toString() {
		return "HeroNode1 [num=" + num + ", 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 );
	}
	
	/**
	 * 前序搜索
	 * @param num
	 * @return
	 */
	public HeroNode1 preSearch( int num ) {
		System.out.println( "前序搜索~~~~~~~~" );
		if(this.num == num) {
			return this;
		}
		HeroNode1 resNode = null;
		if( this.left !=null  ) {
			resNode = this.left.preSearch(num);
		}
		
		if(resNode !=null) {
			return resNode;
		}
		
		if( this.right !=null ) {
			resNode = this.right.preSearch(num);
		}
		return resNode;
	}
	
	/**
	 * 中序搜索
	 * @param num
	 * @return
	 */
	public HeroNode1 infixSearch( int num ) {
		
		HeroNode1 resultNode = null;
		if( this.left !=null ) {
			resultNode = this.left.infixSearch(num);
		}
		
		if( resultNode != null ) {
			return resultNode;
		}
		System.out.println( "中序搜索开始~~~~" );
		if( this.num == num ) {
			return this;
		}
		
		if( this.right != null ) {
			resultNode = this.right.infixSearch(num);
		}
		return resultNode;
	}
	
	/**
	 * 后序搜索
	 * @param num
	 * @return
	 */
	public HeroNode1 postSearch( int num ) {
		
		HeroNode1 resultNode = null;
		
		if( this.left != null ) {
			resultNode = this.left.postSearch(num);
		}
		
		if( resultNode != null ) {
			return resultNode;
		}
		
		if( this.right !=null ) {
			resultNode = this.right.postSearch(num);
		}
		
		if( resultNode != null ) {
			return resultNode;
		}
		System.out.println( "后序搜索开始~~~~" );
		if( this.num == num ) {
			return this;
		}
		return resultNode;
	}
	
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值