二叉树删除节点以及前中后序遍历

本文介绍了一个简单的二叉树实现,包括节点的添加、删除及三种遍历方式:前序、中序和后序遍历。通过具体的Java代码示例展示了如何构造一个二叉树,并对特定节点进行删除操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public class BinaryTreeDemo {
    public static void main(String args[]){
        Employee emp1= new Employee(1,"李洛");
        Employee emp2= new Employee(2,"张元清");
        Employee emp3= new Employee(3,"牧尘");
        Employee emp4= new Employee(4,"萧炎");
        Employee emp5= new Employee(5,"许七安");
        BinaryTree binaryTree =new BinaryTree(emp1);
        emp1.setLeft(emp2);
        emp1.setRight(emp3);
        emp2.setLeft(emp4);
        emp2.setRight(emp5);
//        binaryTree.preOrder();//12453
//        binaryTree.delEmployee(emp1);
        binaryTree.delEmployee(emp5);
        binaryTree.preOrder();
//        binaryTree.infixOrder();//42513
//        binaryTree.lastOrder();//45231
    }
}

class BinaryTree{
    private Employee root;

    public BinaryTree(Employee root) {
        this.root = root;
    }

    public Employee getRoot() {
        return root;
    }

    public void setRoot(Employee 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 lastOrder(){
        if (this.root!=null){
            this.root.lastOrder();
        }else {
            System.out.println("八嘎!");
        }
    }
    //删除节点
    public void delEmployee(Employee emp){
        if(this.root!=null){
            if(root==emp){
                root=root.getLeft();
                return;
            }else {
                this.root.delEmployee(emp);
            }
        }else {
            System.out.println("二叉树为空,无法删除!");
        }
    }

}

class Employee{
    private int id;
    private String name;
    private Employee left;
    private Employee right;

    public Employee(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Employee getLeft() {
        return left;
    }

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

    public Employee getRight() {
        return right;
    }

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

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Employee employee = (Employee) o;
        return id == employee.id &&
                Objects.equals(name, employee.name) &&
                Objects.equals(left, employee.left) &&
                Objects.equals(right, employee.right);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, name, left, right);
    }

    @Override
    public String toString() {
        return "Employee{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
    //删除节点
    public void delEmployee(Employee emp){
        if(this.getLeft()==emp){
            if(this.getLeft().getLeft()==null&&this.getLeft().getRight()==null){
                this.setLeft(null);
            }else{
                if (this.getLeft().getLeft()!=null){
                    this.setLeft(this.getLeft().getLeft());
                }else{
                    this.setLeft(this.getLeft().getRight());
                }
            }
            return;
        }
        if(this.getRight()==emp){
            if(this.getRight().getLeft()==null&&this.getRight().getRight()==null){
                this.setRight(null);
            }else{
                if (this.getRight().getLeft()!=null){
                    this.setRight(this.getRight().getLeft());
                }else{
                    this.setRight(this.getLeft().getRight());
                }
            }
            return;
        }
        if(this.getLeft()!=null){
            this.getLeft().delEmployee(emp);
        }
        if(this.getRight()!=null){
            this.getRight().delEmployee(emp);
        }

    }
    //前序遍历
    public void preOrder(){
        System.out.println(this);
        if(this.getLeft()!=null){
            this.getLeft().preOrder();
        }
        if (this.getRight()!=null){
            this.getRight().preOrder();
        }


    }
    //中序遍历
    public void infixOrder(){
        if (this.left!=null){
            this.left.infixOrder();
        }
        System.out.println(this);
        if (this.right!=null){
            this.right.infixOrder();
        }
    }

    //后序遍历
    public void lastOrder(){
        if (this.left!=null){
            this.left.lastOrder();
        }
        if (this.right!=null){
            this.right.lastOrder();
        }
        System.out.println(this);
    }


}

共勉!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值