-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBinaryTree.cpp
More file actions
109 lines (101 loc) · 2.34 KB
/
BinaryTree.cpp
File metadata and controls
109 lines (101 loc) · 2.34 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
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <iostream>
using namespace std;
struct node
{
char data;
node* left;
node* right;
};
class BTree
{
private:
node* root;
public:
BTree()
{
root = nullptr;
}
node* createNode()
{
node* tmp = new node;
cout<<"Enter data for node: ";
cin>>tmp->data;
tmp->left = nullptr;
tmp->right = nullptr;
}
void insert(node* r, node* newNode)
{
if(r != nullptr)
{
char choice;
l1:
cout<<"Enter node to left(L) or right(R) of "<<r->data<<": ";
cin>>choice;
if(choice == 'L' || choice == 'l')
{
if(r->left == nullptr)
{
r->left = newNode;
return;
}
else
{
r = r->left;
goto l1;
}
}
else if(choice == 'R' || choice == 'r')
{
if(r->right == nullptr)
{
r->right = newNode;
return;
}
else
{
r = r->right;
goto l1;
}
}
else
{
cout<<"Wrong Value Entered";
}
}
}
void tree()
{
node* newNode = createNode();
if(root == nullptr)
{
root = newNode;
}
else
{
insert(root, newNode);
}
}
void preOrder(node* r)
{
if(r != nullptr)
{
cout<<r->data;
preOrder(r->left);
preOrder(r->right);
}
}
void preOrder()
{
preOrder(root);
}
};
int main()
{
BTree b1;
b1.tree();
b1.tree();
b1.tree();
b1.tree();
b1.preOrder();
return 0;
}