Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
TemplateBinTree
//g++ 7.4.0 //Templated Binary Tree: Insert operation //code is created by Rezaul Hoque on March 04,2021 //please contact at jewelmrh@yahoo.com #include <bits/stdc++.h> using namespace std; template <class T> class node { public: T data; node<T> *left, *right; }; // create a new BST node template <class T> node<T>* addNew(T item) { node<T>* temp = (node<T>*)malloc(sizeof( node<T>)); temp->data = item; temp->left = temp->right = NULL; return temp; } // inorder traversal template <class T> void inorder( node<T>* root) { if (root != NULL) { inorder(root->left); cout <<" "<<root->data; inorder(root->right); } } //Pre order traversal template <class T> void preorder( node<T> *temp) { if (temp != NULL) { cout << " " << temp->data; preorder(temp->left); preorder(temp->right); } } //Post Order traversal template <class T> void postorder( node<T> *temp) { if (temp != NULL) { postorder(temp->left); postorder(temp->right); cout << " " << temp->data; } } // Insert a new node with given data template <class T> node<T>* insert( node<T>* node, T d) { if (node == NULL) return addNew(d); if (d < node->data) node->left = insert(node->left, d); else node->right = insert(node->right, d); return node; } int main() { node<int>* root=NULL; node<char>* croot = NULL; root= insert(root,90); root= insert(root,80); root= insert(root,70); root= insert(root,100); root= insert(root,110); croot = insert(croot,'E'); croot = insert(croot,'D'); croot = insert(croot,'C'); croot = insert(croot,'F'); croot = insert(croot,'G'); cout <<"Pre order:\n"; preorder(root); cout<<endl; preorder(croot); cout<<endl; cout <<"In order:\n"; inorder(root); cout<<endl; inorder(croot); cout<<endl; cout <<"Post order:\n"; postorder(root); cout<<endl; postorder(croot); cout<<endl; return 0; }
run
|
edit
|
history
|
help
0
BinSearch
Sort row sorted matrix
numberOftweets
Connected components in complement graph
Mirror a binary tree
12532
cppPyClamInher
Test 1(2021)
segmented sieve
Lowest common ancestor