普通二叉树遍历、查找、删除节点

手动创建一个二叉树 如图

在这里插入图片描述

代码
public class Main {
    public static void main(String[] args) {
        //手动创建树
        HeroNode heroNode1 = new HeroNode(1,"宋江");
        HeroNode heroNode2 = new HeroNode(2,"吴用");
        HeroNode heroNode3 = new HeroNode(3,"卢俊义");
        HeroNode heroNode4 = new HeroNode(4,"关胜");
        HeroNode heroNode5 = new HeroNode(5,"林冲");

        BinaryTree binaryTree = new BinaryTree();
        binaryTree.root = heroNode1;
        heroNode1.left = heroNode2;
        heroNode1.right = heroNode3;
        heroNode3.left = heroNode4;
        heroNode3.right = heroNode5;

        System.out.println("------前序 中序 后序遍历-------");
        //前序 中序 后序遍历
        binaryTree.preOrder();
        binaryTree.infixOrder();
        binaryTree.postOrder();

        System.out.println("------------------------------");

        //查找
        System.out.println("------查找------");
        System.out.println(binaryTree.preOrderSearch(2));
        System.out.println(binaryTree.infixOrderSearch(9));
        System.out.println("------------------------------");

        //删除
        System.out.println("------删除------");
        binaryTree.deleteNode(3);
        binaryTree.preOrder();
    }



}
//建立二叉树
class BinaryTree{
    HeroNode root;

    //删除节点
    public void deleteNode(int id){
        if (root == null){
            System.out.println("根节点为null  无法删除");
        }else {
            if (root.id == id){
                root = null;
            }else {
                root.deleteNode(id);
            }
        }
    }
    //前序、中序、后序遍历
    public void preOrder(){
        if (root == null){
            System.out.println("二叉树为空 无法遍历");
            return;
        }

        root.preOrder();
    }

    public void infixOrder(){
        if (root == null){
            System.out.println("二叉树为空 无法遍历");
            return;
        }

        root.infixOrder();
    }

    public void postOrder(){
        if (root == null){
            System.out.println("二叉树为空 无法遍历");
            return;
        }

        root.postOrder();
    }

    //前序查找、中序查找。后序查找
    public HeroNode preOrderSearch(int id){
        if (root == null){
            System.out.println("二叉树为空 无法遍历");
            return null;
        }else {
            return root.preOrderSearch(id);
        }
    }
    public HeroNode infixOrderSearch(int id){
        if (root == null){
            System.out.println("二叉树为空 无法遍历");
            return null;
        }else {
            return root.infixOrderSearch(id);
        }
    }
    public HeroNode postOrderSearch(int id){
        if (root == null){
            System.out.println("二叉树为空 无法遍历");
            return null;
        }else {
            return root.postOrderSearch(id);
        }
    }

}
//树的每个节点
class HeroNode{
    int id;
    String name;
    HeroNode left;
    HeroNode right;

    public HeroNode(int id, String name) {
        this.id = id;
        this.name = name;
    }
    //删除某个节点  如果删除的节点不是叶子节点,则直接将以该节点为根的子树删除
    public void deleteNode(int id){
        if (this.left != null && this.left.id == id){
            this.left = null;
            return;
        }
        if (this.right != null && this.right.id == id){
            this.right = null;
            return;
        }
        //如果左子节点没有删除 则再递归向左
        if (this.left != null){
            this.left.deleteNode(id);
        }

        //如果向左递归不行,就向右递归
        if (this.right != null){
            this.right.deleteNode(id);
        }
    }
    //前序遍历
    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);
    }

    //前序遍历查找
    public HeroNode preOrderSearch(int id){
        if (this.id == id){
            return this;
        }

        HeroNode heroNode = null;
        //向左边找
        if (this.left != null){
            heroNode = this.left.preOrderSearch(id);
        }

        if (heroNode != null){
            //说明找到了
            return heroNode;
        }

        //向右边找
        if (this.right != null){
            heroNode = this.right.preOrderSearch(id);
        }

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

    //中序遍历查找
    public HeroNode infixOrderSearch(int id){
        HeroNode heroNode = null;
        if (this.left != null){
            heroNode = this.left.infixOrderSearch(id);
        }

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

        if (this.id == id){
            return this;
        }

        if (this.right != null){
            heroNode = this.right.infixOrderSearch(id);
        }

        return heroNode;
    }

    //后序遍历查找
    public HeroNode postOrderSearch(int id){
        HeroNode heroNode = null;

        if (this.left != null){
            heroNode = this.left.postOrderSearch(id);
        }

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

        if (this.right != null){
            heroNode = this.right.postOrderSearch(id);
        }

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

        if (this.id == id){
            return this;
        }
        return null;
    }

    @Override
    public String toString() {
        return "HeroNode{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}
结果
------前序 中序 后序遍历-------
HeroNode{id=1, name='宋江'}
HeroNode{id=2, name='吴用'}
HeroNode{id=3, name='卢俊义'}
HeroNode{id=4, name='关胜'}
HeroNode{id=5, name='林冲'}
HeroNode{id=2, name='吴用'}
HeroNode{id=1, name='宋江'}
HeroNode{id=4, name='关胜'}
HeroNode{id=3, name='卢俊义'}
HeroNode{id=5, name='林冲'}
HeroNode{id=2, name='吴用'}
HeroNode{id=4, name='关胜'}
HeroNode{id=5, name='林冲'}
HeroNode{id=3, name='卢俊义'}
HeroNode{id=1, name='宋江'}
------------------------------
------查找------
HeroNode{id=2, name='吴用'}
null
------------------------------
------删除------
HeroNode{id=1, name='宋江'}
HeroNode{id=2, name='吴用'}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值