二叉树的遍历
- 前序遍历:先输出根节点,在遍历左子树和右子树:1–2--3–4--5–6
- 中序遍历:先遍历左子树,在输出父节点,在遍历右子树:2–3--1–5--4–6
- 后序遍历:先遍历左子树,在遍历右子树,最后输出父节点:3–2--5–6--4–1
注意可以根据父节点的输出顺序来判断遍历是什么遍历
二叉树的前序遍历
思路:
- 先输出当前节点(初始的时候是父节点)
- 如果左子节点不为空,则递归继续前序遍历
- 如果右子节点不为空,则递归继续前序遍历
//前序遍历方法
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);
}
测试代码:
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
结果和预期结果一致。