-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTreeDFS.cpp
More file actions
84 lines (75 loc) · 1.63 KB
/
Copy pathTreeDFS.cpp
File metadata and controls
84 lines (75 loc) · 1.63 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
74
75
76
77
78
79
80
81
82
83
84
#include<bits/stdc++.h>
using namespace std;
class Node{ //Created structure of the node of the tree
public:
int value;
Node* left;Node* right;
Node() //default contructor of the node class
{
value=-1;
left=nullptr;
right=nullptr;
}
Node(int x) // Parameterized contructor of the node class
{
value=x;
left=nullptr;
right=nullptr;
}
};
class Tree{
public:
Node* root;
Tree(){
root=nullptr;
}
Node* create(){
int x;
cout<<"\nEnter the value of x or -1 to stop :";
cin >> x;
cout<<endl;
if(x==-1){
return NULL;
}
Node *p = new Node(x);
cout<<"enter the left of the "<<x<<" :"<<endl;
p->left=create();
cout<<"enter the right of the "<<x<<" :"<<endl;
p->right=create();
return p;
}
void inorder(Node * p){ //inorder is /*/left root right/*/
if(p==NULL){
return ;
}
inorder(p->left);
cout<<p->value<<" ";
inorder(p->right);
}
void preorder(Node *p) // preorder is /*/root left right/*/
{
if (p == NULL)
{
return;
}
cout << p->value << " ";
preorder(p->left);
preorder(p->right);
}
void postorder(Node *p) // postorder is /*/left right root/*/
{
if (p == NULL)
{
return;
}
postorder(p->left);
postorder(p->right);
cout << p->value << " ";
}
};
int main(){
Tree T;
T.root=T.create();
T.inorder(T.root);
return 0 ;
}