#include <iostream> #include <stdlib.h> using namespace std; //enum {TRUE = 1,FALSE = 0}; typedef struct _node{ int info; struct _node *left; struct _node *right; }node; class btree{ private: node *root; void deltree(node*); public: btree(); ~btree(); void Insert(node* & , int data); node* GetRoot(void); // void DeleteNode(node *,node *,int); int Searchnode(node*,int); void DisplayTree(node*,int); void Inorder(node* ); void Preorder(node*); void Postorder(node*); bool IsBST(node*); }; btree::btree(){ root = new node; cout<<"Enter a number which will become a root"<<endl; cin>>root->info; root->left = NULL; root->right = NULL; } btree::~btree(){ deltree(root); } void btree::deltree(node *root){ if(root) { deltree(root->left); deltree(root->right); delete root; } } node* btree::GetRoot(void){ return(root); } void btree::Insert(node* &newnode,int data1){ if(newnode==NULL){ newnode=new node; (newnode)->info=data1; (newnode)->left=NULL; (newnode)->right=NULL; }else if(data1<((newnode)->info)){ Insert(((newnode)->left), data1); }else{ Insert((newnode)->right, data1); } } /* void btree::DeleteNode(node *current,node *parent,int delnode){ if(delnode < current->info) DeleteNode(current->left,current,delnode); else if(delnode > current->info) DeleteNode(current->right,current,delnode); else if(delnode == current->info) { if(current->left == NULL) { if(parent->info > current->info) parent->left = current->right; else parent->right = current->right; } else if(current->right == NULL) { if(parent->info > current->info) parent->left = current->left; else parent->right = current->left; } else { node *temp; int flag = 0; parent = current; current = current->left; temp = current; while(current->right != NULL) { current = current->right; if(flag == 0) flag = 1; else temp = temp->right; } parent->info = current->info; if(flag == 0) // No right child parent->left = current->left; else temp->right = current->left; } delete current; } }*/ int btree::Searchnode(node *current,int num){ if(num<current->info && current->left!=NULL) Searchnode(current->left,num); else if(num>current->info && current->right!=NULL) Searchnode(current->right,num); else { if(num==current->info) return 1; else return 0; } //return 0; } void btree::DisplayTree(node *current,int Level){ if(current) { DisplayTree((current)->right,Level+1); cout<<endl; for(int i=0;i<Level;i++) cout<<" "; cout<<(current)->info; DisplayTree((current)->left,Level+1); } } /*bool btree::IsBST(node *current){ if(current==NULL){ return 1; }else{ static node* prev=NULL; IsBST(current->left); if(prev!=NULL && (prev->info > current->info)) return 0; prev=current; IsBST(current->right); } return 1; }*/ bool btree::IsBST(node *current){ if(current->left !=NULL){ if(current->info<current->left->info){ return 0; }else{ IsBST(current->left); } }else if(current->right !=NULL){ if(current->info>current->right->info){ return 0; }else{ IsBST(current->right); } } return 1; } void btree::Inorder(node *current){ if(current!=NULL) { Inorder((current)->left); cout<<(current)->info; Inorder((current)->right); } } void btree::Preorder(node *current){ if(current!=NULL) { cout<<((current)->info); Preorder((current)->left); Preorder((current)->right); } } void btree::Postorder(node *current){ if(current!=NULL) { Postorder((current)->left); Postorder((current)->right); cout<<(current)->info; } } int main(){ int opt; int num; char ch='y'; char cha='y'; btree bt; node *rt; do { cout<<"\nEnter your option\n"; cout<<"1. Insert Tree\n"; //cout<<"2. Delete a node\n"; cout<<"3. search a node\n"; cout<<"4. display the tree\n"; cout<<"5. Inorder traversal\n"; cout<<"6. preorder traversal\n"; cout<<"7. postorder traversal\n"; cout<<"8. Check whether tree is binary tree\n"; cout<<"9. Exit\n"; cin>>opt; switch(opt) { case 1: while(cha=='y'){ cout<<"Enter the data:\n"; cin>>num; rt=bt.GetRoot(); bt.Insert(rt, num); cout<<"Do you want to insert more[y/n]\n"; cin>>cha; } break; /*case 2: int delnode; cout<<"\nEnter an information to be deleted\n"; cin>>delnode; bt.DeleteNode(bt.GetRoot(),NULL,delnode); break;*/ case 3: cout<<"\nEnter a number to search in the tree\n"; cin>>num; if(bt.Searchnode(bt.GetRoot(),num)) cout<<"The number is present"<<endl; else cout<<"The number is not present"<<endl; break; case 4: bt.DisplayTree(bt.GetRoot(),1); break; case 5: bt.Inorder(bt.GetRoot()); break; case 6: bt.Preorder(bt.GetRoot()); break; case 7: bt.Postorder(bt.GetRoot()); break; case 8: //cout<<(bt.GetRoot())->info<<endl; if(bt.IsBST(bt.GetRoot())) cout<<"Binary Search Tree\n"; else cout<<"Not a Binary Search Tree\n"; break; case 9: exit(0); default: cout<<"\nInvalid Entry\n"; } } while(ch=='Y' || ch=='y'); }
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
}