I need someone to create the Delete Node Code For me in this binary search tree.
ID: 3876031 • Letter: I
Question
I need someone to create the Delete Node Code For me in this binary search tree. It is the only missing puzzle from my project and I spent 3 days thinking about it but couldn't come up with a solution.
*
*
*
*
This is the missing piece
*
*
*
template
inline void BST::DeleteItem(Node*& pNode, const DataType & SearchItem)
{
}
template
inline void BST::DeleteNodeItem(Node*& pNode)
{
}
*
*
*
*
*
*
And this the full code of the binary search tree that i wrote
*
*
*
*
*
home / study / engineering / computer science / computer science questions and answers / i have this bst #ifndef bst_h #define bst_h #include #include #include template struct node ...
Question: I have this BST #ifndef BST_H #define BST_H #include #include #include template struct Node { Nod...
I have this BST
#ifndef BST_H
#define BST_H
#include
#include
#include
template
struct Node
{
Node();
DataType Item; //assumes a copy constructor has been written for a DataType
Node* Left;
Node* Right;
};
template
Node::Node()
{
Left = NULL;
Right = NULL;
}
template
class BST
{
typedef void(*FunctionType)(DataType& Item);
public:
BST();
~BST();
bool IsEmpty();
void Insert(const DataType& NewItem);
void Delete(const DataType& SearchItem);
bool Retrieve(const DataType& SearchItem, DataType& Result);
void PreorderTraverse(FunctionType Visit);
void InorderTraverse(FunctionType Visit);
void PostorderTraverse(FunctionType Visit);
private:
Node* Root;
void Destroy(Node*& pNode); //called in the destructor
void InsertItem(Node*& pNode, const DataType& NewItem);
void DeleteItem(Node*& pNode, const DataType& SearchItem);
void DeleteNodeItem(Node*& pNode);
void DeleteLeftMost(Node*& pNode, DataType& Item);
bool RetrieveItem(Node* pNode, const DataType& SearchItem, DataType& Item);
void Preorder(Node* pNode, FunctionType Visit);
void Inorder(Node* pNode, FunctionType Visit);
void Postorder(Node* pNode, FunctionType Visit);
};
template
BST::BST()
{
Root = NULL;
}
template
BST::~BST()
{
Destroy(Root);
}
template
bool BST::IsEmpty()
{
return (Root == NULL);
}
template
void BST::Insert(const DataType& NewItem)
{
InsertItem(Root, NewItem);
}
template
void BST::Delete(const DataType& SearchItem)
{
DeleteItem(Root, SearchItem);
}
template
bool BST::Retrieve(const DataType& SearchItem, DataType& Result)
{
return RetrieveItem(Root, SearchItem, Result);
}
template
void BST::PreorderTraverse(FunctionType Visit)
{
Preorder(Root, Visit);
}
template
void BST::InorderTraverse(FunctionType Visit)
{
Inorder(Root, Visit);
}
template
void BST::PostorderTraverse(FunctionType Visit)
{
Postorder(Root, Visit);
}
template
void BST::Destroy(Node*& pNode) //called in the destructor
{
if (pNode->Left != NULL)
{
Destroy(pNode->Left);
pNode->Left = NULL;
}
if (pNode->Right != NULL)
{
Destroy(pNode->Right);
pNode->Right = NULL;
}
if (pNode != NULL)
{
delete pNode;
pNode = NULL;
}
else
{
assert(false);
}
}
template
void BST::InsertItem(Node*& pNode, const DataType& NewItem)
{
if (pNode == NULL)
{
pNode = new Node();
pNode->Item = NewItem;
}
else if (NewItem < pNode->Item)
{
InsertItem(pNode->Left, NewItem);
}
else if (NewItem > pNode->Item)
{
InsertItem(pNode->Right, NewItem);
}
}
template
inline void BST::DeleteItem(Node*& pNode, const DataType & SearchItem)
{
}
template
inline void BST::DeleteNodeItem(Node*& pNode)
{
}
template
inline void BST::DeleteLeftMost(Node*& pNode, DataType & Item)
{
}
//operators == and > must be defined for DataType
template
bool BST::RetrieveItem(Node* pNode,
const DataType& SearchItem,
DataType& Item)
{
bool result = false;
if (pNode == NULL)
{
result = false;
}
else if (SearchItem == pNode->Item)
{
result = true;
Item = pNode->Item;
}
else if (SearchItem > pNode->Item)
{
result = RetrieveItem(pNode->Right, SearchItem, Item);
}
else
{
result = RetrieveItem(pNode->Left, SearchItem, Item);
}
return result;
}
template
void BST::Preorder(Node* pNode, FunctionType Visit)
{
if (pNode != NULL)
{
Visit(pNode->Item);
}
if (pNode->Left != NULL)
{
Preorder(pNode->Left, Visit);
}
if (pNode->Right != NULL)
{
Preorder(pNode->Right, Visit);
}
}
template
void BST::Inorder(Node* pNode, FunctionType Visit)
{
if (pNode->Left != NULL)
{
Inorder(pNode->Left, Visit);
}
if (pNode != NULL)
{
Visit(pNode->Item);
}
if (pNode->Right != NULL)
{
Inorder(pNode->Right, Visit);
}
}
template
void BST::Postorder(Node* pNode, FunctionType Visit)
{
if (pNode->Left != NULL)
{
Postorder(pNode->Left, Visit);
}
if (pNode->Right != NULL)
{
Postorder(pNode->Right, Visit);
}
if (pNode != NULL)
{
Visit(pNode->Item);
}
}
#endif