forked from adee-dev/clg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree_recursive.cpp
80 lines (54 loc) · 1.51 KB
/
tree_recursive.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
74
75
76
77
78
79
80
#include<bits/stdc++.h>
using namespace std;
struct node{
int roll;
struct node* left;
struct node* right;
};
class tree{
public:
node* newNode(int data){
node* temp = new node;
temp->roll = data;
temp->left = temp->right = NULL;
return temp;
}
void recursive_preorder(node* root){
if(root == NULL){
return;
}
cout<<root->roll<<" ";
recursive_preorder(root->left);
recursive_preorder(root->right);
}
void recursive_postorder(node* root){
if(root == NULL){
return;
}
recursive_postorder(root->left);
recursive_postorder(root->right);
cout<<root->roll<<" ";
}
void recursive_inorder(node* root){
if(root == NULL){
return;
}
recursive_inorder(root->left);
cout<<root->roll<<" ";
recursive_inorder(root->right);
}
};
int main() {
tree tt;
struct node* root = tt.newNode(1);
root->left = tt.newNode(2);
root->right = tt.newNode(3);
root->left->left = tt.newNode(4);
root->left->right = tt.newNode(5);
tt.recursive_preorder(root);
cout<<endl;
tt.recursive_postorder(root);
cout<<endl;
tt.recursive_inorder(root);
return 0;
}