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 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
|
/*
Write C++ code for a class whose objects are binary search trees,
where the data items have type integer.
The class must contain a methods "insert", "find" (which returns a Boolean),
"delete" as well as method "in-order print" and "pre-order print" which output
the data in the binary search tree in in-order and post-order respectively.
Requirement:
You should design your own BST data structure.
Your program should read a text file input.txt as input.
The input.txt contains a list of operations that your program should be able to handle,
below are the definitions of the corresponding commands:
N insert a node
F find
D delete
I inorder output
P preorder output
For the example input file which you can find in the homework folder as:
I am expecting your output to be:
Inorder 2 3 5 7
Illegal function
Preorder 5 3 2 7
7 found
4 not found
7 not found
Inorder 2 3 5
Preorder 5 3 2
Note that your program should be able to detect any illegal command other than the five legal commands.
*/
/*input.txt
N 5
N 3
N 7
N 3
N 2
I
A 4
P
F 7
F 4
D 7
F 7
I
P
*/
#include <fstream>
#include <iostream>
#include <sstream>
#include <cstdlib>
#include <string>
#include <algorithm>
#include <iterator>
using namespace std;
struct node
{
int info;
struct node *left;
struct node *right;
}*root;
class BST
{
public:
bool find(int, node **, node **);
bool insert(int);
void del(int);
void preorder(node *);
void inorder(node *);
BST()
{
root = NULL;
}
};
int main() {
ifstream myfile( "input.txt" );
string line;
int num;
string temp_str = "default";
BST bst;
node *temp, *par, *loc;
while (!myfile.eof()) {
getline(myfile, line);
string fun = line.substr(0, 1);
if (line. substr(1,2) != "") {
temp_str = line. substr(1,2);
}
if( temp_str != "default") {
int num = atoi (temp_str.c_str());
}
if (fun != "N" && fun != "I" && fun != "P" && fun != "D" && fun != "F") {
fun = "Illegal function!";
} else {
if (fun == "N") {
bst.insert(root, temp);
}
if (fun == "I") {
bst.inorder(root);
}
if (fun == "P") {
bst.preorder(root);
}
if (fun == "D") {
bst.del(num);
}
if (fun == "F") {
bst.find(num, par, loc);
}
}
}
return 0;
}
//Find an element in the tree
bool BST::find(int item, node **par, node **loc)
{
node *ptr, *ptrsave;
if (root == NULL)
{
*loc = NULL;
*par = NULL;
return false;
}
if (item == root->info)
{
*loc = root;
*par = NULL;
return true;
}
if (item < root->info)
ptr = root->left;
else
ptr = root->right;
ptrsave = root;
while (ptr != NULL)
{
if (item == ptr->info)
{
*loc = ptr;
*par = ptrsave;
return true;
}
ptrsave = ptr;
if (item < ptr->info)
ptr = ptr->left;
else
ptr = ptr->right;
}
*loc = NULL;
*par = ptrsave;
}
//Inserting an element to the tree
bool BST::insert(node *tree, node *newnode)
{
if (root == NULL)
{
root = new node;
root->info = newnode->info;
root->left = NULL;
root->right = NULL;
cout<<"Root Node is Added"<<endl;
return true;
}
if (tree->info == newnode->info)
{
cout<<"Element already in the tree"<<endl;
return false;
}
if (tree->info > newnode->info)
{
if (tree->left != NULL)
{
insert(tree->left, newnode);
}
else
{
tree->left = newnode;
(tree->left)->left = NULL;
(tree->left)->right = NULL;
cout<<"Node Added To Left"<<endl;
returntrue;
}
}
else
{
if (tree->right != NULL)
{
insert(tree->right, newnode);
}
else
{
tree->right = newnode;
(tree->right)->left = NULL;
(tree->right)->right = NULL;
cout<<"Node Added To Right"<<endl;
return true;
}
}
}
//Delete an element from the tree
void BST::del(int item)
{
node *parent, *location;
if (root == NULL)
{
cout<<"Tree empty"<<endl;
return;
}
find(item, &parent, &location);
if (location == NULL)
{
cout<<"Item not present in tree"<<endl;
return;
}
if (location->left == NULL && location->right == NULL)
case_a(parent, location);
if (location->left != NULL && location->right == NULL)
case_b(parent, location);
if (location->left == NULL && location->right != NULL)
case_b(parent, location);
if (location->left != NULL && location->right != NULL)
case_c(parent, location);
free(location);
}
// PreOrder output
void BST::preorder(node *ptr)
{
if (root == NULL)
{
cout<<"Tree is empty"<<endl;
return;
}
if (ptr != NULL)
{
cout<<ptr->info<<" ";
preorder(ptr->left);
preorder(ptr->right);
}
}
// InOrder output
void BST::inorder(node *ptr)
{
if (root == NULL)
{
cout<<"Tree is empty"<<endl;
return;
}
if (ptr != NULL)
{
inorder(ptr->left);
cout<<ptr->info<<" ";
inorder(ptr->right);
}
}
/*
* Case A
*/
void BST::case_a(node *par, node *loc )
{
if (par == NULL)
{
root = NULL;
}
else
{
if (loc == par->left)
par->left = NULL;
else
par->right = NULL;
}
}
/*
* Case B
*/
void BST::case_b(node *par, node *loc)
{
node *child;
if (loc->left != NULL)
child = loc->left;
else
child = loc->right;
if (par == NULL)
{
root = child;
}
else
{
if (loc == par->left)
par->left = child;
else
par->right = child;
}
}
/*
* Case C
*/
void BST::case_c(node *par, node *loc)
{
node *ptr, *ptrsave, *suc, *parsuc;
ptrsave = loc;
ptr = loc->right;
while (ptr->left != NULL)
{
ptrsave = ptr;
ptr = ptr->left;
}
suc = ptr;
parsuc = ptrsave;
if (suc->left == NULL && suc->right == NULL)
case_a(parsuc, suc);
else
case_b(parsuc, suc);
if (par == NULL)
{
root = suc;
}
else
{
if (loc == par->left)
par->left = suc;
else
par->right = suc;
}
suc->left = loc->left;
suc->right = loc->right;
}
|