-
Notifications
You must be signed in to change notification settings - Fork 0
/
binary tree.cpp
180 lines (180 loc) · 3.25 KB
/
binary tree.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
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
/*
* binary tree.cpp
*
* Created on: 11-Sep-2018
* Author: indrajeet
*/
#include<bits/stdc++.h>
using namespace std;
struct node{
int data;
node *left,*right;
};
class tree{
private:
node *root;
public:
tree():root(nullptr){}
node *getRoot(){
return root;
}
void setRoot(node *root){
this->root = root;
}
node* createNode(int);
void bfs(node *);
void insert(node *,int);
int findmax(node *);
bool search(node *,int);
int size(node *);
void reversebfs(node *);
void deltree(node *);
};
node *tree::createNode(int val){
node *newNode = new(node);
newNode-> data=val;
newNode->left = newNode->right = nullptr;
return newNode;
}
void tree::insert(node *rt,int val){
if(rt==nullptr)
root = createNode(val);
else{
queue<node*> q;
q.push(rt);
while(true){
rt = q.front();
q.pop();
if(rt->left)
q.push(rt->left);
else{
rt->left = createNode(val);
break;
}
if(rt->right)
q.push(rt->right);
else{
rt->right = createNode(val);
break;
}
}
}
}
int tree::findmax(node *rt){
if(rt == nullptr)
return 0;
if(rt->left == nullptr && rt->right == nullptr)
return rt->data;
int lst_max = findmax(rt->left);
int rst_max = findmax(rt->right);
return max(rt->data,max(lst_max,rst_max));
}
bool tree::search(node *rt,int target){
if(rt == nullptr)return false;
if(rt->data == target)return true;
bool found = search(rt->left,target);
if(found)return true;
else return search(rt->right,target);
}
void tree::bfs(node *rt){
if(rt != nullptr){
queue<node *> q;
q.push(rt);
while(!q.empty()){
rt= q.front();
q.pop();
cout<<rt->data<<" ";
if(rt->left)
q.push(rt->left);
if(rt->right)
q.push(rt->right);
}
}
}
int tree::size(node *rt){
if(rt == nullptr)
return 0;
return 1+size(rt->left)+size(rt->right);
}
void tree::reversebfs(node *rt){
queue<node*> q;
stack<node*> st;
if(rt != nullptr){
q.push(rt);
while(!q.empty()){
rt = q.front();
q.pop();
if(rt->left)
q.push(rt->left);
if(rt->right)
q.push(rt->right);
st.push(rt);
}
while(!st.empty()){
cout<<st.top()->data<<" ";
st.pop();
}
}
}
void tree::deltree(node *rt){
if(rt == nullptr)
return;
deltree(rt->left);
deltree(rt->right);
cout<<"deleting node ..."<<rt->data<<endl;
delete rt;
rt = nullptr;
}
int main(){
tree t;
string str ="Enter choice :\n"
"0:Exit\n"
"1:insert\n"
"2:delete\n"
"3:print\n"
"4:search\n"
"5:find max\n"
"6:size\n"
"7:reverse print\n";
int choice;
do{
cout<<str;
cin>>choice;
int val;
switch(choice){
case 1:
cout<<"Insert an item"<<endl;
cin>>val;
t.insert(t.getRoot(),val);
break;
case 2:
t.deltree(t.getRoot());
t.setRoot(nullptr);
break;
case 3:
t.bfs(t.getRoot());
cout<<endl;
break;
case 4:
cout<<"Enter value to be searched"<<endl;
cin>>val;
if(t.search(t.getRoot(),val))cout<<"value found"<<endl;
else cout<<"value not found"<<endl;
break;
case 5:
cout<<t.findmax(t.getRoot())<<endl;
break;
case 6:
cout<<t.size(t.getRoot())<<endl;
break;
case 7:
t.reversebfs(t.getRoot());
cout<<endl;
break;
default:
if(choice != 0)
cout<<"Invalid choice"<<endl;
}
}while(choice != 0);
return 0;
}