-
Notifications
You must be signed in to change notification settings - Fork 0
/
lab8a_Binary_Tree2.cpp
73 lines (61 loc) · 1.28 KB
/
lab8a_Binary_Tree2.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
62
63
64
65
66
67
68
69
70
71
72
73
// Example of binary tree traversal... ... ... ... ...
#include <bits/stdc++.h>
using namespace std;
class node{
public:
node *left;
int info;
node *right;
node(int a) : left(nullptr), info(a), right(nullptr){}
};
node* insert(node *root, int a) {
node *ptr = new node(a);
if (root->left == nullptr) {
root->left = ptr;
}
else {
root->right = ptr;
}
return ptr;
}
void inorder(node *root) {
if (root == nullptr) {
return;
}
inorder(root->left);
cout << root->info << " ";
inorder(root->right);
}
void preorder(node *root) {
if (root == nullptr) {
return;
}
cout << root->info << " ";
preorder(root->left);
preorder(root->right);
}
void postorder(node *root) {
if (root == nullptr) {
return;
}
postorder(root->left);
postorder(root->right);
cout << root->info << " ";
}
int main() {
node *root = new node(1);
node *rt1 = insert(root, 12);
node *rt2 = insert(root, 9);
node* rt3 = insert(rt1, 5);
node* rt4 = insert(rt1, 6);
cout << "inorder: ";
inorder(root);
cout << endl;
cout << "preorder: ";
preorder(root);
cout << endl;
cout << "postorder: ";
postorder(root);
cout << endl;
return 0;
}