一篇文章和我一起进行二叉树【前、中、后】序遍历实战(算法)

前言

  如果您觉得有用的话,记得给博主点个赞,评论,收藏一键三连啊,写作不易啊^ _ ^。
  而且听说点赞的人每天的运气都不会太差,实在白嫖的话,那欢迎常来啊!!!


一篇文章和我一起进行二叉树【前、中、后】序遍历实战(算法)

01 前期准备

  1. 一个要遍历的二叉树
  2. 我们要知道【前、中、后】序遍历是以什么逻辑来遍历的

01::01 我们要遍历的二叉树

        6
   3         9
 2   5     8   10
1   4    7

01::02 【前、中、后】序遍历后应该是什么样子的?

先序遍历 (根->左->右):

序号 过程
01 6 、【6】左分支、 【6】右分支
02 6、3、【3】左分支、【3】右分支、9、【9】左分支、10
03 6、3、2、1、5、4、9、8、7、10

中序遍历 (左->根->右):

序号 过程
01 【6】左分支、6 、 【6】右分支
02 【3】左分支 、3、【3】右分支、6、【9】左分支、9、10
03 1、2、3、4、5、6、7、8、9、10

后序遍历 (左->右->根) :

序号 过程
01 【6】左分支 、 【6】右分支、6
02 【3】左分支、【3】右分支、3、【9】左分支、10、9、6
03 1、2、4、5、3、7、8、10、9、6

02 实战

02::01 准备二叉树创建的方法

/**
 * 二叉树模板
 * @author yangzhenyu
 * */
public class BitNode {
    
    
    int data;
    //lChild左孩子,rChild 右孩子
    BitNode lChild,rChild;

}
/**
 * @author yangzhenyu
 * */
public class TreeCreate {
    
    
    /**
     * @param arr 数组
     * @param start 第一个索引位置
     * @param end 最后一个索引位置
     * 实现思路:取数组中间元素作为根节点,将数组分成左右两部分,对数据的两部分分别使用
     * 递归的方式分别构建左右子树
     * */
    public static BitNode arrayToTree(int [] arr,int start,int end){
    
    
        BitNode bitNode = null;
        if (end>=start){
    
    
            bitNode = new BitNode();
            int mid  = (start+end+1)/2;
            //树的根节点为中间的元素
            bitNode.data = arr[mid];
            //递归左部分数组构建左子树
            bitNode.lChild = arrayToTree(arr,start,mid-1);
            //递归右部分数组构建右子树
            bitNode.rChild = arrayToTree(arr,mid+1,end);
        }else {
    
    
            bitNode = null;
        }
        return bitNode;
    }
    
}

02::02 【前、中、后】序遍历算法


import com.alibaba.fastjson.JSON;
import java.util.ArrayList;
import java.util.List;

/**
 * 树的前中后遍历
 *         6
 *    3         9
 *  2   5     8   10
 * 1   4    7
 * */
public class TreeList {
    
    
    private static final int [] msg = {
    
    1,2,3,4,5,6,7,8,9,10};

    /**
     * 前序遍历(根左右)
     * 6 、【6】左分支、 【6】右分支
     * 6、3、【3】左分支、【3】右分支、9、【9】左分支、10
     * 6、3、2、1、5、4、9、8、7、10
     * @param bitNode 二叉树
     * @param returnData 遍历后返回值
     * */
    public static void qianxu(BitNode bitNode, List<Integer> returnData){
    
    
        if (bitNode == null){
    
    
            return;
        }
        //根
        returnData.add(bitNode.data) ;
        //回调左子树
        qianxu(bitNode.lChild,returnData);
        //回调右子树
        qianxu(bitNode.rChild,returnData);
    }
    /**
     * 中序遍历(左根右)
     * 【6】左分支、6 、 【6】右分支
     * 【3】左分支 、3、【3】右分支、6、【9】左分支、9、10
     *  1、2、3、4、5、6、7、8、9、10
     * @param bitNode 二叉树
     * @param returnData 遍历后返回值
     * */
    public static void zhongxu(BitNode bitNode, List<Integer> returnData){
    
    
        if (bitNode == null){
    
    
            return;
        }
        //回调左子树
        zhongxu(bitNode.lChild,returnData);
        //根
        returnData.add(bitNode.data) ;
        //回调右子树
        zhongxu(bitNode.rChild,returnData);
    }
    /**
     * 后序遍历(左右根)
     * 【6】左分支 、 【6】右分支、6
     * 【3】左分支、【3】右分支、3、【9】左分支、10、9、6
     *  1、2、4、5、3、7、8、10、9、6
     * @param bitNode 二叉树
     * @param returnData 遍历后返回值
     * */
    public static void houxu(BitNode bitNode, List<Integer> returnData){
    
    
        if (bitNode == null){
    
    
            return;
        }
        //回调左子树
        houxu(bitNode.lChild,returnData);
        //回调右子树
        houxu(bitNode.rChild,returnData);
        //根
        returnData.add(bitNode.data) ;
    }


    public static void main(String[] args) {
    
    
        //组装二叉树
        BitNode bitNode = TreeCreate.arrayToTree(msg, 0, msg.length-1);
        List<Integer> qianxuData = new ArrayList<>(10);
        List<Integer> zhongxuData = new ArrayList<>(10);
        List<Integer> houxuData = new ArrayList<>(10);

        //前序遍历(根左右)
        qianxu(bitNode,qianxuData);
        System.out.println("二叉树前序遍历 :"+ JSON.toJSONString(qianxuData));
        //中序遍历(左根右)
        zhongxu(bitNode,zhongxuData);
        System.out.println("二叉树中序遍历 :"+ JSON.toJSONString(zhongxuData));
        //后序遍历(左右根)
        houxu(bitNode,houxuData);
        System.out.println("二叉树后序遍历 :"+ JSON.toJSONString(houxuData));

    }
}

02::02 测试

在这里插入图片描述

之后和我们之前手工计算得出的遍历结果进行对比,证明测试成功!!!

猜你喜欢

转载自blog.csdn.net/weixin_38316697/article/details/118363847