1) Write a BST function called Find which will search for the node which contains Data and if found,
returns a pointer to this node and a pointer to its parent via reference parameters. If Data is not found,
simply return false. The assumption is made that TreeType is a c-string and the BST is ordered on this
type. Also, a node contains a struct called Info which contains several fields, one of which is a name
field.
bool BST::Find (TreeType Data, Node * & Current, Node * & Parent)
bool BST::Find (TreeType Data, Node * & Current, Node * & Parent)
{
//initialize a current and trailer pointer
Current = Root;
Parent = NULL;
//Loop to find the node as long as current is pointing at a valid node
//and the key data has not been found.
while ((Current != NULL) && (strcmp(Data,Current->Info.Name) != 0))
{
Parent = Current;
if (strcmp(Data,Current->Info.Name) < 0)
Current = Current->Left;
else
Current =Current->Right;
}
//if current is not a NULL pointer, then return that the node has been
//found and its pointer is in current and its parent is in Parent.
if (Current != NULL)
return true;
else
return false;
}
2) Write a function called delete, which will take two pointers: 1) a pointer to the node to be deleted
and 2) a pointer to its parent. This function should determine the appropriate deletion routine to call
and the choices are:
DeleteLeaf (needs a pointer to the node being deleted and its parent)
DeleteOneChild (needs a pointer to the node being deleted and its parent)
DeleteTwoChild(needs only a pointer to the node being deleted)
void BST::Delete (Node * Current, Node * Parent)
void BST::Delete (Node * Current, Node * Parent)
{
if (Current->Rt == NULL && Current->Lt == NULL)
DeleteLeaf(Current,Parent);
else if (Current->Rt != NULL && Current->Lt != NULL)
DeleteTwoChild(Current);
else
DeleteOneChild(Current,Parent);
return true;
}
3. You might need to make a copy of a tree (an exact duplicate). A preorder traversal can help to
make a copy of a tree. Assume the following code:
BST::BST(BST & Source)
{
Root = NULL;
CopyTree(Source.Root);
}
This is the code for a copy constructor and will be invoked in a statement such as:
BST copy(source); //source is a BST object
How would you write the CopyTree function.
void BST::CopyTree(Node * p)
void BST::CopyTree(Node * p)
{
if (p != NULL)
{
Insert(p->Info); //function which should already be in place
CopyTree(p->Left);
CopyTree(p->Right);
}
}
4) Assume the user wants to print out an alphabetical listing of your BST, so he or she executes the
following statement:
CriminalDB.Print();
What will you need to do to print out the tree contents in sorted order?
//public function which the client can call
void BST::Print ()
{
InOrderPrint (Root);
}
//private function which will service the public function Print
void BST::InOrderPrint(Node * p)
{
if (p != NULL)
{
InOrderPrint(p->Lt);
cout << p->Info.Name;
InOrderPrint(p->Rt);
}
}
© Copyright 2026 Paperzz