数据结构与算法-二叉树(前序中序后序)Java实现


欢迎访问我的个人博客(点击进入)

1.概念

在计算机科学中,二叉树是每个结点最多有两个子树的树结构。通常子树被称作“左子树”(left subtree)和“右子树”(right subtree)。二叉树常被用于实现二叉查找树和二叉堆。

在这里插入图片描述

2.二叉树的遍历方式

遍历是对树的一种最基本的运算,所谓遍历二叉树,就是按一定的规则和顺序走遍二叉树的所有结点,使每一个结点都被访问一次,而且只被访问一次。由于二叉树是非线性结构,因此,树的遍历实质上是将二叉树的各个结点转换成为一个线性序列来表示。

2.1前序遍历

  1. 先输出当前结点
  2. 如果左子节点不为空,则继续递归进行前序遍历
  3. 如果右字结点不为空,则继续递归进行前序遍历

2.2中序遍历

  1. 当前结点左子节点不为空,继续遍历
  2. 输出当前结点
  3. 当前结点右子节点不为空,继续遍历

2.3后序遍历

  1. 当前结点的左子节点不为空,继续递归遍历
  2. 带前结点的右子节点不为空,继续递归遍历
  3. 输出当前结点

3.二叉树的搜索

二叉树的搜索依赖于二叉树的遍历,分为前序中序后序搜索,只是在遍历时输出的位置变为对结点是否为被找结点的判断,如果判断正确则返回结点,如果没找到,则返回空

4.代码实现

/**
 * 定义一个BinaryTree 二叉树
 */
class BinaryTree{
    private Node root;

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

    /**
     * 删除结点
     * @param no
     */
    public void deleteNode(int no){
        //判断根节点是否为空
        if(root != null){
            //如果根节点就是需要删除的结点,直接将根节点置空
            if(root.getNo() == no){
                root = null;
            }else {
                //递归删除
                root.deleteNode(no);
            }
        }else {
            System.out.println("空树");
        }
    }

    /**
     * 前序遍历
     */
    public void preOrder(){
        if(this.root != null){
            this.root.preOrder();
        }else {
            System.out.println("二叉树为空");
        }
    }

    /**
     * 前序遍历查找
     * @param no
     * @return
     */
    public Node preOrderSearch(int no){
        if(root != null){
            return root.preOrderSearch(no);
        }else {
            return null;
        }
    }

    /**
     * 中序遍历
     */
    public void infixOrder(){
        if(this.root != null){
            this.root.infixOrder();
        }else {
            System.out.println("二叉树为空");
        }
    }

    /**
     * 中序遍历查找
     * @param no
     * @return
     */
    public Node infixOrderSearch(int no){
        if(root != null){
            return root.infixOrderSearch(no);
        }else {
            return null;
        }
    }

    /**
     * 后序遍历
     */
    public void postOrder(){
        if(this.root != null){
            this.root.postOrder();
        }else {
            System.out.println("二叉树为空");
        }
    }

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

}


/**
 * 先创建结点
 */
class Node{
    private int no;
    private int data;
    private Node left;
    private Node right;

    public Node(int no, int data) {
        this.no = no;
        this.data = data;
    }

    public int getNo() {
        return no;
    }

    public int getData() {
        return data;
    }

    public Node getLeft() {
        return left;
    }

    public Node getRight() {
        return right;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public void setData(int data) {
        this.data = data;
    }

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

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

    @Override
    public String toString() {
        return "com.lysong.Node{" +
                "no=" + no +
                ", data=" + data +
                '}';
    }


    /**
     * 递归删除结点
     * @param no
     */
    public void deleteNode(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.deleteNode(no);
        }
        if(this.right != null){
            this.right.deleteNode(no);
        }
    }

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

    /**
     * 前序遍历查找
     * @param no no
     * @return
     */
    public Node preOrderSearch(int no){
        System.out.println("进入前序遍历");
        //比较当前结点是否相同
        if(this.no == no){
            return this;
        }
        Node 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 void infixOrder(){
        //先递归向左子树
        if(this.left != null){
            this.left.infixOrder();
        }
        //输出父节点
        System.out.println(this);
        //递归向右子树中序遍历
        if(this.right != null){
            this.right.infixOrder();
        }
    }

    /**
     * 中序查找
     * @param no 编号
     * @return
     */
    public Node infixOrderSearch(int no){
        Node resNode = null;
        if(this.left != null){
            resNode = this.left.infixOrderSearch(no);
        }
        if(resNode != null){
            return resNode;
        }
        System.out.println("中序查找");
        //如果找到则返回
        if(this.no == no){
            return this;
        }
        //否则右递归
        if(this.right != null){
            resNode = this.right.infixOrderSearch(no);
        }
        return resNode;
    }

    /**
     * 后序遍历
     */
    public void postOrder(){
        //先递归向左子树
        if(this.left != null){
            this.left.postOrder();
        }
        if(this.right != null){
            this.right.postOrder();
        }
        System.out.println(this);
    }

    /**
     * 后序遍历查找
     * @param no
     * @return
     */
    public Node postOrderSearch(int no){
        Node resNode = null;
        //向左递归查找
        if(this.left != null){
            resNode = this.left.postOrderSearch(no);
        }
        //说明在左子树找到
        if(resNode != null){
            return resNode;
        }
        //向右递归查找
        if(this.right != null){
            resNode = this.right.postOrderSearch(no);
        }
        //说明在右子树找到
        if(resNode != null){
            return resNode;
        }
        System.out.println("进入后序查找");
        //如果找到
        if(this.no == no){
            return this;
        }
        //如果没找到
        return null;
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值