本文實(shí)例為大家分享了java實(shí)現(xiàn)二叉樹遍歷的具體代碼,供大家參考,具體內(nèi)容如下
二叉樹如下:
遍歷結(jié)果如下:
以下是實(shí)現(xiàn)代碼:
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
|
package bintree; import java.util.stack; /** * @author bin.zhang * @version 2017年8月29日 上午10:22:01 */ public class bintreetraversal { public static void main(string[] args) { system.out.print( "前序:" ); traversal.preorder(); traversal.preorderrecursion(traversal.createbintree()); system.out.print( "中序:" ); traversal.inorder(); traversal.inorderrecursion(traversal.createbintree()); system.out.print( "后序:" ); traversal.postorder(); traversal.postorderrecursion(traversal.createbintree()); } } /** * 節(jié)點(diǎn)數(shù)據(jù)結(jié)構(gòu) * * @author bin.zhang * @version 2017年8月30日 上午11:49:38 */ class bintreenode { bintreenode() { } bintreenode( char data, int flag, bintreenode lchild, bintreenode rchild) { this .data = data; this .flag = flag; this .lchild = lchild; this .rchild = rchild; } char data; int flag; bintreenode lchild, rchild; } class traversal { /** * 創(chuàng)建一棵二叉樹 * * @author bin.zhang * @return 根節(jié)點(diǎn) */ public static bintreenode createbintree() { bintreenode r3 = new bintreenode( 'f' , 0 , null , null ); bintreenode l2 = new bintreenode( 'd' , 0 , null , null ); bintreenode r2 = new bintreenode( 'e' , 0 , null , r3); bintreenode l1 = new bintreenode( 'b' , 0 , l2, r2); bintreenode r1 = new bintreenode( 'c' , 0 , null , null ); bintreenode t = new bintreenode( 'a' , 0 , l1, r1); return t; } // 前序 public static void preorder() { bintreenode p = createbintree(); stack<bintreenode> stack = new stack<bintreenode>(); while (p != null || !stack.empty()) { if (p != null ) { system.out.print(p.data); stack.push(p); p = p.lchild; } else { p = stack.pop(); p = p.rchild; } } system.out.println(); } // 前序遞歸 public static void preorderrecursion(bintreenode top) { if (top != null ) { system.out.println(top.data); preorderrecursion(top.lchild); preorderrecursion(top.rchild); } } // 中序 public static void inorder() { bintreenode p = createbintree(); stack<bintreenode> stack = new stack<bintreenode>(); while (p != null || !stack.empty()) { if (p != null ) { stack.push(p); p = p.lchild; } else { p = stack.pop(); system.out.print(p.data); p = p.rchild; } } system.out.println(); } // 中序遞歸 public static void inorderrecursion(bintreenode top) { if (top != null ) { inorderrecursion(top.lchild); system.out.println(top.data); inorderrecursion(top.rchild); } } // 后序 public static void postorder() { bintreenode p = createbintree(); stack<bintreenode> stack = new stack<bintreenode>(); // 初始化棧 int mark = 1 ; // 轉(zhuǎn)向標(biāo)志 while (p != null || !stack.empty()) { // 遍歷 if (p != null && mark != 0 ) { stack.push(p); p = p.lchild; } // 轉(zhuǎn)向左子樹 else { p = stack.pop(); p.flag++; // 退棧 if (p.flag == 1 ) { stack.push(p); p = p.rchild; mark = 1 ; } // 轉(zhuǎn)向右子樹 else if (p.flag == 2 && !stack.empty()) { // 輸出結(jié)點(diǎn) system.out.print(p.data); mark = 0 ; } else if (p.flag == 2 && stack.empty()) { // 輸出根結(jié)點(diǎn)并退出 system.out.print(p.data); break ; } } // if-else } // while system.out.println(); } // 后序遞歸 public static void postorderrecursion(bintreenode top) { if (top != null ) { postorderrecursion(top.lchild); postorderrecursion(top.rchild); system.out.println(top.data); } } } |
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://blog.csdn.net/zhangbinu/article/details/77679901