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::TreeNode' : dependent name is not a type

3.) error C2182: 'freeMemory' : illegal use of type 'void'

4.) error C2470: 'Tree::freeMemory' : looks like a function deffinition, but there is no parameter list; skipping apparent body

5.) error C2072: 'Tree::freeMemory' : initialization of a function

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::Tree():root(NULL){}

template

Tree::~Tree()

{

freeMemory(root);

}

template

void Tree::freeMemory(Tree::TreeNode *node)

{

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::insert(T val)

{

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->datadata)

prev->right = treeNode; // use setter function?

else

prev->left = treeNode;

}

}

template

void Tree::print(TreeNode *root)

{

if (root==NULL)

return ;

print(root->left);

std::cout << root->data << std::endl;

print(root->right);

}

template

void Tree::print()

{

print(root);

}

int main()

{

Tree 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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!