二叉树

 public  static  void main(String[] args)
    {
        //先需要创建一颗二叉树
        BinaryTree binaryTree=new BinaryTree();
        //创建需要的节点
        HeroNode root=new HeroNode(1,"宋江");
       HeroNode node2=new HeroNode(2,"mao");
        HeroNode node3=new HeroNode(3,"gou");
        HeroNode node4=new HeroNode(4,"zhu");
       HeroNode node5=new HeroNode(5,"ma");
        //先手动创建
        root.setLeft(node2);
        root.setRight(node3);
        node3.setRight(node4);
        node3.setLeft(node5);
        binaryTree.setRoot(root);
        //测试
      /*  //前序
        System.out.println("前序");
        binaryTree.preOrder();
        //中序
        System.out.println("中序");
        binaryTree.infixOrder();
        //前序
        System.out.println("后序");
        binaryTree.postOrder();
*/
       //前序遍历
        System.out.println("前序遍历方式");
        HeroNode resNode=binaryTree.postSearch(5);
        if(resNode!=null)
            System.out.printf("找到了,信息为no=%d name=%s",resNode.getVal(),resNode.getName());
        else
        {
            System.out.printf("没有找到 no= %d 的英雄",2);
        }
    }

}
//定义二叉树
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 preSearch(int val)
    {
        if(this.root!=null)
         return    this.root.preOrdersearch(val);
        else System.out.println("无法遍历");
        return null;
    }
    public HeroNode infixSearch(int val)
    {
        if(this.root!=null)
        return     this.root.infixOrdersearch(val);
        else System.out.println("无法遍历");
        return null;
    }
    public HeroNode postSearch(int val)
    {
        if(this.root!=null)
         return    this.root.postOrdersearch(val);
        else System.out.println("无法遍历");
        return null;
    }

}
//先创建HeroNode
class HeroNode
{
    private int val;
    private String name;
    private HeroNode left;
    private HeroNode Right;

    public HeroNode(int val, String name) {
        this.val = val;
        this.name = name;
    }

    public int getVal() {
        return val;
    }

    public void setVal(int val) {
        this.val = val;
    }

    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) {
        Right = right;
    }

    @Override
    public String toString() {
        return "HeroNode{" +
                "val=" + val +
                ", 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);
    }
    //前序遍历查找 找到 返回该node  没找到 返回null
    public HeroNode preOrdersearch(int no)
    {
     //比较当前节点是不是
        if(this.val==no) return this;
        HeroNode resNode=null;
        if(this.left!=null)
        {
          resNode=  this.left.preOrdersearch(no);
        }
        if(resNode!=null)
        {
            System.out.println("找到了");
            //说明找到
            return  resNode;
        }
        if(this.Right!=null)
        {
         resNode=   this.Right.preOrdersearch(no);
        }
        return resNode;
    }
    //中序遍历查找 找到 返回该node  没找到 返回null
    public HeroNode infixOrdersearch(int no)
    {

        HeroNode resNode=null;
        if(this.left!=null)
        {
          resNode=  this.left.infixOrdersearch(no);
        }
        if(resNode!=null)
        {
            //说明找到
            return  resNode;
        }
        System.out.println("进入中序查找");
        //比较当前节点是不是
        if(this.val==no) return this;

        if(this.Right!=null)
        {
          resNode=  this.Right.infixOrdersearch(no);
        }
        return resNode;
    }
    //后序遍历查找 找到 返回该node  没找到 返回null
    public HeroNode postOrdersearch(int no)
    {
        HeroNode resNode=null;
        if(this.left!=null)
        {
         resNode=   this.left.postOrdersearch(no);
        }
        if(resNode!=null)
        {
            //说明找到
            return  resNode;
        }


        if(this.Right!=null)
        {
         resNode=   this.Right.postOrdersearch(no);
        }
        if(resNode!=null)
        {
            //说明找到
            return  resNode;
        }
        System.out.println("后续遍历");
        //比较当前节点是不是
        if(this.val==no) return this;
        return resNode;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值