二叉树

本文深入探讨了二叉树的概念,包括满二叉树和完全二叉树的定义。接着,详细阐述了二叉树的前序、中序和后序遍历方法,并提供了具体的代码实现。此外,还介绍了如何使用这三种遍历方式来查找二叉树中的指定节点,分析了每种查找方法的比较次数。
摘要由CSDN通过智能技术生成

二叉树

一、二叉树的概念

  1. 每个节点最多能有两个子节点的这样一种形式称为二叉树;
  2. 二叉树的子节点分为左节点和右节点;
  3. 满二叉树:该二叉树的叶子节点都在最后一层,并且节点总数为2^n-1(n 层数);
  4. 完全二叉树:该二叉树的所有叶子结点都在最后一层或者倒数第二层,而且最后一层的叶子节点在左边连续,倒数第二层的叶子节点在右边连续。

二、二叉树的遍历

(1)使用前序、中序、后序对二叉树进行遍历

  • 前序遍历:先输出父节点,再遍历左子树和右子树
  • 中序遍历:先遍历左子树,再输出父节点,再遍历右子树
  • 后序遍历:先遍历左子树,再遍历右子树,最后输出父节点
  • 可以通过输出父节点的顺序来确定是前序、中序还是后序

(2)二叉树的前序遍历步骤:

  1. 创建一颗二叉树
  2. 先输出当前节点(初始状态是root节点)
  3. 如果左子节点不为空,则递归继续前序遍历
  4. 如果右子节点不为空,则递归继续前序遍历

中序、后序......

具体代码实现见底部

三、二叉树查找指定的节点

要求:分别使用前序、中序、后序的方式来查询指定的节点

前序查找步骤:

1)先判断当前节点的no是否等于要查找的

2)如果是,则返回当前节点

3)如果不是,则判断当前节点的左子节点是否为空,如果不为空,则递归前序查找

4)如果左递归前序查找,找到节点则返回,否则继续判断当前节点的右子节点是否为空,如果不空,则继续向右递归前序查找

中序查找步骤:

1)判断当前节点的左子节点是否为空,如果不为空,则递归中序查找

2)如果找到,则返回,如果没有找到,就和当前节点比较,如果是则返回,否则继续进行右递归的中序查找

3)如果右递归中序查找,找到就返回,否则返回null

后序查找步骤:

1)判断当前节点的左子节点是否为空,如果不为空,则递归后序查找

2)如果找到,则返回,如果没有找到,判断当前节点的右子节点是否为空,如果不为空,则继续向右递归后序查找,如果找到,就返回

3)如果没找到,就和当前节点进行比较,如果是则返回,否则返回null

二叉树查找指定节点

题目要求:1.使用前序、中序、后序对二叉树进行遍历

                  2.前序查找、中序查找、后序查找二叉树的指定节点4

                  3.并分析三种查找方法分别比较了多少次

完整的代码实现

public class BinaryTreeDemo {

    public static void main(String[] args) {
        //先需要创建一颗二叉树
        BinaryTree binaryTree = new BinaryTree();
        //创建需要的结点
        Node root = new Node(1);
        Node node2 = new Node(2);
        Node node3 = new Node(3);
        Node node4 = new Node(4);
        Node node5 = new Node(5);

        //创建该二叉树
        root.setLeft(node2);
        root.setRight(node3);
        node3.setLeft(node4);
        node3.setRight(node5);
        binaryTree.setRoot(root);

        System.out.println("前序遍历"); // 1,2,3,4,5
        binaryTree.preOrder();

        System.out.println("中序遍历");
        binaryTree.infixOrder(); // 2,1,4,3,5

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

        //前序遍历查找    4
        System.out.println("前序遍历查找开始");
        Node resNode = binaryTree.preOrderSearch(4);
        if (resNode != null) {
            System.out.printf("找到了no = %d",resNode.getNo());
        } else {
            System.out.printf("没有找到no = %d ", 4);
        }

        //中序遍历查找   3
        System.out.println("中序遍历查找开始");
        Node resNode1 = binaryTree.infixOrderSearch(4);
        if (resNode1 != null) {
            System.out.printf("找到了 no=%d", resNode1.getNo());
        } else {
            System.out.printf("没有找到 no = %d ", 4);
        }

        //后序遍历查找   2
        System.out.println("后序遍历查找开始");
        Node resNode2 = binaryTree.postOrderSearch(4);
        if (resNode2 != null) {
            System.out.printf("找到了no=%d", resNode2.getNo());
        } else {
            System.out.printf("没有找到 no = %d ", 4);
        }


    }

}

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

    public void setRoot(Node 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 postOrder() {
        if(this.root != null) {
            this.root.postOrder();
        }else {
            System.out.println("二叉树为空,无法遍历");
        }
    }

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

//先创建Node 结点
class Node {
    private int no;

    private Node left; //默认null
    private Node right; //默认null
    public Node(int no) {
        this.no = no;

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

    public Node getLeft() {
        return left;
    }
    public void setLeft(Node left) {
        this.left = left;
    }
    public Node getRight() {
        return right;
    }
    public void setRight(Node right) {
        this.right = right;
    }
    @Override
    public String toString() {
        return "Node{" +
                "no=" + no +
                '}';
    }



    //前序遍历
    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 Node preOrderSearch(int no) {
        System.out.println("进入前序遍历");
        Node resNode = null;
        //比较当前结点是不是
        if(this.no == no) {
            return this;
        }
        //1.判断当前结点的左子节点是否为空,如果不为空,则递归前序查找
        //2.如果左递归前序查找,找到结点,则返回
        if(this.left != null) {
            resNode = this.left.preOrderSearch(no);
        }
        if(resNode != null) {//说明左子树找到
            return resNode;
        }

        //3.当前的结点的右子节点是否为空,如果不空,则继续向右递归前序查找
        if(this.right != null) {
            resNode = this.right.preOrderSearch(no);
        }
        return resNode;
    }

    //中序遍历查找
    public Node infixOrderSearch(int no) {
        //判断当前结点的左子节点是否为空,如果不为空,则递归中序查找
        Node resNode1 = null;
        if(this.left != null) {
            resNode1 = this.left.infixOrderSearch(no);
        }
        if(resNode1 != null) {
            return resNode1;
        }
        System.out.println("进入中序查找");
        //如果找到,则返回,如果没有找到,就和当前结点比较,如果是则返回当前结点
        if(this.no == no) {
            return this;
        }
        //否则继续进行右递归的中序查找
        if(this.right != null) {
            resNode1= this.right.infixOrderSearch(no);
        }
        return resNode1;

    }

    //后序遍历查找
    public Node postOrderSearch(int no) {

        //判断当前结点的左子节点是否为空,如果不为空,则递归后序查找
        Node resNode2 = null;
        if(this.left != null) {
            resNode2 = this.left.postOrderSearch(no);
        }
        if(resNode2 != null) {//说明在左子树找到
            return resNode2;
        }

        //如果左子树没有找到,则向右子树递归进行后序遍历查找
        if(this.right != null) {
            resNode2 = this.right.postOrderSearch(no);
        }
        if(resNode2 != null) {
            return resNode2;
        }
        System.out.println("进入后序查找");
        //如果左右子树都没有找到,就比较当前结点是不是
        if(this.no == no) {
            return this;
        }
        return resNode2;
    }

}

 

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值