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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
|
public
class
BinaryTreeNode {
int
value;
BinaryTreeNode left;
BinaryTreeNode right;
// 前序遍历
public
static
void
preOrder(BinaryTreeNode tree) {
if
(tree ==
null
)
return
;
System.out.print(tree.value +
" "
);
preOrder(tree.left);
preOrder(tree.right);
}
/**
* 二叉树前序遍历的循环实现方式
*
* @param tree
*/
public
static
void
preOrderLoop(BinaryTreeNode tree) {
if
(tree ==
null
)
return
;
Stack<BinaryTreeNode> stack =
new
Stack<BinaryTreeNode>();
BinaryTreeNode node = tree;
while
(node !=
null
|| !stack.isEmpty()) {
if
(node !=
null
) {
stack.push(node);
System.out.print(node.value +
" "
);
node = node.left;
}
else
{
BinaryTreeNode item = stack.pop();
node = item.right;
}
}
}
// 中序遍历
public
static
void
inOrder(BinaryTreeNode tree) {
if
(tree ==
null
)
return
;
inOrder(tree.left);
System.out.print(tree.value +
" "
);
inOrder(tree.right);
}
/**
* 二叉树中序遍历的循环实现
*
* @param tree
*/
public
static
void
inOrderLoop(BinaryTreeNode tree) {
if
(tree ==
null
)
return
;
Stack<BinaryTreeNode> stack =
new
Stack<BinaryTreeNode>();
BinaryTreeNode node = tree;
while
(node !=
null
|| !stack.empty()) {
if
(node !=
null
) {
stack.push(node);
node = node.left;
}
else
{
BinaryTreeNode item = stack.pop();
System.out.print(item.value +
" "
);
node = item.right;
}
}
}
// 后序遍历
public
static
void
postOrder(BinaryTreeNode tree) {
if
(tree ==
null
)
return
;
postOrder(tree.left);
postOrder(tree.right);
System.out.print(tree.value +
" "
);
}
/**
* 二叉树后序遍历的循环实现
*
* @param tree
*/
public
static
void
postOrderLoop(BinaryTreeNode tree) {
if
(tree ==
null
)
return
;
HashSet<BinaryTreeNode> visited =
new
HashSet<BinaryTreeNode>();
Stack<BinaryTreeNode> stack =
new
Stack<BinaryTreeNode>();
BinaryTreeNode node = tree;
while
(node !=
null
|| !stack.isEmpty()) {
if
(node !=
null
) {
stack.push(node);
node = node.left;
}
else
{
BinaryTreeNode item = stack.peek();
if
(item.right !=
null
&& !visited.contains(item.right))
node = item.right;
else
{
System.out.print(item.value +
" "
);
visited.add(item);
stack.pop();
}
}
}
}
public
static
void
main(String[] args) {
List<Integer> list1 =
new
ArrayList<Integer>();
List<Integer> list2 =
new
ArrayList<Integer>();
list1.add(
1
);
list1.add(
2
);
list1.add(
4
);
list1.add(
7
);
list1.add(
3
);
list1.add(
5
);
list1.add(
6
);
list1.add(
8
);
list2.add(
4
);
list2.add(
7
);
list2.add(
2
);
list2.add(
1
);
list2.add(
5
);
list2.add(
3
);
list2.add(
8
);
list2.add(
6
);
BinaryTreeNode tree = BinaryTreeNode.Construct(list1, list2);
BinaryTreeNode.preOrderLoop(tree);
System.out.println();
BinaryTreeNode.inOrderLoop(tree);
System.out.println();
BinaryTreeNode.postOrderLoop(tree);
}
}
|
本文转自 许大树 51CTO博客,原文链接:http://blog.51cto.com/abelxu/1968112,如需转载请自行联系原作者