#include<iostream> using namespace std; class node
{
public: int data; node *left, *right; node() { left=right=NULL; } node(int val) { left=right=NULL; data=val; }
};
class bst
{
private: node *root; void insertNode(node *&rootptr, node *pnew); void deleteNode(node *&root, int delval); int least(node *rootptr); int max(node *rootptr); void pre(node *rootptr); void post(node *rootptr); void in(node *rootptr); int countinternal(node *rootptr, int &count); void printTree(node *p, int level); int search(node *rootptr, int data);
public: bst(); void callsearch(int data); void insertBST(int datain); void deleteBST(int data); void displayin(); void displaypost(); void displaypre(); void count(); void print(); void leastele(); void maxele();
};
bst::bst()
{
root=NULL;
}
void bst::print()
{
printTree(root, 0);
};
void bst::callsearch(int data)
{
search(root, data);
}
int bst::search(node *rootptr, int data)
{
if(rootptr==NULL) { cout<<"Data not found"; return 0; } else if(rootptr->data==data) cout<<"Element found"<<endl; else if(data<rootptr->data) search(rootptr->left, data); else search(rootptr->right, data);
}
void bst::count()
{
int c=0; cout<<"The number of internal nodes is: "<<countinternal(root, c)<<endl;
}
int bst::countinternal(node *rootptr,int &count)
{
if(rootptr!=NULL) { countinternal(rootptr->left, count); if(rootptr->right!=NULL || rootptr->left!=NULL) count++; countinternal(rootptr->right, count); return count; }
}
void bst::printTree(node *p, int level)
{
if(p!=NULL) { printTree(p->right, level+1); for(int i=0;i<level;i++) cout<<" "; cout<<level<<":"<<p->data<<endl; printTree(p->left, level+1); }
};