- Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathTreeTraversalsExample.java
More file actions
Latest commit
68 lines (60 loc) · 1.67 KB
/
Copy pathTreeTraversalsExample.java
File metadata and controls
68 lines (60 loc) · 1.67 KB
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
packagech14;
publicclassTreeTraversalsExample {
staticclassTNode {
charval;
TNodeleft;
TNoderight;
publicTNode(charval) {
this.val = val;
}
publicTNode(charval, TNodeleft, TNoderight) {
this.val = val;
this.left = left;
this.right = right;
}
}
publicstaticvoidpreorder(TNodenode) {
if (node == null)
return;
System.out.printf("%c ", node.val);
preorder(node.left);
preorder(node.right);
}
publicstaticvoidinorder(TNodenode) {
if (node == null)
return;
inorder(node.left);
System.out.printf("%c ", node.val);
inorder(node.right);
}
publicstaticvoidpostorder(TNodenode) {
if (node == null)
return;
postorder(node.left);
postorder(node.right);
System.out.printf("%c ", node.val);
}
publicstaticvoidmain(String[] args) {
// 그림 14-26 예제 트리 생성
TNoderoot = newTNode('F',
newTNode('B',
newTNode('A'),
newTNode('D',
newTNode('C'), newTNode('E'))
),
newTNode('G',
null,
newTNode('I',
newTNode('H'), null))
);
// 전위 순회
preorder(root);
System.out.println();
// 중위 순회
inorder(root);
System.out.println();
// 후위 순회
postorder(root);
System.out.println();
}
}