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;
    }
}
你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧
文章题目:10.2二叉树查找-创新互联
新闻来源:http://www.scyingshan.cn/article/jdsih.html

 建站
建站
 咨询
咨询 售后
售后
 建站咨询
建站咨询 
 