-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryTree.cpp
More file actions
217 lines (214 loc) · 5.6 KB
/
BinaryTree.cpp
File metadata and controls
217 lines (214 loc) · 5.6 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include<iostream>
using namespace std;
class leaf
{
public:
int data;
leaf *left,*right;
};
class tree
{
leaf *root,*parent,*curr;
public:
tree()
{
root=parent=curr=NULL;
}
leaf *createleaf(int n)
{
leaf *temp=new leaf;
temp->data=n;
temp->left=temp->right=NULL;
return temp;
}
void insert()
{
leaf *temp;
curr=root;
int a;
cout<<"Enter data"<<endl;
cin>>a;
temp=createleaf(a);
if(root==NULL)
{
root=temp;
cout<<"Root added"<<endl;
}
else
{
while(curr!=NULL)
{
parent=curr;
if(temp->data > curr->data)
curr=curr->right;
else if(temp->data < curr->data)
curr=curr->left;
else
{
cout<<"DUPLICATES NOT ALLOWED"<<endl;
return;
}
}
if(temp->data >parent->data)
{
cout<<"Element inserted in right of "<<parent->data<<endl;
parent->right=temp;
}
else
{
cout<<"Element inserted in left of "<<parent->data<<endl;
parent->left=temp;
}
cout<<"Element added"<<endl;
}
}
void inorder(leaf* &temp)
{
if(temp==NULL)
{
return;
}
inorder(temp->left);
cout<<temp->data<<" ";
inorder(temp->right);
}
void preorder(leaf* &temp)
{
if(temp==NULL)
{
return;
}
cout<<temp->data<<" ";
preorder(temp->left);
preorder(temp->right);
}
void postorder(leaf* &temp)
{
if(temp==NULL)
{
return;
}
postorder(temp->left);
postorder(temp->right);
cout<<temp->data<<" ";
}
void traversal()
{
cout<<"Inorder Traversal"<<endl;
cout<<"----------------------------------------------------"<<endl;
inorder(root);
cout<<"\n----------------------------------------------------"<<endl;
cout<<"Preorder Traversal"<<endl;
cout<<"----------------------------------------------------"<<endl;
preorder(root);
cout<<"\n----------------------------------------------------"<<endl;
cout<<"Postorder Traversal"<<endl;
cout<<"----------------------------------------------------"<<endl;
postorder(root);
cout<<"\n----------------------------------------------------"<<endl;
}
void deleteleaf(leaf* &curr,leaf* &parent)
{
if(curr->data < parent->data)
parent->left=NULL;
else
parent->right=NULL;
cout<<curr->data<<" element deleted"<<endl;
delete curr;
}
void deletehavingone(leaf* &curr,leaf* &parent)
{
leaf* child;
if(curr->left==NULL)
child=curr->right;
else
child=curr->left;
if(parent->right==curr)
parent->right=child;
else
parent->left=child;
cout<<curr->data<<" deleted form tree"<<endl;
delete curr;
}
void deletehavingboth(leaf* &curr,leaf* &parent)
{
leaf* inos;
inos=curr->right;
parent=curr; //if curr=ROOT & parent=NULL
while(inos->left!=NULL)
{
parent=inos;
inos=inos->left;
}
curr->data=inos->data;
if(inos->left==NULL && inos->right==NULL)
{
deleteleaf(inos,parent);
}
else
deletehavingone(inos,parent);
}
void deleteit()
{
int num;
cout<<"Enter element to be deleted"<<endl;
cin>>num;
curr=root;
if(root==NULL)
{
cout<<"EMPTY TREE";
}
else
{
while(curr != NULL && num != curr->data)
{
parent = curr;
if(num < curr->data)
curr = curr->left;
else if(num > curr->data)
curr = curr->right;
}
if(curr==root && curr->left==NULL && curr->right==NULL)
{
root=NULL;
delete curr;
cout<<"Root deleted"<<endl;
}else if(curr==NULL)
{
cout<<"Element NOT FOUND"<<endl;
return;
}
else if(curr->left==NULL && curr->right==NULL)
deleteleaf(curr,parent);
else if(curr->left!=NULL && curr->right!=NULL)
deletehavingboth(curr,parent);
else
deletehavingone(curr,parent);
}
}
};
main()
{
tree bt;
int opn;
do{
cout<<"1. Insert\n2. Delete\n3. Display\n0. EXIT"<<endl;
cin>>opn;
switch(opn){
case 1:
bt.insert();
break;
case 2:
bt.deleteit();
break;
case 3:
bt.traversal();
break;
case 0:
break;
default:
cout<<"INVALID COMMAND"<<endl;
break;
}
}while(opn!=0);
}