java二叉树前序、中序、后序、层序遍历

 本文为博主原创文章,转载请附带博客地址:https://www.cnblogs.com/xbjhs/p/12108230.html

出来混总是要还的,当初上大学不好好听老师讲,现在工作了,还得买老师出的书再看一遍(手动笑哭)。还在上学的好好学吧。

java系的童鞋给你们推荐一本书:王红梅老师的《数据结构--从概念到java实现》,相对比c++的那版,这个可能更适合java主语言的你

--------------------------------------------------------------------------------------------------------------------------------------------------

1、看此文章之前,需要先掌握队列、二叉树的顺序存储结构。(下次更新用非递归实现遍历)

2、树的截图,代码debug截图在下面,帮助更好理解。

import java.util.AbstractSequentialList;

/**
 * @description:
 * @author: 
 * @create: 2019-12-27 15:49
 **/
public class BiNode<T> {


    /**
     * 定义节点泛型数据域
     */
    private T data;
    /**
     * 定义节点左孩子引用域
     */
    private BiNode<T> lChild;
    /**
     * 定义节点右孩子引用域
     */
    private BiNode<T> rChild;

    /**
     * 构造数据域为element的结点
     */
    public BiNode(T element) {
        this.data = element;
    }

    public T getData() {
        return data;
    }

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

    public BiNode<T> getlChild() {
        return lChild;
    }

    public void setlChild(BiNode<T> lChild) {
        this.lChild = lChild;
    }

    public BiNode<T> getrChild() {
        return rChild;
    }

    public void setrChild(BiNode<T> rChild) {
        this.rChild = rChild;
    }
}

 

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentLinkedQueue;

/**
 * @description:
 * @author:
 * @create: 2019-12-27 15:55
 **/
public class BinaryTreeImpl {


    /**
     * 前序遍历:根、左,右
     */
    public static void preOrder(BiNode node) {
        if (null == node) {
            return;
        }
        //访问当前根节点的数据域
        System.out.print(node.getData());
        //前序递归遍历node左子树
        preOrder(node.getlChild());
        //前序递归遍历node右子树
        preOrder(node.getrChild());
    }

    /**
     * 中:左 根 右
     */
    public static void inOrder(BiNode node) {
        if (null == node) {
            return;
        }
        //中序递归遍历node左子树
        inOrder(node.getlChild());
        //访问当前根节点的数据域
        System.out.print(node.getData());
        //中序递归遍历node右子树
        inOrder(node.getrChild());

    }

    /**
     * 后:  左右根
     */
    public static void postOrder(BiNode node) {
        if (null == node) {
            return;
        }
        //后序递归遍历node左子树
        postOrder(node.getlChild());
        //后序递归遍历node右子树
        postOrder(node.getrChild());
        //访问当前根节点的数据域
        System.out.print(node.getData());
    }

    /**
     * 层 逐层遍历,每层从左到右
     * 先被访问结点的孩子 先与 后被访问结点的孩子 "先先"特性用队列
     */
    public static void levelOrder(BiNode root) {
        ConcurrentLinkedQueue<BiNode> queue = new ConcurrentLinkedQueue<>();
        if (null == root) {
            return;
        }
        queue.add(root);
        while (!queue.isEmpty()) {
            BiNode node = queue.poll();
            System.out.print(node.getData());
            if (null != node.getlChild()) {
                queue.add(node.getlChild());
            }
            if (null != node.getrChild()) {
                queue.add(node.getrChild());
            }
        }
    }


    public static BiNode createTree(Object[] objects) {
        //存储所有节点
        List<BiNode> datas = new ArrayList<>();
        for (Object obj : objects) {
            datas.add(new BiNode(obj));
        }
        int location = objects.length / 2;
        for (int i = 0; i < location; i++) {
            datas.get(i).setlChild(datas.get(2 * i + 1));
            //避免偶数的时候,下标越界
            if (2 * i + 2 < objects.length) {
                datas.get(i).setrChild(datas.get(2 * i + 2));
            }
        }
        //将一个作为根节点
        BiNode root = datas.get(0);
        return root;
    }

    public static void main(String[] args) {
        //二叉树顺序存储数据,二叉树见下图
        String[] seqTree = {"A", "B", "C", "D", "", "E", "F", "", "G"};
        BiNode root = createTree(seqTree);
        System.out.print("前序遍历结果:");
        preOrder(root);
        System.out.println();
        System.out.print("中序遍历结果:");
        inOrder(root);
        System.out.println();
        System.out.print("后序遍历结果:");
        postOrder(root);
        System.out.println();
        System.out.print("层序遍历结果:");
        levelOrder(root);
    }
}

 

 

posted @ 2019-12-27 16:53  小宝的进化之路  阅读(273)  评论(0编辑  收藏  举报