-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbinarytree.cpp
199 lines (199 loc) · 4.71 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
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
#include <iostream>
#include <cstdlib>
using namespace std;
struct tree {
int info;
tree *Left, *Right;
};
tree* root;
class Binary_tree {
public:
Binary_tree();
void insert1(int);
tree* insert2(tree*, tree*);
void Delete(int);
void pretrav(tree*);
void intrav(tree*);
void posttrav(tree*);
};
Binary_tree::Binary_tree()
{
root = NULL;
}
tree* Binary_tree::insert2(tree* temp, tree* newnode)
{
if (temp == NULL) {
temp = newnode;
}
else if (temp->info < newnode->info) {
insert2(temp->Right, newnode);
if (temp->Right == NULL)
temp->Right = newnode;
}
else {
insert2(temp->Left, newnode);
if (temp->Left == NULL)
temp->Left = newnode;
}
return temp;
}
void Binary_tree::insert1(int n)
{
tree *temp = root, *newnode;
newnode = new tree;
newnode->Left = NULL;
newnode->Right = NULL;
newnode->info = n;
root = insert2(temp, newnode);
}
void Binary_tree::pretrav(tree* t = root)
{
if (root == NULL) {
cout << "Nothing to display";
}
else if (t != NULL) {
cout << t->info << " ";
pretrav(t->Left);
pretrav(t->Right);
}
}
void Binary_tree::intrav(tree* t = root)
{
if (root == NULL) {
cout << "Nothing to display";
}
else if (t != NULL) {
intrav(t->Left);
cout << t->info << " ";
intrav(t->Right);
}
}
void Binary_tree::posttrav(tree* t = root)
{
if (root == NULL) {
cout << "Nothing to display";
}
else if (t != NULL) {
posttrav(t->Left);
posttrav(t->Right);
cout << t->info << " ";
}
}
void Binary_tree::Delete(int key)
{
tree *temp = root, *parent = root, *marker;
if (temp == NULL)
cout << "The tree is empty" << endl;
else {
while (temp != NULL && temp->info != key) {
parent = temp;
if (temp->info < key) {
temp = temp->Right;
}
else {
temp = temp->Left;
}
}
}
marker = temp;
if (temp == NULL)
cout << "No node present";
else if (temp == root) {
if (temp->Right == NULL && temp->Left == NULL) {
root = NULL;
}
else if (temp->Left == NULL) {
root = temp->Right;
}
else if (temp->Right == NULL) {
root = temp->Left;
}
else {
tree* temp1;
temp1 = temp->Right;
while (temp1->Left != NULL) {
temp = temp1;
temp1 = temp1->Left;
}
if (temp1 != temp->Right) {
temp->Left = temp1->Right;
temp1->Right = root->Right;
}
temp1->Left = root->Left;
root = temp1;
}
}
else {
if (temp->Right == NULL && temp->Left == NULL) {
if (parent->Right == temp)
parent->Right = NULL;
else
parent->Left = NULL;
}
else if (temp->Left == NULL) {
if (parent->Right == temp)
parent->Right = temp->Right;
else
parent->Left = temp->Right;
}
else if (temp->Right == NULL) {
if (parent->Right == temp)
parent->Right = temp->Left;
else
parent->Left = temp->Left;
}
else {
tree* temp1;
parent = temp;
temp1 = temp->Right;
while (temp1->Left != NULL) {
parent = temp1;
temp1 = temp1->Left;
}
if (temp1 != temp->Right) {
temp->Left = temp1->Right;
temp1->Right = parent->Right;
}
temp1->Left = parent->Left;
parent = temp1;
}
}
delete marker;
}
int main()
{
Binary_tree bt;
int choice, n, key;
while (1) {
cout << "\n\t1. Insert\n\t2. Delete\n\t3. Preorder Traversal\n\t4. Inorder Treversal\n\t5. Postorder Traversal\n\t6. Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Enter item: ";
cin >> n;
bt.insert1(n);
break;
case 2:
cout << "Enter element to delete: ";
cin >> key;
bt.Delete(key);
break;
case 3:
cout << endl;
bt.pretrav();
break;
case 4:
cout << endl;
bt.intrav();
break;
case 5:
cout << endl;
bt.posttrav();
break;
case 6:
exit(0);
}
}
return 0;
}