-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
96 lines (78 loc) · 1.67 KB
/
main.cpp
File metadata and controls
96 lines (78 loc) · 1.67 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <bits/stdc++.h>
using namespace std;
class BinaryTree
{
private:
struct Node
{
int value;
Node* left;
Node* right;
Node(int val)
{
value = val;
left = nullptr;
right = nullptr;
}
};
Node* root;
void populate(Node* node)
{
char choice;
cout<<"Do you enter the left of "<<node->value<<"?(y/n)";
cin>>choice;
if(choice == 'y' || choice == 'Y')
{
int val;
cout<<"Enter the value of the left of "<<node->value<<": ";
cin>>val;
node->left = new Node(val);
populate(node->left);
}
cout<<"Do you enter the right of "<<node->value<<"?(y/n)";
cin>>choice;
if(choice == 'y' || choice == 'Y')
{
int val;
cout<<"Enter the value of the right of "<<node->value<<": ";
cin>>val;
node->right = new Node(val);
populate(node->right);
}
}
void display(Node* node, string indent)
{
if(node == nullptr)
{
return;
}
cout<<indent<<node->value<<endl;
display(node->left, indent + "\t");
display(node->right, indent+"\t");
}
public:
BinaryTree()
{
root = nullptr;
}
void populate()
{
int val;
cout<<"Enter the root node: ";
cin>>val;
root = new Node(val);
populate(root);
}
void display()
{
cout<<"\nBinary Tree Structure:\n";
display(root, "");
}
};
int main()
{
BinaryTree tree;
tree.populate();
tree.display();
return 0;
}