0% found this document useful (0 votes)
32 views2 pages

4 Take+Input+Level+Wise

The document discusses binary tree nodes and methods to take input and print binary trees. It includes a BTNode class template to represent nodes, and functions to take input level-wise or recursively, and print the tree.

Uploaded by

harsh raj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views2 pages

4 Take+Input+Level+Wise

The document discusses binary tree nodes and methods to take input and print binary trees. It includes a BTNode class template to represent nodes, and functions to take input level-wise or recursively, and print the tree.

Uploaded by

harsh raj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include<iostream>

#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<<"Enter left child of "<<f->data<<endl;


int leftChildData;
cin>>leftChildData;
if(leftChildData != -1){
BTNode<int>* child = new BTNode<int>(leftChildData);
q.push(child);
f->left = child;
}
cout<<"Enter right child of "<<f->data<<endl;
int rightChildData;
cin>>rightChildData;
if(rightChildData != -1){
BTNode<int>* child = new BTNode<int>(rightChildData);
q.push(child);
f->right = child;
}
}
return root;
}

void printTree(BTNode<int>* root){


if(root==NULL){ /// base case
return;
}

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(){

/* BTNode<int>* root = new BTNode<int>(1);


BTNode<int>* n1 = new BTNode<int>(2);
BTNode<int>* n2 = new BTNode<int>(3);

root->left = n1;
root->right = n2;*/
BTNode<int>* root = takeInputLevelWise();
printTree(root);

delete root;
return 0;
}

You might also like