forked from kapoorp99/hacktoberfest2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
binarytree.cpp
61 lines (57 loc) · 1.08 KB
/
binarytree.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <bits/stdc++.h>
using namespace std;
template <typename T>
class BinaryTreeNode
{
public :
T data;
BinaryTreeNode* left;
BinaryTreeNode* right;
BinaryTreeNode(T a)
{
data=a;
left=NULL;
right=NULL;
}
};
void PrintTree(BinaryTreeNode<int>* root)
{
if(root==NULL)
{
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);
}
BinaryTreeNode<int>* TakeInput()
{
int rootdata;
cout<<"enter root data"<<endl;
cin>>rootdata;
if(rootdata==-1)
{
return NULL;
}
BinaryTreeNode<int>* root=new BinaryTreeNode<int>(rootdata);
BinaryTreeNode<int>* leftchild=TakeInput();
BinaryTreeNode<int>* rightchild=TakeInput();
root->left=leftchild;
root->right=rightchild;
return root;
}
int main()
{
BinaryTreeNode<int>* root=TakeInput();
PrintTree(root);
return 0;
}