Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Ex 2 . Find and Remove a Subtree Problem Description Write a BST member function that takes a Binary Search Tree ( BST ) as
Ex Find and Remove a Subtree
Problem Description
Write a BST member function that takes a Binary Search Tree BST as a parameter:
bool findremovesubtreeconst BST &other
The function should search for a subtree within the current BST that exactly matches the structure and values of the received BST 'other'. If a match is found, the function should delete the matching subtree from the current BST and return true. Otherwise, the function should not modify the current tree and return false.
If the entire current BST matches the received BST the function should clear the entire original BST and return true You are allowed to define and use private functions. #pragma once
#include "queuedllh
#include dllh
#include
using std::string;
template
class BST;
template
class BSTNode
public:
BSTNodeconst T& val, BSTNode left, BSTNode right;
T getval const return val;
BSTNode getleft const return left;
BSTNode getright const return right;
private:
T val;
BSTNode left;
BSTNode right;
friend class BST;
;
template
BSTNode::BSTNodeconst T& val,
BSTNode left,
BSTNode right
thisval val;
thisleft left;
thisright right;
template
class BST
public:
bool findremovesubtreeconst BST &other
ADD YOUR IMPLEMENTATION HERE
BST;
BSTconst BST& other;
~BST;
bool isempty const;
bool containsconst T& val const;
void insertconst T& val;
bool removeconst T& val;
void clear;
DLList elements const;
DLList elementslevelordered const;
BSTNode getroot const return root;
BST& operatorconst BST& other;
private:
BSTNode root;
void copyfromBSTNode node;
void elementsDLList& result, BSTNode node const;
bool containsconst T& val, BSTNode node const;
void removeBSTNode ptr BSTNode prev;
void removeBSTNode node;
void clearBSTNode node;
;
template
BST::BST
root nullptr;
template
void BST::copyfromBSTNode node
if node nullptr
return;
insertnodeval;
copyfromnodeleft;
copyfromnoderight;
template
BST::BSTconst BST& other
root nullptr;
copyfromotherroot;
template
BST::~BST
clear;
template
bool BST::isempty const
return root nullptr;
Iterative implementation of searching in the tree.
template
bool BST::containsconst T& val const
BSTNode node root; always start the search from the root
whilenode nullptr
If the current node has the value we are looking for
if val nodeval
return true;
If the value that we are looking for is smaller than the
value of the current tree node, go left; else, go right
ifval nodeval
node nodeleft;
else
node noderight;
If the loop completes, node is necessarily a null pointer,
which means that val was not found in the tree
return false;
inserts the given val into the tree if it is not already in the tree.
template
void BST::insertconst T& val
if the tree is empty, the root needs to point at the new node.
if isempty
root new BSTNodeval nullptr, nullptr;
return;
BSTNode curr root;
BSTNode prev nullptr;
Loop to search for the right position for val
whilecurr nullptr
prev curr;
if val currval
curr currleft;
else if val currval
curr currright;
else
return;
BSTNode newnode new BSTNodeval nullptr, nullptr;
if val prevval
prevleft newnode;
else
prevright newnode;
Breadthfirst traversal
template
DLList BST::elementslevelordered const
DLList result;
if root nullptr
return result;
a queue of pointers to BSTNode objects.
QueueDLL queue;
BSTNode node root;
queue.enqueuenode;
while queue.isempty
Take a node out of the queue, process it and then insert
its children into the queue.
node queue.dequeue;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started