10.2 二叉树查找

目录

概述

代码


概述

代码

public class BinaryTreeDemo {
    public static void main(String[] args) {
        //先需要创建一颗二叉树
        BinaryTree binaryTree = new BinaryTree();
        //创建需要的结点
        HeroPoint root = new HeroPoint(1, "宋江");
        HeroPoint node2 = new HeroPoint(2, "吴用");
        HeroPoint node3 = new HeroPoint(3, "卢俊义");
        HeroPoint node4 = new HeroPoint(4, "林冲");
        HeroPoint node5 = new HeroPoint(5, "关胜");

        //先手动创建该二叉树,后面递归的方式创建二叉树
        root.setLeft(node2);
        root.setRight(node3);
        node3.setRight(node4);
        node3.setLeft(node5);
        binaryTree.setRoot(root);

        System.out.println("前序遍历方式~~~");
        HeroPoint resNode = binaryTree.lefSearch(1);
        if (resNode != null) {
            System.out.printf("找到了,信息为 no=%d name=%s", resNode.getId(), resNode.getName());
        } else {
            System.out.println("没有找到");
        }
        
        //中序遍历查找
        System.out.println("中序遍历方式~~~");
        resNode = binaryTree.centerSearch(5);
        if (resNode != null) {
            System.out.printf("找到了,信息为 no=%d name=%s", resNode.getId(), resNode.getName());
        } else {
            System.out.println("没有找到");
        }

        //后序遍历查找
        System.out.println("后序遍历方式~~~");
        resNode = binaryTree.finalSearch(5);
        if (resNode != null) {
            System.out.printf("找到了,信息为 no=%d name=%s", resNode.getId(), resNode.getName());
        } else {
            System.out.println("没有找到");
        }
    }

}


class BinaryTree{
    private HeroPoint root;

    public void setRoot(HeroPoint root) {
        this.root = root;
    }

    public HeroPoint lefSearch(int no){
        if (this.root != null){
            return this.root.perSearch(no);
        }else {
            System.out.println("查找失败, 二叉树为空");
            return null;
        }
    }
    public HeroPoint centerSearch(int no){
        if (this.root != null){
            return this.root.centerSearch(no);
        }else {
            System.out.println("查找失败, 二叉树为空");
            return null;
        }
    }
    public HeroPoint finalSearch(int no){
        if (this.root != null){
            return this.root.finalSearch(no);
        }else {
            System.out.println("查找失败, 二叉树为空");
            return null;
        }
    }
}


class HeroPoint{

    private int id;
    private String name;

    private HeroPoint left;
    private HeroPoint right;

    public HeroPoint perSearch(int no){
        if ( no == this.id){
            return this;
        }

        HeroPoint temp = null;
        if (this.left != null){
            temp = this.left.perSearch(no);
        }
        if (temp != null){
            return temp;
        }
        if (this.right != null){
            temp = this.right.perSearch(no);
        }
        return temp;
    }

    public HeroPoint centerSearch(int no){
        HeroPoint temp = null;
        if (this.left != null){
            temp = this.left.perSearch(no);
        }
        if (temp != null){
            return temp;
        }
        if ( no == this.id){
            return this;
        }
        if (this.right != null){
            temp = this.right.perSearch(no);
        }
        return temp;
    }

    public HeroPoint finalSearch(int no){
        HeroPoint temp = null;
        if (this.left != null){
            temp = this.left.perSearch(no);
        }
        if (temp != null){
            return temp;
        }
        if (this.right != null){
            temp = this.right.perSearch(no);
        }
        if (temp != null){
            return temp;
        }
        if ( no == this.id){
            return this;
        }
        return null;
    }


    public HeroPoint(int id, String name) {
        this.id = id;
        this.name = name;
    }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值