www2csuidahoedubrucebcs 121Assignment Papillon 1973 Steve Mc
Solution
#include <iostream>
#include <math.h>
using namespace std;
template <class T>
struct nded {
T value;
nded *lft;
nded *rght;
nded(T vl) {
this->value = vl;
}
nded(T vl, nded<T> lft, nded<T> rght) {
this->value = vl;
this->lft = lft;
this->rght = rght;
}
};
template <class T>
class bst {
private:
nded<T> *ruut;
void addHelper(nded<T> *ruut, T vl) {
if (ruut->value > vl) {
if (!ruut->lft) {
ruut->lft = new nded<T>(vl);
} else {
addHelper(ruut->lft, vl);
}
} else {
if (!ruut->rght) {
ruut->rght = new nded<T>(vl);
} else {
addHelper(ruut->rght, vl);
}
}
}
void printHelper(nded<T> *ruut) {
if (!ruut) return;
printHelper(ruut->lft);
cout<<ruut->value<<\' \';
printHelper(ruut->rght);
}
int nodesCountHelper(nded<T> *ruut) {
if (!ruut) return 0;
else return 1 + nodesCountHelper(ruut->lft) + nodesCountHelper(ruut->rght);
}
int heightHelper(nded<T> *ruut) {
if (!ruut) return 0;
else return 1 + max(heightHelper(ruut->lft), heightHelper(ruut->rght));
}
void printMaxPathHelper(nded<T> *ruut) {
if (!ruut) return;
cout<<ruut->value<<\' \';
if (heightHelper(ruut->lft) > heightHelper(ruut->rght)) {
printMaxPathHelper(ruut->lft);
} else {
printMaxPathHelper(ruut->rght);
}
}
bool deleteValueHelper(nded<T>* parent, nded<T>* current, T value) {
if (!current) return false;
if (current->value == value) {
if (current->lft == NULL || current->rght == NULL) {
nded<T>* temp = current->lft;
if (current->rght) temp = current->rght;
if (parent) {
if (parent->lft == current) {
parent->lft = temp;
} else {
parent->rght = temp;
}
} else {
this->ruut = temp;
}
} else {
nded<T>* validSubs = current->rght;
while (validSubs->lft) {
validSubs = validSubs->lft;
}
T temp = current->value;
current->value = validSubs->value;
validSubs->value = temp;
return deleteValueHelper(current, current->rght, temp);
}
delete current;
return true;
}
return deleteValueHelper(current, current->lft, value) ||
deleteValueHelper(current, current->rght, value);
}
public:
void add(T vl) {
if (ruut) {
this->addHelper(ruut, vl);
} else {
ruut = new nded<T>(vl);
}
}
void print() {
printHelper(this->ruut);
}
int nodesCount() {
return nodesCountHelper(ruut);
}
int height() {
return heightHelper(this->ruut);
}
void printMaxPath() {
printMaxPathHelper(this->ruut);
}
bool deleteValue(T value) {
return this->deleteValueHelper(NULL, this->ruut, value);
}
};
int main() {
bst<int> *bst = new bst<int>();
bst->add(11);
bst->add(1);
bst->add(6);
bst->add(-1);
bst->add(-10);
bst->add(100);
bst->print();
cout<<endl;
cout<<\"Nodes count: \"<<bst->nodesCount();
cout<<endl;
cout<<\"Height: \"<<bst->height();
cout<<endl;
cout<<\"Max path: \";
bst->printMaxPath();
cout<<endl;
bst->deleteValue(11);
cout<<\"11 removed: \";
bst->print();
cout<<endl;
cout<<\"1 removed: \";
bst->deleteValue(1);
bst->print();
cout<<endl;
cout<<\"-1 removed: \";
bst->deleteValue(-1);
bst->print();
cout<<endl;
cout<<\"6 removed: \";
bst->deleteValue(6);
bst->print();
cout<<endl;
cout<<\"-10 removed: \";
bst->deleteValue(-10);
bst->print();
cout<<endl;
cout<<\"100 removed: \";
bst->deleteValue(100);
bst->print();
cout<<endl;
return 0;
}


