二叉树知识之1二叉树的遍历

二叉树的遍历

二叉树的遍历图解

  1. 前序遍历:先输出根节点,在遍历左子树和右子树:1–2--3–4--5–6
  2. 中序遍历:先遍历左子树,在输出父节点,在遍历右子树:2–3--1–5--4–6
  3. 后序遍历:先遍历左子树,在遍历右子树,最后输出父节点:3–2--5–6--4–1

注意可以根据父节点的输出顺序来判断遍历是什么遍历

二叉树的前序遍历

思路:

  1. 先输出当前节点(初始的时候是父节点)
  2. 如果左子节点不为空,则递归继续前序遍历
  3. 如果右子节点不为空,则递归继续前序遍历
 //前序遍历方法
    public void preOrder(){
        //先输出父节点
        System.out.println(this);
        //递归左子树
        if(this.left!=null){
            this.left.preOrder();
        }
        //递归向右的子树
        if(this.right!=null){
            this.right.preOrder();
        }
    }

二叉树的中序遍历

思路:

  1. 如果当前节点的左子节点不为空,则递归中序遍历
  2. 输出当前节点
  3. 如果当前节点的右子节点不为空,则递归中序遍历
//中序遍历
    public void infix(){
        //左节点遍历
        if(this.left!=null){
            this.left.infix();
        }
        //中序遍历
        System.out.println(this);
        //右节点遍历
        if(this.right!=null){
            this.right.infix();
        }
    }

二叉树的后序遍历

思路:

  1. 如果当前节点的左子节点不为空,则递归后序遍历
  2. 如果当前节点的右子节点不为空,则递归后序遍历
  3. 输出当前节点
public void suffix(){
        if(this.left!=null){
            this.left.suffix();
        }
        if(this.right!=null){
            this.right.suffix();
        }
        System.out.println(this);
    }

测试代码:

package com.njupt.binaryTree;

/**
 * Creat with IntelliJ IDEA
 *
 * @Auther:倔强的加瓦
 * @Date:2021/07/22/17:57
 * @Description:二叉树前中后遍历的实现
 */
public class Ergodic {
    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, "云云");
        Node node6 = new Node(6, "凯凯");
        //手动创建二叉树
        root.left = node2;
        node2.right = node3;
        root.right = node4;
        node4.left = node5;
        node4.right = node6;
        //设置好头节点
        binaryTree.root = root;
        //测试前序遍历
        System.out.println("前序遍历的结果:");
        binaryTree.preOrder();
        System.out.println("中序遍历的结果:");
        binaryTree.infixOrder();
        System.out.println("后序遍历的结果:");
        binaryTree.postOrder();
    }
}

//二叉树,来管理节点
class BinaryTree {
//参数是需要传入根节点
    public Node root;

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

    //中序遍历
    public void infixOrder() {
        if (this.root != null) {
            this.root.infix();
        } else {
            System.out.println("二叉树为空");
        }
    }

    //后序遍历
    public void postOrder() {
        if (this.root != null) {
            this.root.suffix();
        } else {
            System.out.println("二叉树为空");
        }
    }
}

//节点信息
class Node {
    public int no;
    public String name;
    public Node left;
    public Node right;

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

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

    //前序遍历方法
    public void preOrder() {
        //先输出父节点
        System.out.println(this);
        //递归左子树
        if (this.left != null) {
            this.left.preOrder();
        }
        //递归向右的子树
        if (this.right != null) {
            this.right.preOrder();
        }
    }

    //中序遍历
    public void infix() {
        //左节点遍历
        if (this.left != null) {
            this.left.infix();
        }
        //中序遍历
        System.out.println(this);
        //右节点遍历
        if (this.right != null) {
            this.right.infix();
        }
    }

    //后序遍历
    public void suffix() {
        if (this.left != null) {
            this.left.suffix();
        }
        if (this.right != null) {
            this.right.suffix();
        }
        System.out.println(this);
    }
}

结果:

前序遍历的结果:
Node{no=1, name='张飞'}
Node{no=2, name='牛牛'}
Node{no=3, name='兰兰'}
Node{no=4, name='班班'}
Node{no=5, name='云云'}
Node{no=6, name='凯凯'}
中序遍历的结果:
Node{no=2, name='牛牛'}
Node{no=3, name='兰兰'}
Node{no=1, name='张飞'}
Node{no=5, name='云云'}
Node{no=4, name='班班'}
Node{no=6, name='凯凯'}
后序遍历的结果:
Node{no=3, name='兰兰'}
Node{no=2, name='牛牛'}
Node{no=5, name='云云'}
Node{no=6, name='凯凯'}
Node{no=4, name='班班'}
Node{no=1, name='张飞'}

Process finished with exit code 0

结果和预期结果一致。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值