Question: Set Class in C++ Write an implementation of the set class, with associated iterators using a binary search tree. Add to each node a link
Set Class in C++ Write an implementation of the set class, with associated iterators using a binary search tree. Add to each node a link to the next smallest and next largest node. Note: To make your code simpler, add a header and tail node which are not part of the binary search tree but help make the linked list part of the code simpler. I use the following code but I get the following errors in Visual Studio 2013:
1.) error C2065: 'node' : undeclared identifier
2.) warning C4346: 'Tree
3.) error C2182: 'freeMemory' : illegal use of type 'void'
4.) error C2470: 'Tree
5.) error C2072: 'Tree
I need help fixing the errors. Thanks!
#include
template
class Tree
{
// Internal class which stores only Node related information.
struct TreeNode
{
T data;
TreeNode * left;
TreeNode * right;
TreeNode(T val):data(val),left(NULL),right(NULL)
{
}
};
TreeNode * root;
void print(TreeNode*);
void freeMemory(TreeNode*);
public:
Tree();
~Tree();
void insert(T);
void print();
};
template
Tree
template
Tree
{
freeMemory(root);
}
template
void Tree
{
if (node==NULL)
return;
if (node->left)
freeMemory(node->left);
if (root->right)
freeMemory(node->right);
delete node;
}
template
//make it return value?
void Tree
{
TreeNode * treeNode = NULL;
try
{
treeNode = new TreeNode(val); // handle exception necessary?
} catch (std::bad_alloc &exception)
{
std::cerr << "bad_alloc caught: " << exception.what() << std::endl;
EXIT_FAILURE;
}
TreeNode *temp=NULL;
TreeNode *prev=NULL;
temp = root;
while(temp)
{
prev = temp;
if (temp->data < treeNode->data)
temp = temp->right;
else
temp = temp->left;
}
if (prev==NULL)
root = treeNode;
else
{
if (prev->data
prev->right = treeNode; // use setter function?
else
prev->left = treeNode;
}
}
template
void Tree
{
if (root==NULL)
return ;
print(root->left);
std::cout << root->data << std::endl;
print(root->right);
}
template
void Tree
{
print(root);
}
int main()
{
Tree
tree.insert(14);
tree.insert(12);
tree.insert(6);
tree.insert(17);
tree.insert(8);
tree.print();
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
