二叉树的构建 遍历 和查询

162 阅读4分钟

一起养成写作习惯!这是我参与「掘金日新计划 · 4 月更文挑战」的第25天,点击查看活动详情

1.什么是二叉树

二叉树(binary tree)是指树中节点的度不大于2的有序树,它是一种最简单且最重要的树。二叉树的递归定义为:二叉树是一棵空树,或者是一棵由一个根节点和两棵互不相交的,分别称作根的左子树和右子树组成的非空树;左子树和右子树又同样都是二叉树

2.二叉树的构建

2.1 构建节点对象和树对象

image.png

image.png

2.2将节点连接成树

image.png

3.二叉树的遍历

分为前序遍历 中序遍历 后序遍历 区分的方式为 父节点在前面遍历 即为前序 在第二遍历即为中序 最后即为后续,左右节点的顺序都是先左后右

3.1前序遍历

前序遍历
1。先输出当前节点
2。左节点不为空执行前序遍历
3。右节点不为空执行前序遍历

image.png

3.2中序遍历

中序遍历
1。先递归左子数 执行中序便利
2。输出父节点
3。右递归 执行中序遍历

image.png

3.3后序遍历

后序遍历 1。先递归左子数 执行中序便利
2。右递归 执行中序遍历
3。输出父节点\

image.png

4.树的查询

跟遍历逻辑大同小异

5.具体代码

package src.com.luoxinduan.study.designpattern.tree;

/**
 * @author duanlx
 * @version 1.0.0
 * @ClassName BinaryTreeDemo.java
 * @Description TODO 二叉树
 * @createTime 2022年04月25日 14:54:00
 */
public class BinaryTreeDemo {
    public static void main(String[] args) {

        //创建二叉树
        BinaryTree binaryTree = new BinaryTree();

        TreeNode root = new TreeNode("1");
        TreeNode treeNode2 = new TreeNode("2");
        TreeNode treeNode3 = new TreeNode("3");
        TreeNode treeNode4 = new TreeNode("4");
        TreeNode treeNode5 = new TreeNode("5");

        binaryTree.setRoot(root);
        root.setLeft(treeNode2);
        root.setRight(treeNode3);
        treeNode3.setRight(treeNode4);
        treeNode3.setLeft(treeNode5);

        System.out.println("前序遍历");
        binaryTree.preOrder();

        System.out.println("中序遍历");
        binaryTree.infixOrder();

        System.out.println("后序遍历");
        binaryTree.postOrder();

        System.out.println("前序遍历查询");
        binaryTree.preOrderInquire("4");

        System.out.println("中序遍历查询");
        binaryTree.infixOrderInquire("4");

        System.out.println("后序遍历查询");
        binaryTree.postOrderInquire("4");





    }
}
class BinaryTree{
    private TreeNode root;

    public void setRoot(TreeNode 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 void preOrderInquire(String no){
        if(this.root!=null){
            System.out.println(this.root.preOrderInquire(no));
        }else {
            System.out.println("二叉树为空");
        }
    }
    //中序遍历
    public void infixOrderInquire(String no){
        if(this.root!=null){
            System.out.println(this.root.infixOrderInquire(no));
        }else {
            System.out.println("二叉树为空");
        }
    }
    //后序遍历
    public void postOrderInquire(String no){
        if(this.root!=null){
            System.out.println(this.root.postOrderInquire(no));
        }else {
            System.out.println("二叉树为空");
        }
    }
}
//创建节点
class TreeNode{
   private String no;//编号
    private TreeNode left;//左孩子
    private TreeNode right;//右孩子

    public TreeNode(String no) {
        this.no = no;
    }

    public String getNo() {
        return no;
    }

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

    public TreeNode getLeft() {
        return left;
    }

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

    public TreeNode getRight() {
        return right;
    }

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

    @Override
    public String toString() {
        return "TreeNode{" +
                "no='" + no + ''' +
                '}';
    }


    /**
     * 前序遍历
     * 1。先输出当前节点
     * 2。左节点不为空执行前序遍历
     * 3。右节点不为空执行前序遍历
     */
    public void preOrder(){
        //1。先输出当前节点
        System.out.println(this);
        //2。左节点不为空执行前序遍历
        if(this.left!=null){
            this.left.preOrder();
        }
        //3。右节点不为空执行前序遍历
        if(this.right!=null){
            this.right.preOrder();
        }
    }


    /**
     * 前序遍历查询
     * 1。先输出当前节点
     * 2。左节点不为空执行前序遍历
     * 3。右节点不为空执行前序遍历
     */
    public TreeNode preOrderInquire(String no){

        if(this.no.equals(no)){
            return this;
        }
        //2。左节点不为空执行前序遍历
        TreeNode resTree = null;
        if(this.left!=null){
            resTree = this.left.preOrderInquire(no);
        }
        if(resTree!=null){
            return resTree;
        }
        //3。右节点不为空执行前序遍历
        if(this.right!=null){
            return this.right.preOrderInquire(no);
        }
        return null;
    }
    /**
     * 中序遍历
     * 1。先递归左子数 执行中序便利
     * 2。输出父节点
     * 3。右递归 执行中序遍历
     */
    public TreeNode infixOrderInquire(String no){
        //1。先递归左子数 执行中序便利
        TreeNode resTree = null;
        if(this.left!=null){
            resTree = this.left.infixOrderInquire(no);
        }
        if(resTree!=null){
            return resTree;
        }
        if(this.no.equals(no)){
            return this;
        }
        //3。右递归 执行中序遍历
        if(this.right!=null){
            return this.right.infixOrderInquire(no);
        }
        return null;
    }
    //后序遍历
    public TreeNode postOrderInquire(String no){
        //1。先递归左子数 执行后序遍历
        TreeNode resTree = null;
        if(this.left!=null){
            resTree = this.left.postOrderInquire(no);
        }
        if(resTree!=null){
            return resTree;
        }
        //2。右递归 执行后序遍历
        if(this.right!=null) {
            return this.right.postOrderInquire(no);
        }
        if(this.no.equals(no)){
            return this;
        }
        return null;
    }
    /**
     * 中序遍历
     * 1。先递归左子数 执行中序便利
     * 2。输出父节点
     * 3。右递归 执行中序遍历
     */
    public void infixOrder(){
        //1。先递归左子数 执行中序便利
      if(this.left!=null){
          this.left.infixOrder();
      }
        //2。输出父节点
        System.out.println(this);
        //3。右递归 执行中序遍历
        if(this.right!=null){
            this.right.infixOrder();
        }
    }
    //后序遍历
    public void postOrder(){
        //1。先递归左子数 执行后序遍历
        if(this.left!=null){
            this.left.postOrder();
        }
        //2。右递归 执行后序遍历
        if(this.right!=null) {
            this.right.postOrder();


        }
        //3。输出父节点
        System.out.println(this);
    }


}