Java数据结构——二叉树的遍历(汇总)

二叉树的遍历分为深度优先遍历(DFS)和广度优先遍历(BFS)

DFS遍历主要有:

  • 前序遍历
  • 中序遍历
  • 后序遍历

一、递归实现DFS
Node.java:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
public class Node {
private Object data;
Node richild;
Node lechild;
 
public Object getData() {
return data;
}
 
public void setData(Object data) {
this.data = data;
}
 
public Node getRichild() {
return richild;
}
 
public void setRichild(Node richild) {
this.richild = richild;
}
 
public Node getLechild() {
return lechild;
}
 
public void setLechild(Node lechild) {
this.lechild = lechild;
}
 
public Node(Object data, Node lechild, Node richild) {
super();
this.data = data;
this.richild = richild;
this.lechild = lechild;
}
 
public Node() {
super();
}
 
}

递归遍历:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
public class BTree {
 
private static Node root;
//构造树
public static void init() {
Node node1 = new Node("A", null, null);
Node node2 = new Node("B", node1, null);
Node node3 = new Node("C", null, null);
Node node4 = new Node("D", node2, node3);
Node node5 = new Node("E", null, null);
Node node6 = new Node("F", null, node5);
Node node7 = new Node("G", node4, node6);
root = node7;
}
//访问节点
public static void visited(Node n) {
System.out.print(n.getData() + " ");
}
//前序遍历
public static void preOrder(Node n) {
if (n != null) {
visited(n);
preOrder(n.getLechild());
preOrder(n.getRichild());
}
}
//中序遍历
public static void inOrder(Node n) {
if (n != null) {
inOrder(n.getLechild());
visited(n);
inOrder(n.getRichild());
}
}
//后序遍历
public static void postOrder(Node n) {
if (n != null) {
postOrder(n.getLechild());
postOrder(n.getRichild());
visited(n);
}
}
 
public static void main(String[] args) {
init();
System.out.print("递归前序:");
preOrder(root);
System.out.println();
System.out.print("递归中序:");
inOrder(root);
System.out.println();
System.out.print("递归后序:");
postOrder(root);
System.out.println();
}
 
}

 

二、非递归实现DFS(依靠栈)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
//前序遍历
public static void preOrder(Node n) {
System.out.print("非递归前序:");
Stack<Node> stack = new Stack<>();
int index = 0;
while (n != null || index > 0) {
while (n != null) {
System.out.print(n.getData() + " ");
stack.push(n);
index++;
n = n.getLechild();
}
n = stack.pop();
index--;
n = n.getRichild();
}
}
//中序遍历
public static void inOrder(Node n) {
System.out.print("非递归中序:");
Stack<Node> stack = new Stack<>();
int index = 0;
while (n != null || index > 0) {
while (n != null) {
stack.push(n);
index++;
n = n.getLechild();
}
n = stack.pop();
System.out.print(n.getData() + " ");
index--;
n = n.getRichild();
}
}
//后序遍历
public static void postOrder(Node n) {
System.out.print("非递归后序:");
Stack<Node> stack = new Stack<>();
int index = 0;
Node lastVisited = null;
while (n != null || index > 0) {
while (n != null) {
stack.push(n);
index++;
n = n.getLechild();
}
n = stack.peek();
if (n.getRichild() == null || n.getRichild() == lastVisited) {
System.out.print(n.getData() + " ");
lastVisited = n;
index--;
stack.pop();
n = null;
} else {
n = n.getRichild();
}
}
}

  

三、实现层序遍历(依靠队列)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public static void LevenOrder(Node root) {
if (root == null) {
return;
}
Queue<Node> queue = new LinkedList<>();
queue.add(root);
Node temp = null;
while (!queue.isEmpty()) {
temp = queue.poll();
visited(temp);
if (temp.getLeChild() != null) {
queue.add(temp.getLeChild());
}
if (temp.getRChild() != null) {
queue.add(temp.getChild());
}
}
}
posted @   橘子洲头。  阅读(433)  评论(0编辑  收藏  举报
编辑推荐:
· Linux glibc自带哈希表的用例及性能测试
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
· 现代计算机视觉入门之:什么是图片特征编码
阅读排行:
· 手把手教你在本地部署DeepSeek R1,搭建web-ui ,建议收藏!
· Spring AI + Ollama 实现 deepseek-r1 的API服务和调用
· 数据库服务器 SQL Server 版本升级公告
· C#/.NET/.NET Core技术前沿周刊 | 第 23 期(2025年1.20-1.26)
· 程序员常用高效实用工具推荐,办公效率提升利器!
点击右上角即可分享
微信分享提示