Java数据结构《二叉树》的手动生成、前序中序后序遍历和查找以及删除节点

package tree;

public class BinaryTreeDemo {
    public static void main(String[] args) {
        HeroNode root = new HeroNode(1,"宋江");
        HeroNode node1 = new HeroNode(2,"吴用");
        HeroNode node2 = new HeroNode(3,"卢俊义");
        HeroNode node3 = new HeroNode(4,"武松");
        HeroNode node4 = new HeroNode(5,"鲁智深");
        //手动生成树
        root.left = node1;
        root.right = node2;
        node2.left = node3;
        node2.right = node4;
        BinaryTree binaryTree = new BinaryTree();
        binaryTree.setRoot(root);
        System.out.println("前序遍历~~~~");
        binaryTree.preList();
//        System.out.println("中序遍历~~~~");
//        binaryTree.infixList();
//        System.out.println("后序遍历~~~~");
//        binaryTree.postList();
//        HeroNode res1 = binaryTree.preSearch(4);
//        if (res1 == null) {
//            System.out.println("前序没找到");
//        }else {
//            System.out.println("前序找到的结果为:" + res1);
//        }
//        HeroNode res2 = binaryTree.infixSearch(3);
//        if (res2 == null) {
//            System.out.println("中序序没找到");
//        }else {
//            System.out.println("中序找到的结果为:" + res2);
//        }
//        HeroNode res3 = binaryTree.postSearch(10);
//        if (res3 == null) {
//            System.out.println("后序没找到");
//        }else {
//            System.out.println("后序找到的结果为:" + res3);
//        }
        binaryTree.delete(10);
        binaryTree.preList();




    }
}
class BinaryTree{
    private HeroNode root;

    public void setRoot(HeroNode root) {
        this.root = root;
    }
    //前序遍历
    public void preList(){
        if (root == null) {
            System.out.println("该树为空");
            return;
        }
        root.preOrderList();
    }
    //中序遍历
    public void infixList(){
        if (root == null) {
            System.out.println("该树为空");
            return;
        }
        root.infixOrderList();
    }
    //后序遍历
    public void postList(){
        if (root == null) {
            System.out.println("该树为空");
            return;
        }
        root.postOrderList();
    }
    //前序查找
    public HeroNode preSearch(int no){
        if (root == null) {
            return null;
        }
        return root.preSearch(no);
    }
    //中序查找
    public HeroNode infixSearch(int no){
        if (root == null) {
            return null;
        }
        return root.infixSearch(no);
    }
    //后序查找
    public HeroNode postSearch(int no){
        if (root == null) {
            return null;
        }
        return root.postSearch(no);
    }
    //删除
    public void delete(int no){
        if (root == null) {
            System.out.println("该树为空,删除失败");
            return;
        }
        if (root.getNo() == no){
            root = null;
            System.out.println("删除成功");
        }else {
            root.delete(no);
        }
    }
}
class HeroNode{
    private int no;
    private String name;
    HeroNode left;
    HeroNode right;

    public HeroNode(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;
    }

    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                '}';
    }
    //前序遍历
    public void preOrderList(){
        System.out.println(this);
        if (this.left != null) {
            this.left.preOrderList();
        }
        if (this.right != null) {
            this.right.preOrderList();
        }
    }
    //中序遍历
    public void infixOrderList(){
        if (this.left != null) {
            this.left.infixOrderList();
        }
        System.out.println(this);
        if (this.right != null) {
            this.right.infixOrderList();
        }
    }
    //后序遍历
    public void postOrderList(){
        if (this.left != null) {
            this.left.postOrderList();
        }
        if (this.right != null) {
            this.right.postOrderList();
        }
        System.out.println(this);
    }
    //前序查找
    public HeroNode preSearch(int no){
        HeroNode resNode = null;
        if (this.no == no){
            return this;
        }
        if (this.left != null) {
            resNode = this.left.preSearch(no);
        }
        if (resNode != null) {
            return resNode;
        }
        if (this.right != null) {
            resNode = this.right.preSearch(no);
        }
        return resNode;
    }
    //中序查找
    public HeroNode infixSearch(int no){
        HeroNode resNode = null;
        if (this.left != null) {
            resNode = this.left.infixSearch(no);
        }
        if (resNode != null) {
            return resNode;
        }
        if (this.no == no){
            return this;
        }
        if (this.right != null) {
            resNode = this.right.infixSearch(no);
        }
        return resNode;
    }
    //后序查找
    public HeroNode postSearch(int no){
        HeroNode resNode = null;
        if (this.left != null) {
            resNode = this.left.postSearch(no);
        }
        if (resNode != null) {
            return resNode;
        }
        if (this.right != null) {
            resNode = this.right.postSearch(no);
        }
        if (resNode != null) {
            return resNode;
        }
        if (this.no == no){
            return this;
        }
        return null;
    }
    //删除
    public void delete(int no){
        if (this.left != null && this.left.no == no) {
            this.left = null;
            System.out.println("删除成功");
            return;
        }
        if (this.right != null && this.right.no == no){
            this.right = null;
            System.out.println("删除成功");
            return;
        }
        if (this.left != null) {
            this.left.delete(no);
        }
        if (this.right != null){
            this.right.delete(no);
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值