I have difficultly understanding how to isolate the code enough to compile and test it but...I think I've made a breakthrough. There's still some compiling issues, (from the node itself now) and I'm having trouble understanding it, but I think I'm almost done. These are the four files from before, but the bs_treet files have been altered significantly. The test is, of course, the same.
~~~Node~~~
Code:
#ifndef BST_NODET_H
#define BST_NODET_H
#include <string>
#include <climits>
template <typename T>
class BSTNodeT {
public:
BSTNodeT();
BSTNodeT(T contents);
~BSTNodeT();
void SetContents(T contents);
void SetLeft(BSTNodeT node);
void SetRight(BSTNodeT node);
void IncrementCount();
void DecrementCount();
T GetContents() const;
BSTNodeT* GetLeft() const;
BSTNodeT* GetRight() const;
int GetCount();
private:
T contents_;
BSTNodeT<T>* left_;
BSTNodeT<T>* right_;
int count_;
};
#endif /* BST_NODET_H */
Code:
#include "bst_nodet.h"
#include <iostream>
#include <string>
#include <sstream>
using std::string;
using std::stringstream;
BSTNodeT::BSTNodeT() {
left_ = NULL;
right_ = NULL;
contents_ = T();
count_ = 0;
}
BSTNodeT::BSTNodeT(T contents) {
left_ = NULL;
right_ = NULL;
contents_ = contents;
count_ = 0;
}
BSTNodeT::~BSTNodeT() {
left_ = NULL;
right_ = NULL;
}
void BSTNodeT::SetContents(T contents) {
contents_ = contents;
}
void BSTNodeT::SetLeft(BSTNodeT node) {
left_ = node;
}
void BSTNodeT::SetRight(BSTNodeT node) {
right_ = node;
}
void BSTNodeT::IncrementCount() {
count_++;
}
void BSTNodeT::DecrementCount() {
count_--;
}
T BSTNodeT::GetContents() const {
return contents_;
}
BSTNodeT* BSTNodeT::GetLeft() const {
return left_;
}
BSTNodeT* BSTNodeT::GetRight() const {
return right_;
}
int BSTNodeT::GetCount() {
return count_;
}
~~~Tree~~~
Code:
#ifndef BS_TREET_H
#define BS_TREET_H
#include <string>
#include <climits>
#include "bs_nodet.h"
template <typename T>
class BSTreeT {
public:
BSTreeT();
~BSTreeT();
int GetSize() const;
void Clear();
int Insert(T i);
bool Exists (T i);
int Remove(T i);
T Get (T i);
std::string ToStringForwards() const;
std::string ToStringBackwards() const;
private:
BSTNodeT<T>* root_;
unsigned int size_;
void Clear (BSTNodeT<T>*& node);
int Insert (T i, BSTNodeT<T>*& node);
bool Exists (T i, BSTNodeT<T>* node);
int Remove (T i, BSTNodeT<T>* node);
T Get (T i, BSTNodeT<T>* node);
std::string ToStringForwards (BSTNodeT<T>* node) const;
std::string ToStringBackwards (BSTNodeT<T>* node) const;
};
#endif /* BS_TREET_H */
Code:
#include "bs_treet.h"
#include <iostream>
#include <cmath>
#include <string>
#include <cctype>
#include <sstream>
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::stringstream;
template <typename T>
BSTreeT<T>::BSTreeT() {
root_ = NULL;
size_ = 0;
}
template <typename T>
BSTreeT<T>::~BSTreeT() {
Clear();
}
template <typename T>
int BSTreeT<T>::GetSize() const {
return size_;
}
template <typename T>
void BSTreeT<T>::Clear() {
Clear(root_);
size_ = 0;
}
template <typename T>
int BSTreeT<T>::Insert(T i) {
return Insert(i, root_);
}
template <typename T>
bool BSTreeT<T>::Exists (T i) {
return Exists(i, root_);
}
template <typename T>
int BSTreeT<T>::Remove(T i) {
return Remove(i, root_);
}
template <typename T>
T BSTreeT<T>::Get (T i) {
return Get(i, root_);
}
template <typename T>
std::string BSTreeT<T>::ToStringForwards() const {
return ToStringForwards(root_);
}
template <typename T>
std::string BSTreeT<T>::ToStringBackwards() const {
return ToStringBackwards(root_);
}
template <typename T>
void BSTreeT<T>::Clear (BSTNodeT<T>*& node) {
if (node != NULL) {
Clear(node->GetLeft());
Clear(node->GetRight());
delete node;
node = NULL;
}
}
template <typename T>
int BSTreeT<T>::Insert (T i, BSTNodeT<T>*& node) {
int count = 0;
if (node == NULL) {
node = new BSTNodeT<T>(i);
size_++;
count++;
} else if (node->GetContents() > i) {
return Insert(i, node->GetLeft());
} else if (node->GetContents() < i) {
return Insert(i, node->GetRight());
} else {
node->IncrementCount();
count++;
}
return count;
}
template <typename T>
bool BSTreeT<T>::Exists (T i, BSTNodeT<T>* node) {
if (node != NULL) {
Exists(node->GetLeft());
if (node == i) {
return true;
}
Exists(node->GetRight());
}
return false;
}
template <typename T>
int BSTreeT<T>::Remove (T i, BSTNodeT<T>* node) {
if (node != NULL) {
Remove(node->GetLeft());
if (node == i) {
if (node == 1) {
delete node;
node = NULL;
return 0;
} else {
node->DecrementCount();
return node;
}
}
Remove(node->GetRight());
}
return -1;
}
template <typename T>
T BSTreeT<T>::Get (T i, BSTNodeT<T>* node) {
if (node != NULL) {
Get(node->GetLeft());
if (node == i) {
return node;
}
Get(node->GetRight());
}
return NULL;
}
template <typename T>
string BSTreeT<T>::ToStringForwards (BSTNodeT<T>* node) const {
if (node != NULL) {
std::ostringstream ss;
ss << ToStringForwards(node->GetLeft());
ss << node->GetContents() << " ";
ss << ToStringForwards(node->GetRight());
return ss.str();
}
return "";
}
template <typename T>
string BSTreeT<T>::ToStringBackwards (BSTNodeT<T>* node) const {
if (node != NULL) {
std::ostringstream ss;
ss << ToStringBackwards(node->GetRight());
ss << node->GetContents() << " ";
ss << ToStringBackwards(node->GetLeft());
return ss.str();
}
return "";
}
~~~Compiler Error~~~
Code:
$ g++ bs_treet.cpp assignment_5_unit_test.cpp
assignment_5_unit_test.cpp: In function ‘void UnitTest()’:
assignment_5_unit_test.cpp:50:25: error: invalid conversion from ‘BSTNodeT<int>*’ to ‘int’ [-fpermissive]
inode1.SetLeft(&inode2);
^
In file included from bs_treet.h:5:0,
from assignment_5_unit_test.cpp:10:
bs_nodet.h:10:3: note: initializing argument 1 of ‘BSTNodeT<T>::BSTNodeT(T) [with T = int]’
BSTNodeT(T contents);
^
assignment_5_unit_test.cpp:52:26: error: invalid conversion from ‘BSTNodeT<int>*’ to ‘int’ [-fpermissive]
inode1.SetRight(&inode3);
^
In file included from bs_treet.h:5:0,
from assignment_5_unit_test.cpp:10:
bs_nodet.h:10:3: note: initializing argument 1 of ‘BSTNodeT<T>::BSTNodeT(T) [with T = int]’
BSTNodeT(T contents);
^
assignment_5_unit_test.cpp:69:25: error: invalid conversion from ‘BSTNodeT<char>*’ to ‘char’ [-fpermissive]
cnode1.SetLeft(&cnode2);
^
In file included from bs_treet.h:5:0,
from assignment_5_unit_test.cpp:10:
bs_nodet.h:10:3: note: initializing argument 1 of ‘BSTNodeT<T>::BSTNodeT(T) [with T = char]’
BSTNodeT(T contents);
^
assignment_5_unit_test.cpp:71:26: error: invalid conversion from ‘BSTNodeT<char>*’ to ‘char’ [-fpermissive]
cnode1.SetRight(&cnode3);
^
In file included from bs_treet.h:5:0,
from assignment_5_unit_test.cpp:10:
bs_nodet.h:10:3: note: initializing argument 1 of ‘BSTNodeT<T>::BSTNodeT(T) [with T = char]’
BSTNodeT(T contents);
^
assignment_5_unit_test.cpp:89:25: error: no matching function for call to ‘BSTNodeT<std::basic_string<char> >::SetLeft(BSTNodeT<std::basic_string<char> >*)’
snode1.SetLeft(&snode2);
^
assignment_5_unit_test.cpp:89:25: note: candidate is:
In file included from bs_treet.h:5:0,
from assignment_5_unit_test.cpp:10:
bs_nodet.h:13:8: note: void BSTNodeT<T>::SetLeft(BSTNodeT<T>) [with T = std::basic_string<char>]
void SetLeft(BSTNodeT node);
^
bs_nodet.h:13:8: note: no known conversion for argument 1 from ‘BSTNodeT<std::basic_string<char> >*’ to ‘BSTNodeT<std::basic_string<char> >’
assignment_5_unit_test.cpp:91:26: error: no matching function for call to ‘BSTNodeT<std::basic_string<char> >::SetRight(BSTNodeT<std::basic_string<char> >*)’
snode1.SetRight(&snode3);
^
assignment_5_unit_test.cpp:91:26: note: candidate is:
In file included from bs_treet.h:5:0,
from assignment_5_unit_test.cpp:10:
bs_nodet.h:14:8: note: void BSTNodeT<T>::SetRight(BSTNodeT<T>) [with T = std::basic_string<char>]
void SetRight(BSTNodeT node);
^
bs_nodet.h:14:8: note: no known conversion for argument 1 from ‘BSTNodeT<std::basic_string<char> >*’ to ‘BSTNodeT<std::basic_string<char> >’
assignment_5_unit_test.cpp:143:29: error: invalid conversion from ‘int’ to ‘BSTNodeT<int>*’ [-fpermissive]
tree_pointer = tree.Get(50);
^
assignment_5_unit_test.cpp:146:28: error: invalid conversion from ‘int’ to ‘BSTNodeT<int>*’ [-fpermissive]
tree_pointer = tree.Get(0);
^