-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path02.txt
199 lines (178 loc) · 3.28 KB
/
02.txt
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// Beginning with an empty binary search tree, Construct binary search
tree by inserting the values in the order given. After constructing a binary tree –
i.Insert new node
ii.Find number of nodes in longest path
iii.Minimum data value found inthe tree
iv.Change a tree so that the roles of the left and right pointers are swapped at
every node
v.Search a value
#include<iostream>
#include<math.h>
using namespace std;
struct Bstnode
{
int data;
Bstnode *left = NULL;
Bstnode *right = NULL;
};
class Btree
{
int n;
int x;
int flag;
public:
Bstnode * root;
Btree()
{
root = NULL;
}
Bstnode *GetNewNode(int in_data)
{
Bstnode * ptr = new Bstnode();
ptr->data = in_data;
ptr->left = NULL;
ptr->right = NULL;
return ptr;
}
Bstnode *insert( Bstnode *temp , int in_data)
{
if( temp == NULL )
{
temp = GetNewNode(in_data);
}
else if( temp->data > in_data)
{
temp->left = insert(temp->left , in_data);
}
else
{
temp->right = insert( temp->right , in_data);
}
return temp;
}
void input()
{
cout<<"ENTER NUMBER OF ELEMENTS IN THE BST : ";
cin>>n;
for(int i = 0 ; i < n ; i++)
{
cout<<"NUMBER = ";
cin>>x;
root = insert(root , x);
}
}
int search(Bstnode *temp ,int in_data)
{
if( temp != NULL)
{
if(temp->data == in_data)
{
cout<<":-- RECORD FOUND --:"<<endl;
return 1;
}
else if(in_data < temp->data)
{
this->search(temp->left, in_data);
}
else if(in_data > temp->data)
{
this->search(temp->left , in_data);
}
}
else
{
return 0;
}
}
void minvalue(Bstnode *temp)
{
while(temp->left != NULL)
{
temp = temp->left;
}
cout<<"MINIMUM VALUE = "<<temp->data<<endl;
}
void mirror(Bstnode *temp)
{
if(temp == NULL)
{
return;
}
else
{
Bstnode *ptr;
mirror(temp->left);
mirror(temp->right);
ptr = temp->left;
temp->left = temp->right;
temp->right = ptr;
}
}
void display()
{
cout<<endl<<"--- INORDER TRAVERSAL ---"<<endl;
inorder(root);
cout<<endl;
cout<<endl<<"--- POSTORDER TRAVERSAL ---"<<endl;
postorder(root);
cout<<endl;
cout<<endl<<"--- PREORDER TRAVERSAL ---"<<endl;
preorder(root);
cout<<endl;
}
void inorder(Bstnode *temp)
{
if(temp != NULL)
{
inorder(temp->left);
cout<<temp->data<<" ";
inorder(temp->right);
}
}
void postorder(Bstnode *temp)
{
if(temp != NULL)
{
postorder(temp->left);
postorder(temp->right);
cout<<temp->data<<" ";
}
}
void preorder(Bstnode *temp)
{
if(temp != NULL)
{
cout<<temp->data<<" ";
preorder(temp->left);
preorder(temp->right);
}
}
int depth(Bstnode *temp)
{
if(temp == NULL)
return 0;
return (max((depth(temp->left)),(depth(temp->right))) +1);
}
};
int main()
{
Btree obj;
obj.input();
obj.display();
int a = 0;
a = obj.search(obj.root,10);
if( a == 0)
{
cout<<"ELEMENT NOT FOUND"<<endl;
}
else
cout<<"ELEMENT FOUND"<<endl;
cout<<endl<<a<<endl;
obj.minvalue(obj.root);
obj.mirror(obj.root);
obj.inorder(obj.root);
//int d ;
cout<<endl<<obj.depth(obj.root);
//cout<<endl<<d<<endl;
return 0;
}