#include <iostream>
using namespace std;
class bst{
public:
int data;
bst *left,*right;
bst(int val){
data = val;
left = right = NULL;
}
};
bst* insert(bst* root, int val){
if(root==NULL){
return new bst(val);
}
if(val > root->data){
root->right = insert(root->right,val);
}
else{
root->left = insert(root->left,val);
}
return root;
}
void inorder(bst* root){
if(root!=NULL){
inorder(root->left);
cout<<root->data<<" ";
inorder(root->right);
}
}
int height_of_tree(bst* root){
if(root==NULL){
return -1;
}
int l = height_of_tree(root->left);
int r = height_of_tree(root->right);
if(l > r)
return (l+1);
else
return (r+1);
}
int main() {
bst* root = NULL;
int x;
while(cin>>x){
root = insert(root,x);
}
inorder(root);
int height = height_of_tree(root);
cout<<"\nHeight of tree : "<<height+1<<"\n";
return 0;
}
To embed this program on your website, copy the following code and paste it into your website's HTML: