forked from H0NEYP0T-466/DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_tree.cpp
More file actions
85 lines (85 loc) · 1.47 KB
/
Copy pathbinary_tree.cpp
File metadata and controls
85 lines (85 loc) · 1.47 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
85
#include<iostream>
using namespace std;
class tree
{
public:
string data;
tree* left;
tree* right;
tree(string d)
{
data=d;
left=NULL;
right=NULL;
}
};
void preorder(tree* current)
{
if(current==nullptr)
{
return;
}
cout<<current->data;
preorder(current->left);
preorder(current->right);
}
void inorder(tree* current)
{
if(current==nullptr)
{
return ;
}
inorder(current->left);
cout<<current->data;
inorder(current->right);
}
void postorder(tree* current)
{
if(current==nullptr)
{
return;
}
postorder(current->left);
postorder(current->right);
cout<<current->data;
}
tree* searchnode(tree* current,string data)
{
if(current==nullptr || current->data==data)
{
return current;
}
tree* leftsubtree=searchnode(current->left,data);
if(leftsubtree!=NULL)
{
return leftsubtree;
}
return searchnode(current->right,data);
}
int main()
{
tree* root=new tree("A");
root->left=new tree("B");
root->left->left=new tree("D");
root->left->right=new tree("E");
root->right=new tree("C");
root->right->left=new tree("F");
root->right->right=new tree("G");
cout<<"PREORDER:\n";
preorder(root);
cout<<"\nINORDER:\n";
inorder(root);
cout<<"\nPOSTORDER:\n";
postorder(root);
tree* res=searchnode(root,"A");
{
if(res)
{
cout<<"\nNODE FOUND WITH THIS DATA:"<<res->data<<" AND THIS ADDRESS:"<<res<<endl;
}
else
{
cout<<"\nNODE NOT FOUND\n";
}
}
}