[Solved] C Using Following Code Modify Completing Following Objectives Finish Implementation E See Q37175507
c++) using the following code modify by completing the followingobjectives to finish the implementation.(I.e. see recursive heightis there, just the implementation of else statement needs to befinished and so on for most of the obj).
in Binary_tree.h
- Implement operator=() by using pointer swapping technique.
- Implement the following member functions by usingrecursion
- ~Binary_tree():
- recursive_preorder()
- recursive_inorder()
- recursive_postorder()
- recursive_height()
- Add the following public member function
int getLeafCount()
in Search_tree.h
- Implement search_and_insert()
- Implement search_and_delete() with immediate predecessorapproach
//Binary_node.h
enum Balance_factor { left_higher, equal_height, right_higher};
template
struct Binary_node{
// data members:
Entry data;
Binary_node *left;
Binary_node *right;
// constructors:
Binary_node();
Binary_node(const Entry &x);
};
template
Binary_node::Binary_node(){
left = NULL;
right = NULL;
}
template
Binary_node::Binary_node(const Entry &x){
data = x;
left = NULL;
right = NULL;
}
——————————
//Binary_tree.h
template
class Binary_tree {
public:
Binary_tree();
bool empty() const;
void preorder(void (*visit)(Entry &));
void inorder(void (*visit)(Entry &));
void postorder(void (*visit)(Entry &));
int size() const;
void clear();
int height() const;
void print() const; // A method to print out the tree usingpreorder
Binary_tree(const Binary_tree &original);
Binary_tree& operator =(const Binary_tree &original);
~Binary_tree();
protected:
// Auxiliary functions
void recursive_preorder(Binary_node *sub_root, void (*visit)(Entry&));
void recursive_inorder(Binary_node *sub_root, void (*visit)(Entry&));
void recursive_postorder(Binary_node *sub_root, void (*visit)(Entry&));
int recursive_size(Binary_node *sub_root) const;
void recursive_clear(Binary_node *sub_root);
int recursive_height(Binary_node *sub_root) const;
Binary_node* recursive_copy(Binary_node *sub_root);
void recursive_preorder_print_node(Binary_node *sub_root)const;
// Data member
Binary_node* root;
};
template
Binary_tree::Binary_tree()
/*
Post: An empty binary tree has been created.
*/
{
root = NULL;
}
template
Binary_tree::Binary_tree(const Binary_tree &original)
/*
Post: A new binary tree has been created with the same structureand content as those of original.
Uses: The function recursive_copy
*/
{
root = recursive_copy(original.root);
}
template
Binary_tree& Binary_tree::operator =(const Binary_tree&original)
/*
Post: The binary tree is assigned to copy a parameter
*/
{
}
template
Binary_tree::~Binary_tree() {
}
template
bool Binary_tree::empty() const
/*
Post: A result of true is returned if the binary tree isempty.
Otherwise, false is returned.
*/
{
return root == NULL;
}
template
void Binary_tree::preorder(void (*visit)(Entry &))
/*
Post: The tree has been traversed in inorder sequence.
Uses: The function recursive_inorder
*/
{
recursive_preorder(root, visit);
}
template
void Binary_tree::inorder(void (*visit)(Entry &))
/*
Post: The tree has been traversed in inorder sequence.
Uses: The function recursive_inorder
*/
{
recursive_inorder(root, visit);
}
template
void Binary_tree::postorder(void (*visit)(Entry &))
/*
Post: The tree has been traversed in inorder sequence.
Uses: The function recursive_inorder
*/
{
recursive_postorder(root, visit);
}
template
int Binary_tree::size() const
/*
Post: The number of entries in the binary tree is returned.
Uses: The function recursive_size
*/
{
return recursive_size(root);
}
template
void Binary_tree::clear()
/*
Post: Dispose of all the nodes of the binary tree.
Uses: The function recursive_clear.
*/
{
recursive_clear(root);
root = NULL;
}
template
int Binary_tree::height() const
/*
Post: The height of the binary tree is returned.
Uses: The function recursive_height
*/
{
return recursive_height(root);
}
template
void Binary_tree::print() const{
/*
Post: The tree has been traversed in preorder. Each node and itstwo children is printed
Uses: The function recursive_preorder_print_node
*/
cout << endl;
cout << “++++++++++++++++++++++” << endl;
if(root == NULL)
cout << “EMPTY TREE” << endl;
else{
if(root->left == NULL && root->right == NULL)
cout << root->data << “: – -” << endl;
recursive_preorder_print_node(root);
}
cout << “++++++++++++++++++++++” << endl;
cout << endl;
}
// Functions
template
void Binary_tree::recursive_preorder(Binary_node *sub_root,
void (*visit)(Entry &))
/*
Pre: sub_root is either NULL or points to a subtree of theBinary_tree.
Post: The subtree has been traversed in preorder sequence.
Uses: The function recursive_preorder recursively
*/
{
}
template
void Binary_tree::recursive_inorder(Binary_node *sub_root,
void (*visit)(Entry &))
/*
Pre: sub_root is either NULL or points to a subtree of theBinary_tree.
Post: The subtree has been traversed in inorder sequence.
Uses: The function recursive_inorder recursively
*/
{
}
template
void Binary_tree::recursive_postorder(Binary_node *sub_root,
void (*visit)(Entry &))
/*
Pre: sub_root is either NULL or points to a subtree of theBinary_tree.
Post: The subtree has been traversed in postorder sequence.
Uses: The function recursive_postorder recursively
*/
{
}
template
int Binary_tree::recursive_size(Binary_node *sub_root) const
/*
Pre: sub_root is either NULL or points to a subtree of theBinary_tree.
Post: The number of entries in the subtree is returned.
Uses: The function recursive_size recursively
*/
{
if (sub_root == NULL)
return 0;
else
return 1 + recursive_size(sub_root->left) +recursive_size(sub_root->right);
}
template
void Binary_tree::recursive_clear(Binary_node *sub_root)
/*
Pre: sub_root is either NULL or points to a subtree of theBinary_tree.
Post: All the nodes in the subtree are disposed of inpostorder.
Uses: The function recursive_clear recursively
*/
{
if (sub_root != NULL) {
recursive_clear(sub_root->left);
recursive_clear(sub_root->right);
// Note that at this moment, sub_root->left andsub_root->right may be dangled pointers.
delete sub_root;
}
}
template
int Binary_tree::recursive_height(Binary_node *sub_root)const
/*
Pre: sub_root is either NULL or points to a subtree of theBinary_tree.
Post: the height of the subtree is returned.
Uses: The function recursive_height recursively
*/
{
if (sub_root == NULL)
return 0;
else {
}
}
template
Binary_node* Binary_tree::recursive_copy(Binary_node*sub_root)
/*
Pre: sub_root is either NULL or points to a subtree of theBinary_tree.
Post: returns a pointer to the root of a new binary tree that hasexactly the same structure and content as those of thesubtree.
Uses: The function recursive_copy recursively
*/
{
if (sub_root == NULL)
return NULL;
else {
Binary_node* new_sub_root = newBinary_node(sub_root->data);
new_sub_root->left = recursive_copy(sub_root->left);
new_sub_root->right = recursive_copy(sub_root->right);
return new_sub_root;
}
}
template
void Binary_tree::recursive_preorder_print_node(Binary_node*sub_root) const
/*
Pre: sub_root is either NULL or points to a subtree of theBinary_tree.
Post: The subtree has been traversed in preorder sequence.
Uses: The function recursive_preorder_print_node recursively
*/
{
if (sub_root != NULL) {
if(sub_root->left != NULL || sub_root->right != NULL){
cout << sub_root->data << “: “;
if(sub_root->left != NULL)
cout << sub_root->left->data << ” “;
else
cout << “- “; // “-” represents no child
if(sub_root->right != NULL)
cout << sub_root->right->data << ” “;
else
cout << “- “; // “-” represents no child
//cout << “; “;
cout << endl;
}
recursive_preorder_print_node(sub_root->left);
recursive_preorder_print_node(sub_root->right);
}
}
——————————————-
//Search_tree.h
template
class Search_tree: public Binary_tree {
public:
Error_code insert(const T &new_data);
Error_code remove(const T &old_data);
Error_code tree_search(T &target) const;
protected:
// Auxiliary functions called by the public functions
Error_code search_and_insert(Binary_node* &sub_root, const T&new_data);
Error_code search_and_delete(Binary_node* &sub_root, const T&target);
};
template
Error_code Search_tree::insert(const T &new_data)
{
return search_and_insert(this->root, new_data);
}
template
Error_code Search_tree::search_and_insert(
Binary_node *&sub_root, const T &new_data)
{
}
template
Error_code Search_tree::remove(const T &target)
/*
Post: If a T with a key matching that of target belongs tothe
Search_tree, a code of success is returned, and the correspondingnode is removed from the tree. Otherwise, a code of not_present isreturned.
Uses: Function search_and_delete
*/
{
return search_and_delete(this->root, target);
}
template
Error_code Search_tree::search_and_delete(
Binary_node* &sub_root, const T &target)
/*
Pre: sub_root is either NULL or points to a subtree of theSearch_tree.
Post: If the key of target is not in the subtree, a code ofnot_present
is returned. Otherwise, a code of success is returned and thesubtree
node containing target has been removed in such a way that
the properties of a binary search tree have been preserved.
Uses: Functions search_and_delete recursively
*/
{
}
——————————————————
Expert Answer
Answer to c++) using the following code modify by completing the following objectives to finish the implementation.(I.e. see recur… . . .
OR

