二叉树(遍历,查找,删除)

基本概念

1.结点,根结点,父结点,子结点,兄弟结点,叶子结点;

结点的度:结点的子结点个数;

结点层次:从根开始定义起,根为第一层,下面为第二层,以此类推;

树的深度:树中结点的最大层次。

2.满二叉树:除了叶子结点,每个结点度都为2。

3.完全二叉树:除去最后一层结点,二叉树为满二叉树;最后一层结点从左至右依次分布。

遍历

  • 前序遍历:root 开始,输出,向左递归->向右递归
  • 中序遍历:root开始,向左递归->输出->向右递归
  • 后续遍历:root开始,向左递归->向右递归->输出

 

//前序遍历
    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);
    }

查找

返回的节点为null说明没找到

//前序查找
    public HeroNode preSearch(int no){
        if (this.no==no)
            return this;
        HeroNode resNode=null;
        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 resNode;
    }

删除结点

若待删除的是叶子节点,直接删除;若待删除的是子树的根节点,将子树删除
//删除节点(注意,删除节点后依然会遍历完其他节点;若想删除后即结束,应设返回值)
    //若待删除的是叶子节点,直接删除;若待删除的是子树的根节点,将子树删除
    public void delete(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.delete(no);
        }
        if (this.right != null){
            this.right.delete(no);
        }
    }

完整代码

public class BinaryTree {
    public static void main(String[] args) {
        BinaryTree1 binaryTree = new BinaryTree1();
        HeroNode root=new HeroNode(1,"宋江");
        HeroNode node2=new HeroNode(2,"吴用");
        HeroNode node3=new HeroNode(3,"卢俊义");
        HeroNode node4=new HeroNode(4,"林冲");
        HeroNode node5=new HeroNode(5,"关胜");
        HeroNode node6=new HeroNode(6,"关胜");
        root.setLeft(node2);
        root.setRight(node3);
        node3.setRight(node4);
        node3.setLeft(node5);
        node2.setLeft(node6);
        binaryTree.srtRoot(root);
        binaryTree.preOrder();
        binaryTree.delete(3);
        System.out.println("***************");
        binaryTree.preOrder();
    }
}
class BinaryTree1{
    private HeroNode root;
    public void srtRoot(HeroNode root){
        this.root=root;
    }
    //前序遍历
    public void preOrder(){
        if (this.root!=null)
            this.root.preOrder();
        else System.out.println("tree is empty");
    }
//    中序遍历
    public void infixOrder(){
        if (this.root!=null)
            this.root.infixOrder();
        else System.out.println("tree is empty");
    }
    //后序遍历
    public void postOrder(){
        if (this.root!=null)
            this.root.postOrder();
        else System.out.println("tree is empty");
    }
    //前序查找
    public void preSearch(int no){
        if (this.root!=null) {
            HeroNode heroNode = this.root.preSearch(no);
            if (heroNode==null)
                System.out.println("cant find");
            else
                System.out.println(heroNode);
        } else System.out.println("tree is empty");
    }
    //中序查找
    public void infixSearch(int no){
        if (this.root!=null) {
            HeroNode heroNode = this.root.infixSearch(no);
            if (heroNode==null)
                System.out.println("cant find");
            else
                System.out.println(heroNode);
        } else System.out.println("tree is empty");
    }
    //后序查找
    public void postSearch(int no){
        if (this.root!=null) {
            HeroNode heroNode = this.root.postSearch(no);
            if (heroNode==null)
                System.out.println("cant find");
            else
                System.out.println(heroNode);
        } else System.out.println("tree is empty");
    }
    //删除结点
    public void delete(int no){
        if (root != null){
            if (root.getNo() == no)
                root =null;
            else
                root.delete(no);
        }
        else
            System.out.println("tree is empty");
    }

}
class HeroNode{
    private  int no;
    private String name;
    private HeroNode left;
    private HeroNode right;
    public HeroNode(int no,String name){
        this.no=no;
        this.name=name;
    }

    public int getNo() {
        return no;
    }

    public String getName() {
        return name;
    }

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

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

    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 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 preSearch(int no){
        if (this.no==no)
            return this;
        HeroNode resNode=null;
        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 resNode;
    }
    //删除节点(注意,删除节点后依然会遍历完其他节点;若想删除后即结束,应设返回值)
    //若待删除的是叶子节点,直接删除;若待删除的是子树的根节点,将子树删除
    public void delete(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.delete(no);
        }
        if (this.right != null){
            this.right.delete(no);
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值