二叉树的遍历、查找、删除

一、二叉树简介

1、简介:

每个节点最多只有只能有两个子节点的一种称为二叉树
二叉树的子节点分为左节点和右节点。
满二叉树

如果该二叉树的所有叶子节点都在最后一层,并且节点总数=2^n-1,n为层数。

完全二叉树

如果该二叉树的所有叶子节点都在最后一层或者倒数第二层,而且最后一层的叶子节点在左边连续,倒数第二层的叶子节点在右边连续,称之为完全二叉树
在这里插入图片描述

二、二叉树遍历(三种)

1、思路:

前序遍历: 先输出父节点,再遍历左子树和右子树:
中序遍历: 先遍历左子树,再输出父节点,再遍历右子树
后序遍历: 先遍历左子树,再遍历右子树,最后输出父节点
总结: 看输出父节点的顺序,就确定是前序,中序还是后序

2、代码实现:

public class BinaryTreeDemo {
    public static void main(String[] args) {
        //先创建二叉树
        BinaryTree binaryTree = new BinaryTree();
        //创建需要的节点
        HeroNode root = new HeroNode(1, "lili");
        HeroNode node2 = new HeroNode(2, "tom");
        HeroNode node3 = new HeroNode(3, "kate");
        HeroNode node4 = new HeroNode(4, "jack");
        HeroNode node5 = new HeroNode(5, "susan");
        //先手动创建二叉树,后边再学习使用递归的方式创建二叉树
        root.setLeft(node2);
        root.setRight(node3);
        node3.setRight(node4);
        node3.setLeft(node5);
        binaryTree.setRoot(root);
        //前序遍历
        System.out.println("前序遍历");
        binaryTree.preOrder(); //1,2,3,5,4

        //中序遍历
        System.out.println("中序遍历");
        binaryTree.midOrder(); //2134    21534
        //后序遍历
        System.out.println("后序遍历");
        binaryTree.postOrder(); //2 5,4,3,1


       
    }
}

//定义BinaryTree 二叉树
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 midOrder() {
        if (this.root != null) {
            this.root.midOrder();
        } else {
            System.out.println("二叉树为空,无法遍历");
        }
    }

    //后序遍历
    public void postOrder() {
        if (this.root != null) {
            this.root.postOrder();
        } else {
            System.out.println("二叉树为空,无法遍历");
        }
    } 
}
//创建HeroNode节点

class HeroNode {
    private int no;
    private String name;
    private HeroNode left;
    private HeroNode right;

    //左右节点默认为空
    public HeroNode(int no, String name) {
        super();
        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 HeroNode getLeft() {
        return left;
    }

    public void setLeft(HeroNode left) {
        this.left = left;
    }
    public HeroNode getRight() {
        return right;
    }

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

    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", 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 midOrder() {
        //递归向左子树中序遍历
        if (this.left != null) {
            this.left.midOrder();
        }
        //输出父节点
        System.out.println(this);
        //递归向右子树中序遍历

        if (this.right != null) {
            this.right.midOrder();
        }
    }

    //编写后序遍历的方法
    public void postOrder() {

        //递归向左子树后序遍历
        if (this.left != null) {
            this.left.postOrder();
        }
        //递归向右子树后序遍历
        if (this.right != null) {
            this.right.postOrder();
        }
        //输出父节点
        System.out.println(this);

    }
}

三、查找指定节点:

1、思路:

前序查找:
1)先判断当前节点的 no 是否等于要查找的 no;
2)如果相等,则返回当前节点;
3)如果不想等,则判断当前节点的左子节点是否为空,如果不为空,则前序递归查找;
4)如果左递归前序查找,找到节点,则返回;否则继续判断当前节点的右子节点的是否为空,如果不为空,则继续向右递归前序查找。
中序后序的思路一样的~~

2、代码实现:



//定义BinaryTree 二叉树
class BinaryTree {
    private HeroNode root;

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

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

    //后序遍历查找
    public HeroNode postOrderSearch(int no) {
        if (this.root != null) {
            return this.root.postOrderSearch(no);
        } else {
            return null;
        }
    }


  
}
//创建HeroNode节点

class HeroNode {
    /**
     * 前序遍历查找
     * @param no 查找的编号
     * @return 返回节点
     */
    public HeroNode preOrderSearch(int no) {
        //父节点就是要查找的节点
        if (this.no == no) {
            return this;

        }
        HeroNode 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);
        }
        //不管找没找到都要返回
        return resNode;
    }

    //中序遍历查找
    public HeroNode midOrderSearch(int no) {

        HeroNode resNode = null;
        //左递归查找,先判断左子节点是否为空
        if (this.left != null) {
            resNode = this.left.midOrderSearch(no);
        }
        if (resNode != null) {//说明左子树找到了
            return resNode;
        }

        //父节点就是要查找的节点
        if (this.no == no) {
            return this;
        }

        //右递归查找
        if (this.right != null) {
            resNode = this.right.midOrderSearch(no);
        }
        //不管找没找到都要返回
        return resNode;
    }

    //后序遍历查找
    public HeroNode postOrderSearch(int no) {
        HeroNode resNode = null;
        //左递归查找,先判断左子节点是否为空
        if (this.left != null) {
            resNode = this.left.midOrderSearch(no);
        }
        if (resNode != null) {//说明左子树找到了
            return resNode;
        }


        //右递归查找
        if (this.right != null) {
            resNode = this.right.midOrderSearch(no);
        }

        if (resNode != null) {//说明右子树找到了
            return resNode;
        }

        //父节点就是要查找的节点
        if (this.no == no) {
            return this;

        }
        return resNode;

    }
}

四、删除指定节点:

1、思路:

规定:
如果删除的节点是叶子节点,则删除该节点;
如果删除的节点是非叶子节点,则删除该子树。
思路:
1) 如果树是空树,或者只有一个root节点(this.no==no),则直接将二叉树置空;
2)假定二叉树是单向的,所以我们是判断当前节点的子节点是否是需要删除的节点,而不能判断当前的这个节点;
3)如果当前节点的左子节点不为空,并且左子节点就是需要删除的节点,就将this.left=null,否则就返回,结束递归;
4)如果当前节点的右子节点不为空,并且右子节点就是需要删除的节点,就将this.right=null,否则就返回,结束递归;
5)如果第3、4 步没有删除节点,就需要向左子树递归删除;
6)如果第5步也没有删除节点,就需要向右子树递归删除。

2、代码实现:


//定义BinaryTree 二叉树
class BinaryTree {
    private HeroNode root;

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

    public void deleNode(int no) {
        if (this.root != null) {
            //如果只有一个root节点,先判断root节点是不是需要删除的节点
            if (this.root.getNo() == no) {
                this.root = null;
            }else {
                this.root.delNode(no);
            }
        }
    }
}
//创建HeroNode节点
class HeroNode {
    //递归删除节点
    public void delNode(int no) {
        //如果左子节点不为空,且左子节点就是要删除的节点,那么就直接删除。
        if (this.left != null && this.left.no == no) {
            this.left = null;
            return;
        }
        //如果右子节点不为空,且右子节点就是要删除的节点,那么就直接删除。

        if (this.right != null && this.right.no == no) {
            this.right = null;
            return;
        }
		//向左子树递归删除
        if (this.left != null) {
            this.left.delNode(no);
        }
//向右子树递归删除
        if (this.right != null) {
            this.right.delNode(no);
        }
    }

}

初学数据结构和算法,有不足的地方希望大家多多指出~~一起学习进步!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值