笔记 | Java 数据结构——二叉树查找节点(先序、中序、后序)

public class BinaryTreeDemo {
    public static void main(String[] args) {
        BinaryTree binaryTree=new BinaryTree();
        HerNode root=new HerNode(1,"宋江");
        HerNode node2=new HerNode(2,"吴用");
        HerNode node3=new HerNode(3,"张三");
        HerNode node4=new HerNode(4,"李四");
        HerNode node5=new HerNode(5,"王五");

        root.setLeft(node2);
        root.setRight(node3);
        node3.setRight(node4);
        node3.setLeft(node5);
        binaryTree.setRoot(root);

        /*
        System.out.println("前序遍历查找");
        HerNode resNode=binaryTree.preOrderSearch(5);
        if(resNode!=null){
            System.out.printf("找到了,信息为 no=%d name=%s",resNode.getNo(),resNode.getName());
        }else{
            System.out.printf("没有找到 no=%d 的英雄",5);
        }

         */

        /*
        System.out.println("中序遍历查找");
        HerNode resNode=binaryTree.infixOrderSearch(5);
        if(resNode!=null){
            System.out.printf("找到了,信息为 no=%d name=%s",resNode.getNo(),resNode.getName());
        }else{
            System.out.printf("没有找到 no=%d 的英雄",5);
        }

         */

        System.out.println("后序遍历查找");
        HerNode resNode = binaryTree.postOrderSearch(5);
        if(resNode!=null){
            System.out.printf("找到了,信息为 no=%d name=%s",resNode.getNo(),resNode.getName());
        }else{
            System.out.printf("没有找到 no=%d 的英雄",5);
        }
    }

}

class BinaryTree{
    private HerNode root;

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


    //前序遍历查找
    public HerNode preOrderSearch(int no){
        if(root!=null){
            return root.preOrderSearch(no);
        }
        else return null;
    }

    //中序遍历查找
    public HerNode infixOrderSearch(int no){
        if(root!=null){
            return root.infixOrderSearch(no);
        }
        else return null;
    }

    public HerNode postOrderSearch(int no){
        if(root!=null){
            return root.postOrderSearch(no);
        }
        else return null;
    }
}

class HerNode{
    private int no;
    private String name;
    private HerNode left;
    private  HerNode right;
    public HerNode(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 HerNode getLeft() {
        return left;
    }

    public void setLeft(HerNode left) {
        this.left = left;
    }

    public HerNode getRight() {
        return right;
    }

    public void setRight(HerNode right) {
        this.right = right;
    }

    @Override
    public String toString() {
        return "HerNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                '}';
    }


    //先序遍历查找
    public HerNode preOrderSearch(int no){
        //先看看根节点
        System.out.println("先看看前序遍历");
        if(this.no==no)
            return this;

        HerNode resNode=null;

        //再看看左子节点
        if(this.left!=null){
            resNode=this.left.preOrderSearch(no);
        }
        if(resNode!=null)
            return resNode;

        //最后看看右子节点
        if(this.right!=null){
            resNode=this.right.preOrderSearch(no);
        }

        //不管找没找到 都要返回resNode
            return resNode;
    }

    //中序遍历查找
    public HerNode infixOrderSearch(int no){
        HerNode resNode=null;
        if(this.left!=null){
            resNode=this.left.infixOrderSearch(no);
        }

        if(this.no==no){
            return this;
        }
        System.out.println("进入中序查找");
        if(resNode!=null){
            return resNode;
        }

        if(this.right!=null){
            resNode=this.right.infixOrderSearch(no);
        }

        return resNode;
    }

    public HerNode postOrderSearch(int no){
        HerNode resNode=null;
        if(this.left!=null){
            resNode=this.left.postOrderSearch(no);
        }
        if(this.no==no){
            return this;
        }


        if(resNode!=null){
            return resNode;
        }

        if(this.right!=null){
            resNode=this.right.postOrderSearch(no);
        }
        System.out.println("进入后序查找");
        return resNode;
    }
}


----参考:韩顺平数据结构

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值