Question: C++ help: Implement the class BinarySearchTree, as given in listing 16-4 Driver program should: -Insert 21 random numbers (1-100) on the tree -Remove the last
C++ help:
Implement the class BinarySearchTree, as given in listing 16-4
Driver program should:
-Insert 21 random numbers (1-100) on the tree
-Remove the last number inserted
-Display the tree. To display just use in order traversal
*No user input for driver program.
Use the BST ADT BinarySearchTree.h and the BinaryTreeInterface.h
BinarySearchTree.h code is shown below.
// Listing 16-4. /** Link-based implementation of the ADT binary search tree. @file BinarySearchTree.h */ #ifndef BINARY_SEARCH_TREE_ #define BINARY_SEARCH_TREE_ #include#include "BinaryTreeInterface.h" #include "BinaryNode.h" #include "BinaryNodeTree.h" #include "NotFoundException.h" #include "PrecondViolatedExcep.h" template class BinarySearchTree : public BinaryNodeTree { private: std::shared_ptr > rootPtr; protected: //------------------------------------------------------------ // Protected Utility Methods Section: // Recursive helper methods for the public methods. //------------------------------------------------------------ // Recursively finds where the given node should be placed and // inserts it in a leaf at that point. auto placeNode(std::shared_ptr > subTreePtr, std::shared_ptr > newNode); // Removes the given target value from the tree while maintaining a // binary search tree. std::shared_ptr > removeValue(std::shared_ptr > subTreePtr, const ItemType target, bool& success) override; // Removes a given node from a tree while maintaining a // binary search tree. auto removeNode(std::shared_ptr > nodePtr); // Removes the leftmost node in the left subtree of the node // pointed to by nodePtr. // Sets inorderSuccessor to the value in this node. // Returns a pointer to the revised subtree. auto removeLeftmostNode(std::shared_ptr > subTreePtr, ItemType& inorderSuccessor); // Returns a pointer to the node containing the given value, // or nullptr if not found. auto findNode(std::shared_ptr > treePtr, const ItemType& target) const; public: //------------------------------------------------------------ // Constructor and Destructor Section. //------------------------------------------------------------ BinarySearchTree(); BinarySearchTree(const ItemType& rootItem); BinarySearchTree(const BinarySearchTree & tree); virtual ~BinarySearchTree(); //------------------------------------------------------------ // Public Methods Section. //------------------------------------------------------------ bool isEmpty() const override; int getHeight() const override; int getNumberOfNodes() const override; ItemType getRootData() const throw(PrecondViolatedExcep) override; void setRootData(const ItemType& newData) const throw(PrecondViolatedExcep); bool add(const ItemType& newEntry) override; bool remove(const ItemType& anEntry) override; void clear() override; ItemType getEntry(const ItemType& anEntry) const throw(NotFoundException) override; bool contains(const ItemType& anEntry) const override; //------------------------------------------------------------ // Public Traversals Section. //------------------------------------------------------------ void preorderTraverse(void visit(ItemType&)) const override; void inorderTraverse(void visit(ItemType&)) const override; void postorderTraverse(void visit(ItemType&)) const override; //------------------------------------------------------------ // Overloaded Operator Section. //------------------------------------------------------------ BinarySearchTree & operator=(const BinarySearchTree & rightHandSide); }; // end BinarySearchTree #include "BinarySearchTree.cpp" #endif
-----------------------------------------------------------------------------------------------
BinaryTreeInterface.h is shown below
/** Listing 15-1. @file BinaryTreeInterface.h */
#ifndef BINARY_TREE_INTERFACE_ #define BINARY_TREE_INTERFACE_
#include "NotFoundException.h"
template
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
