Question: There are ten errors in bstree.h and bstree.cpp. Some are syntax errors and some are run-time errors. Can you please help me find them and
There are ten errors in bstree.h and bstree.cpp.
Some are syntax errors and some are run-time errors.
Can you please help me find them and fix the erros?
// bstree.h file containing class for a Binary Search Tree
#pragma once
typedef int InfoType;
class BSTree
{
public:
BSTree() { tree = NULL; }
~BSTree();
bool Contains ( InfoType x ) const;
void Insert ( InfoType x ) { Insert ( tree, x ); }
bool Delete ( InfoType x ) { return Delete ( tree, x ); }
private:
struct TreeNode
{
InfoType info;
TreeNode *left, *right;
TreeNode( InfoType x ) { info = x; left = right = NULL; }
};
TreeNode tree;
void Insert ( TreeNode * & t, InfoType x );
bool Delete ( TreeNode * & t, InfoType x );
}
-------------------------------------------------------------
// bstree.cpp file containing bodies for BSTree class
#include "bstree.h"
#include
using namespace std;
bool BSTree::Contains( InfoType x ) const
{
TreeNode * p = tree;
while ( p->info != NULL )
{
if ( x == p->info )
return true;
if ( x < p->info )
p = p->left;
else
p = p->right;
}
return false;
}
void BSTree::Insert ( TreeNode * & t, InfoType x )
{
if ( t != NULL )
{
if ( x < t->info )
Insert ( t->left );
else
Insert ( t->right );
}
}
bool BSTree::Delete ( TreeNode * & t, InfoType x )
{
if ( t == NULL )
return false;
if ( x < t->info )
return Delete ( t->right, x );
if ( t->info < x )
return Delete ( t->left, x );
TreeNode * p = t;
if ( t->left == NULL )
t = t->right;
else if ( t->right == NULL )
t = t->left;
else
{
while ( p->right != NULL )
p = p->right;
t->info = p->info;
return Delete ( t->left, x );
}
return true;
}
BSTree::~BSTree()
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
