Professional Documents
Culture Documents
#include<queue>
using namespace std;
template<typename T>
class BTNode{
public:
T data;
BTNode* left;
BTNode* right;
BTNode(T data){
this->data = data;
left = NULL;
right = NULL;
}
~BTNode(){
delete left;
delete right;
}
};
BTNode<int>* takeInput(){
int rootData;
cout<<"Enter data"<<endl;
cin>>rootData;
if(rootData==-1){
return NULL;
}
BTNode<int>* root = new BTNode<int>(rootData);
root->left = takeInput();
root->right = takeInput();
return root;
}
BTNode<int>* takeInputLevelWise(){
int rootData;
cout<<"Enter root data"<<endl;
cin>>rootData;
BTNode<int>* root = new BTNode<int>(rootData);
queue<BTNode<int>*> q;
q.push(root);
while(!q.empty()){
BTNode<int>* f = q.front();
q.pop();
cout<<root->data<<": ";
if(root->left!=NULL){
cout<<"L"<<root->left->data;
}
if(root->right!=NULL){
cout<<"R"<<root->right->data;
}
cout<<endl;
printTree(root->left);
printTree(root->right);
}
/// 1 2 3 4 5 6 7 -1 -1 -1 -1 8 9 -1 -1 -1 -1 -1 -1
int main(){
root->left = n1;
root->right = n2;*/
BTNode<int>* root = takeInputLevelWise();
printTree(root);
delete root;
return 0;
}