Skip to content

Commit 8047d26

Browse files
committed
update
1 parent 8b4e2a2 commit 8047d26

File tree

2 files changed

+234
-0
lines changed

2 files changed

+234
-0
lines changed

CP Templetes/trees/bst.cpp

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/*----------------------------------------------------------------------*/
2+
#include <bits/stdc++.h>
3+
using namespace std;
4+
5+
/*----------------------------------------------------------------------------*/
6+
7+
typedef long long ll;
8+
typedef vector<int> vi;
9+
10+
/*----------------------------------------------------------------------------*/
11+
12+
#define MOD 1e9 + 7
13+
#define pb push_back
14+
#define ppb pop_back
15+
#define f first
16+
#define s second
17+
#define nl "\n"
18+
#define pie 3.141592653589793238462
19+
#define set_bits(x) __builtin_popcountll(x)
20+
#define all(x) (x).begin(), (x).end()
21+
#define debug(x) cerr<<x<<" "
22+
#define loop(i,a,b) for(int i=(a);i<(b);i++)
23+
#define printvector(x) for(auto i:(x)) cout<<(i)<<" "
24+
25+
/*----------------------------------------------------------------------------*/
26+
27+
template<typename T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
28+
template<typename T> T binpow(T base,T power){ ll ans=1; while(power){if(power&1) ans=ans*base; base=base*base; power>>=1;}return ans;}
29+
30+
31+
/*----------------------------------------------------------------------------*/
32+
class node
33+
{
34+
public:
35+
int val;
36+
node*left;
37+
node*right;
38+
node(int val)
39+
{
40+
this->val=val;
41+
this->left=nullptr;
42+
this->right=nullptr;
43+
}
44+
};
45+
node* insertbst(node*&root,int val)
46+
{
47+
48+
if(root==NULL)
49+
{
50+
node*temp=new node(val);
51+
root=temp;
52+
return root;
53+
54+
}
55+
if(val>root->val)
56+
{
57+
root->right=insertbst(root->right,val);
58+
}
59+
else
60+
{
61+
root->left=insertbst(root->left,val);
62+
}
63+
return root;
64+
}
65+
void buildbst(node*&root)
66+
{
67+
int val;
68+
cin>>val;
69+
while(val!=-1)
70+
{
71+
root=insertbst(root,val);
72+
cin>>val;
73+
}
74+
}
75+
void levelordertreversal(node*root)
76+
{
77+
queue<node*>q;
78+
q.push(root);
79+
q.push(NULL);
80+
while(!q.empty())
81+
{
82+
node*temp=q.front();
83+
q.pop();
84+
if(temp==NULL)
85+
{
86+
cout<<endl;
87+
if(!q.empty())
88+
{
89+
q.push(NULL);
90+
}
91+
}
92+
else
93+
{
94+
cout<<temp->val<<" ";
95+
if(temp->left)
96+
{
97+
q.push(temp->left);
98+
}
99+
if(temp->right)
100+
{
101+
q.push(temp->right);
102+
}
103+
}
104+
}
105+
}
106+
107+
int main()
108+
{
109+
ios_base::sync_with_stdio(0);
110+
cin.tie(0);
111+
node* root=NULL;
112+
buildbst(root);
113+
levelordertreversal(root);
114+
115+
}
116+

Cp Algorithms/Binary Tree.cpp

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/*----------------------------------------------------------------------*/
2+
#include <bits/stdc++.h>
3+
using namespace std;
4+
5+
/*----------------------------------------------------------------------------*/
6+
7+
typedef long long ll;
8+
typedef vector<int> vi;
9+
10+
/*----------------------------------------------------------------------------*/
11+
12+
#define MOD 1e9 + 7
13+
#define pb push_back
14+
#define ppb pop_back
15+
#define f first
16+
#define s second
17+
#define nl "\n"
18+
#define pie 3.141592653589793238462
19+
#define set_bits(x) __builtin_popcountll(x)
20+
#define all(x) (x).begin(), (x).end()
21+
#define debug(x) cerr<<x<<" "
22+
#define loop(i,a,b) for(int i=(a);i<(b);i++)
23+
#define printvector(x) for(auto i:(x)) cout<<(i)<<" "
24+
25+
/*----------------------------------------------------------------------------*/
26+
27+
template<typename T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
28+
template<typename T> T binpow(T base,T power){ ll ans=1; while(power){if(power&1) ans=ans*base; base=base*base; power>>=1;}return ans;}
29+
30+
31+
/*----------------------------------------------------------------------------*/
32+
class node
33+
{
34+
int val;
35+
node*left;
36+
node*right;
37+
node(int val)
38+
{
39+
this->val=val;
40+
this->left=nullptr;
41+
this->right=nullptr;
42+
}
43+
}
44+
node*insertbst(node*&root,int val)
45+
{
46+
47+
if(root==NULL)
48+
{
49+
node*temp=new node(val);
50+
root=temp;
51+
return root;
52+
53+
}
54+
if(val>root->val)
55+
{
56+
root->right=insertbst(root->right,val);
57+
}
58+
else
59+
{
60+
root->left=insertbst(root->left,val);
61+
}
62+
return root;
63+
}
64+
void buildbst(node*&root)
65+
{
66+
int val;
67+
cin>>val;
68+
while(val!=-1)
69+
{
70+
root=insertbst(root,val);
71+
cin>>val;
72+
}
73+
}
74+
void levelordertreversal(node*root)
75+
{
76+
queue<node*>q;
77+
q.push(root);
78+
q.push(NULL);
79+
while(!q.empty())
80+
{
81+
node*temp=q.front();
82+
q.pop();
83+
if(temp==NULL)
84+
{
85+
cout<<endl;
86+
if(!q.empty())
87+
{
88+
q.push(NULL);
89+
}
90+
}
91+
else
92+
{
93+
cout<<temp->val<<" ";
94+
if(temp->left)
95+
{
96+
q.push(temp->left);
97+
}
98+
if(temp->right)
99+
{
100+
q.push(temp->right);
101+
}
102+
}
103+
}
104+
}
105+
106+
int main()
107+
{
108+
ios_base::sync_with_stdio(0);
109+
cin.tie(0);
110+
node* root=NULL;
111+
buildbst(root);
112+
levelordertreversal(root);
113+
114+
115+
116+
117+
}
118+

0 commit comments

Comments
 (0)