前言
二叉树,有深度优先遍历和广度优先遍历,但是这两个概念不止局限于二叉树,它们更是一种抽象的算法思想,决定了访问某些复杂数据结构的顺序。本文主要讲述深度优先遍历的三种方法!
所谓深度优先,顾名思义,就是偏向于纵深,“一头扎到底”的访问方式。这样说很抽象,下边我们就通过深度优先的前序,中序,后序三种遍历方式,来看一看深度优先把。
内容解读:
三种遍历思想:
前序遍历:根结点 ---> 左子树 ---> 右子树
中序遍历:左子树---> 根结点 ---> 右子树
后序遍历:左子树 ---> 右子树 ---> 根结点
上图树的三种遍历:
前序:A B D E C F
中序:D B E A C F
后序:D E B C A F
为了便于理解下边将围绕中序遍历(迭代法)展开说明:
1.采用非递归的方法我们首先可以考虑到用栈(stack)压入弹出的方法来进行对节点的控制
2.再声明一个专门用来返回的List队列
代码如下(迭代法):
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
/**
* @author gs🐏
* @version 1.0
* @describe
* @date
*/
public class BinTreeTraversal {
//测试
public static void main(String[] args) {
Traversal traversal = new Traversal();
System.out.print("前序遍历:");
System.out.println(traversal.preOrder());
System.out.print("中序遍历:");
System.out.println(traversal.inOrder());
System.out.print("后序遍历:");
System.out.println(traversal.postOrder());
}
}
/**
*
* @Description 节点结构创建
* @author gcystart
* @version
* @date
*
*/
class BinTreeNode<Character> {
char val;
BinTreeNode<Character> lchild,rchild;//树的左孩子,右孩子
BinTreeNode(char val, BinTreeNode<Character> lchild, BinTreeNode<Character> rchild) {
this.val = val;
this.lchild = lchild;
this.rchild = rchild;
}
}
class Traversal {
/**
* @return 根节点
* @Description 创建一棵二叉树
* @author gcystart
* @date
*/
public static BinTreeNode<Character> createBinTree() {
//<Character>:泛型:优点是保证类型的安全
BinTreeNode<Character> RR2 = new BinTreeNode<>('F', null, null);
BinTreeNode<Character> LR2 = new BinTreeNode<>('E', null, null);
BinTreeNode<Character> LL2 = new BinTreeNode<>('D', null, null);
BinTreeNode<Character> L1 = new BinTreeNode<>('B', LL2, LR2);
BinTreeNode<Character> R1 = new BinTreeNode<>('C', null, RR2);
BinTreeNode<Character> node = new BinTreeNode<>('A', L1, R1);
return node;
}
/*
前序遍历:迭代法
*/
public List<Character> preOrder() {
BinTreeNode<Character> root = createBinTree(); //根节点root
List<Character> res = new ArrayList<>(); //创建一个队列,用来返回
Stack<BinTreeNode<Character>> stack = new Stack<>(); //创建一个栈
//迭代访问节点的左孩子,并入栈
while (root != null || !stack.isEmpty()) {
while (root != null) {
res.add(root.val);
stack.push(root);
root = root.lchild;
}
//如果节点没有左孩子,则弹出栈顶节点,访问节点的右孩子
root = stack.pop();
root = root.rchild;
}
return res;
}
/*
中序迭代法
*/
public List<Character> inOrder() {
BinTreeNode<Character> root = createBinTree();
List<Character> res = new ArrayList<>();
Stack<BinTreeNode<Character>> stack = new Stack<>();
while (root != null || !stack.isEmpty()) {
while (root != null) {
stack.push(root);
root = root.lchild;
}
//如果节点没有左孩子,则弹出栈顶节点,并将弹出的的节点加入到res中。访问节点的右孩子
root = stack.pop();
res.add(root.val);
root = root.rchild;
}
return res;
}
/*
后序迭代法
*/
public List<Character> postOrder() {
BinTreeNode<Character> root = createBinTree();
BinTreeNode<Character> prevAccess = null;
List<Character> res = new ArrayList<>();
Stack<BinTreeNode<Character>> stack = new Stack<BinTreeNode<Character>>();
while (root != null || !stack.isEmpty()) {
while (root != null) {
stack.push(root);
root = root.lchild;
}
root = stack.pop();
if (root.rchild == null || root.rchild == prevAccess) {
res.add(root.val);
prevAccess = root;
root = null;
} else {
stack.push(root);
root = root.rchild;
}
}
return res;
}
}
代码如下(递归法):
**
* @author gs🐏
* @version 1.0
* @describe
* @date 2022-04-11 15:47
*/
public class BinTreeTraversal {
//测试
public static void main(String[] args) {
System.out.println("前序遍历:");
Traversal.preOrderRecursion(Traversal.createBinTree()); //递归方法的调用
System.out.println("中序遍历:");
Traversal.inOrderRecursion(Traversal.createBinTree());
System.out.println("后序遍历:");
Traversal.postOrderRecursion(Traversal.createBinTree());
}
}
/**
*
* @Description 节点结构创建
* @author gcystart
* @version
* @date
*
*/
class BinTreeNode<Character> {
char data;
BinTreeNode<Character> lchild,rchild;//树的左孩子,右孩子
BinTreeNode(char data, BinTreeNode<Character> lchild, BinTreeNode<Character> rchild) {
this.data = data;
this.lchild = lchild;
this.rchild = rchild;
}
}
class Traversal {
/**
* @return 根节点
* @Description 创建一棵二叉树
* @author gcystart
* @date
*/
public static BinTreeNode<Character> createBinTree() {
BinTreeNode<Character> RR2 = new BinTreeNode<>('F', null, null);
BinTreeNode<Character> LR2 = new BinTreeNode<>('E', null, null);
BinTreeNode<Character> LL2 = new BinTreeNode<>('D', null, null);
BinTreeNode<Character> L1 = new BinTreeNode<>('B', LL2, LR2);
BinTreeNode<Character> R1 = new BinTreeNode<>('C', null, RR2);
BinTreeNode<Character> node = new BinTreeNode<>('A', L1, R1);
return node;
}
/*
前序递归法
*/
public static void preOrderRecursion(BinTreeNode<Character> root) {
if (root != null) {
System.out.println(root.data);
preOrderRecursion(root.lchild);
preOrderRecursion(root.rchild);
}
}
/*
中序递归法
*/
public static void inOrderRecursion(BinTreeNode<Character> root) {
if (root != null) {
inOrderRecursion(root.lchild);
System.out.println(root.data);
inOrderRecursion(root.rchild);
}
}
/*
后序递归法
*/
public static void postOrderRecursion(BinTreeNode<Character> root) {
if (root != null) {
postOrderRecursion(root.lchild);
postOrderRecursion(root.rchild);
System.out.println(root.data);
}
}
}
二叉树用递归方式来三种遍历,是最自然的方式,因此代码也非常简单。
总结
这三种遍历方式的区别,仅仅是输出的执行位置不同:前序遍历的输出在前,中序遍历的输出在中间,后序遍历的输出在最后。