-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path15_binaryTree.cpp
61 lines (54 loc) ยท 1.02 KB
/
15_binaryTree.cpp
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
#include <iostream>
// ์ด์งํธ๋ฆฌ
using namespace std;
int number = 15;
// ์ฌ์ฉ์ ์ ์ ํ์
typedef struct node* treePointer;
typedef struct node {
int data;
treePointer leftChild, rightChlid;
};
void preorder(treePointer str) {
if (str) {
cout << str->data << ' ';
preorder(str->leftChild);
preorder(str->rightChlid);
}
}
void inorder(treePointer str) {
if (str) {
inorder(str->leftChild);
cout << str->data << ' ';
inorder(str->rightChlid);
}
}
void postorder(treePointer str) {
if (str) {
postorder(str->leftChild);
postorder(str->rightChlid);
cout << str->data << ' ';
}
}
int main(void) {
node nodes[16];
for (int i = 1; i <= number; i++) {
nodes[i].data = i;
nodes[i].leftChild = NULL;
nodes[i].rightChlid = NULL;
}
for (int i = 1; i <= number; i++) {
if (i % 2 == 0) {
nodes[i / 2].leftChild = &nodes[i];
}
else {
nodes[i / 2].rightChlid = &nodes[i];
}
}
preorder(&nodes[1]);
cout << endl;
inorder(&nodes[1]);
cout << endl;
postorder(&nodes[1]);
cout << endl;
return 0;
}