-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlab_DS3_BT.cpp
More file actions
73 lines (64 loc) · 1.95 KB
/
lab_DS3_BT.cpp
File metadata and controls
73 lines (64 loc) · 1.95 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
69
70
71
72
73
#include<bits/stdc++.h>
using namespace std;
struct NODE {
int data;
struct NODE* left;
struct NODE* right;
};
NODE* createNode(int data) {
NODE* new_node = (struct NODE *) malloc(sizeof(struct NODE));
new_node->data = data;
new_node->left = NULL;
new_node->right = NULL;
return new_node;
}
NODE* insertNode(NODE* parent, int key, int dir) {
if(parent == NULL) {
return createNode(key);
}
if(dir > 0) {
parent->right = createNode(key);
} else {
parent->left = createNode(key);
}
return parent;
}
void preOrderTraversal(NODE* root) {
if(root == NULL) return;
cout << root->data << ' ';
preOrderTraversal(root->left);
preOrderTraversal(root->right);
}
void inOrderTraversal(NODE* root) {
if(root == NULL) return;
inOrderTraversal(root->left);
cout << root->data << ' ';
inOrderTraversal(root->right);
}
void postOrderTraversal(NODE* root) {
if(root == NULL) return;
postOrderTraversal(root->left);
postOrderTraversal(root->right);
cout << root->data << ' ';
}
int main() {
NODE* root = createNode(1);
NODE* node[6];
for(int i = 0; i < 6; ++i) {
node[i] = createNode(i+2);
}
root->left = node[0]; /* 1 */
root->right = node[1]; /* / \ */
root->left->left = node[2]; /* 2 3 */
root->left->right = node[3]; /* / \ / \ */
root->right->left = node[4]; /* 4 5 6 7 */
root->right->right = node[5];
cout << "PreOrderTraversal:\t";
preOrderTraversal(root);
cout << "\nInOrderTravesal:\t";
inOrderTraversal(root);
cout << "\nPostOrderTraversal:\t";
postOrderTraversal(root);
cout << '\n';
return 0;
}