Java实现二叉树的前、中、后序遍历

前言

把学习的笔记记录下来,希望能帮助到你。

实现思想

  1. 访问根节点
  2. 访问当前节点的左子树,递归(即把左子树当成一课新的树,重新执行1-3步)
  3. 访问当前节点的右子树,递归

中序遍历的实现思想是:

  1. 访问当前节点的左子树,递归
  2. 访问根节点
  3. 访问当前节点的右子树,递归

后序遍历的实现思想:

  1. 访问当前节点的左子树,递归
  2. 访问当前节点的右子树,递归
  3. 访问根节点

代码实现

以这个图的树作为测试的树。实现遍历

image-20210102223951217

package com.datastructure.tree;

/**
 * @author Hacah
 * @date 2021/1/2 21:04
 * @Description 实现二叉树的前、中、后序遍历。
 */
public class BinaryTreeDemo {

    public static void main(String[] args) {
        BinaryTree binaryTree = new BinaryTree(1, "张三");
        binaryTree.addLeftChild(2, "李四").addLeftChild(3, "小唐");
        binaryTree.addRightChild(4, "大千");

        // 前序遍历
        binaryTree.preOrder();
        System.out.println();
        // 中序遍历
        binaryTree.midOrder();
        System.out.println();
        // 后序遍历
        binaryTree.posOrder();
    }


}

/**
 * 二叉树的类
 */
class BinaryTree {

    /** 根节点 */
    private Node root;

    public BinaryTree(int no, String name) {
        root = new Node(no, name);
    }

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

    public void midOrder() {
        if (root != null) {
            root.midOrder();
        } else {
            System.out.println("root为空");
        }
    }

    public void posOrder() {
        if (root != null) {
            root.postOrder();
        } else {
            System.out.println("root为空");
        }
    }

    /**
     * 添加左节点
     *
     * @return
     */
    public Node addLeftChild(int no, String name) {
        return root.addLeftChild(no, name);
    }

    /**
     * 添加右节点
     *
     * @return
     */
    public Node addRightChild(int no, String name) {
        return root.addRightChild(no, name);
    }


    /**
     * 二叉树的节点
     */
    static class Node {

        private int no;
        private String name;
        private Node leftChild;
        private Node rightChild;

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

        /**
         * 前序遍历
         */
        public void preOrder() {
            System.out.println(this);
            if (this.leftChild != null) {
                leftChild.preOrder();
            }
            if (rightChild != null) {
                rightChild.preOrder();
            }
        }

        /**
         * 中序遍历
         */
        public void midOrder() {
            if (this.leftChild != null) {
                leftChild.midOrder();
            }
            System.out.println(this);
            if (this.rightChild != null) {
                rightChild.midOrder();
            }
        }

        /**
         * 后序遍历
         */
        public void postOrder() {
            if (this.leftChild != null) {
                leftChild.postOrder();
            }
            if (rightChild != null) {
                rightChild.postOrder();
            }
            System.out.println(this);
        }

        /**
         * 添加左节点
         *
         * @return
         */
        public Node addLeftChild(int no, String name) {
            Node node = new Node(no, name);
            this.leftChild = node;
            return node;
        }

        /**
         * 添加右节点
         *
         * @return
         */
        public Node addRightChild(int no, String name) {
            Node node = new Node(no, name);
            rightChild = node;
            return node;
        }

        @Override
        public String toString() {
            return "Node{" +
                    "no=" + no +
                    ", name='" + name + '\'' +
                    '}';
        }

    }

}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值