#include <bits/stdc++.h> using namespace std; class Node{ public: int data; Node *left,*right; Node(int val){ data=val; left=right=NULL; } }; class Btree{ public: Node *root; int max_sum_from_root_to_leaf=0,diameter=0; std::vector<Node*> v; std::vector<Node*> L; std::vector<Node*> R; std::map<int, Node*> mpt; std::map<int, Node*> mpb; Btree(){ root=NULL; } void preorder(Node *root){ Node *temp=root; if(temp==NULL) return; cout<<temp->data<<" "; preorder(temp->left); preorder(temp->right); } void postorder(Node *root){ Node *temp=root; if(temp==NULL) return; postorder(temp->left); postorder(temp->right); cout<<temp->data<<" "; } void inorder(Node *root){ Node *temp=root; if(temp==NULL) return; inorder(temp->left); cout<<temp->data<<" "; inorder(temp->right); } void levelorder(Node* root){ queue<Node*> q; q.push(root); int k=0; while(!q.empty()){ int count=q.size(),sum=0; for(int i=0;i<count;i++){ Node *t=q.front(); cout<<t->data<<" ";sum+=t->data; q.pop(); if(t->left) q.push(t->left); if(t->right) q.push(t->right); } cout<<"---> sum of level "<<++k<<" = "<<sum<<endl; } } int height(Node* root){ if(root==NULL) return 0; int lh=height(root->left); int rh=height(root->right); diameter=max(diameter,lh+rh); return max(lh,rh)+1; } int getDiameter(Node* root){ diameter=0; height(root); return diameter; } void sumPath(Node* root,int sum=0){ if(root==NULL) return; if(root->left==NULL && root->right==NULL){ sum+=root->data; max_sum_from_root_to_leaf=max(sum,max_sum_from_root_to_leaf); return; } sumPath(root->left,sum+root->data); sumPath(root->right,sum+root->data); } void Lview(Node* root){ queue<Node*> q; q.push(root); while(!q.empty()){ int count=q.size(); L.push_back(q.front()); for(int i=0;i<count;i++){ Node *t=q.front(); q.pop(); if(t->left) q.push(t->left); if(t->right) q.push(t->right); } } } void Rview(Node* root){ queue<Node*> q; q.push(root); while(!q.empty()){ int count=q.size(); R.push_back(q.back()); for(int i=0;i<count;i++){ Node *t=q.front(); q.pop(); if(t->left) q.push(t->left); if(t->right) q.push(t->right); } } } void Tview(Node* root,int hd){ /* vector<Node*> T(L); reverse(T.begin(),T.end()); for(auto it=R.begin()+1;it!=R.end();++it) T.push_back(*it); for(auto x:T) cout<<x->data<<" "; */ if(root==NULL) return; if(!mpt.count(hd)) mpt[hd]=root; Tview(root->left,hd-1); Tview(root->right,hd+1); } void Bview(Node *root,int hd){ if(root==NULL) return; mpb[hd]=root; Bview(root->left,hd-1); Bview(root->right,hd+1); } void spiral(Node *root){ queue<Node*> q; vector<Node*> sp; q.push(root); int level=0; while(!q.empty()){ int count=q.size(),sum=0; for(int i=0;i<count;i++){ Node *t=q.front(); sp.push_back(t); q.pop(); if(t->left) q.push(t->left); if(t->right) q.push(t->right); } level++; if(level & 1) reverse(sp.begin(),sp.end()); for(auto x:sp) cout<<x->data<<" "; sp.clear(); } } bool search(Node *root,int val){ if(root==NULL) return 0; bool ltree=search(root->left,val); bool rtree=search(root->right,val); return (root->data==val) or ltree or rtree; } }; int main() { Btree t; t.root=new Node(10); t.root->left=new Node(20); t.root->right=new Node(30); t.root->left->left=new Node(40); t.root->left->right=new Node(50); t.root->right->left=new Node(60); t.root->right->right=new Node(70); cout<<"Preorder: "; t.preorder(t.root); cout<<endl<<"Inorder: "; t.inorder(t.root); cout<<endl<<"Postorder: "; t.postorder(t.root); cout<<endl<<"Levelorder :"<<endl; t.levelorder(t.root); cout<<"Height of Btree = "<<t.height(t.root)<<endl; cout<<"Diameter of Btree = "<<t.getDiameter(t.root)<<endl; t.sumPath(t.root); cout<<"max_sum_from_root_to_leaf = "<<t.max_sum_from_root_to_leaf<<endl; cout<<"Left view of Btree : "; t.Lview(t.root); for(auto x:t.L) cout<<x->data<<" "; cout<<endl; cout<<"Right view of Btree : "; t.Rview(t.root); for(auto x:t.R) cout<<x->data<<" "; cout<<endl; cout<<"Top view of Btree : "; t.Tview(t.root,0); for(auto x:t.mpt) cout<<x.second->data<<" "; cout<<endl; cout<<"Bottom view of Btree : "; t.Bview(t.root,0); for(auto x:t.mpb) cout<<x.second->data<<" "; cout<<endl; cout<<"Elements in spiral order : "; t.spiral(t.root); cout<<endl; if(t.search(t.root,33)) cout<<"Present"; else cout<<"Absent"; return 0; }
Write, Run & Share C++ code online using OneCompiler's C++ online compiler for free. It's one of the robust, feature-rich online compilers for C++ language, running on the latest version 17. Getting started with the OneCompiler's C++ compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as C++
and start coding!
OneCompiler's C++ online compiler supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample program which takes name as input and print your name with hello.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
cout << "Enter name:";
getline (cin, name);
cout << "Hello " << name;
return 0;
}
C++ is a widely used middle-level programming language.
When ever you want to perform a set of operations based on a condition If-Else is used.
if(conditional-expression) {
//code
}
else {
//code
}
You can also use if-else for nested Ifs and If-Else-If ladder when multiple conditions are to be performed on a single variable.
Switch is an alternative to If-Else-If ladder.
switch(conditional-expression){
case value1:
// code
break; // optional
case value2:
// code
break; // optional
......
default:
code to be executed when all the above cases are not matched;
}
For loop is used to iterate a set of statements based on a condition.
for(Initialization; Condition; Increment/decrement){
//code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while (condition) {
// code
}
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
do {
// code
} while (condition);
Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity. Function gets run only when it is called.
return_type function_name(parameters);
function_name (parameters)
return_type function_name(parameters) {
// code
}