Question: c++ i need help with the copyTree function as well as the equals operator overload function #include using namespace std; class IntBinaryTree { private: struct
c++ i need help with the copyTree function as well as the equals operator overload function
#include
// Private member functions void insert(TreeNode *&, TreeNode *&); void destroySubTree(TreeNode *); void deleteNode(int, TreeNode *&); void makeDeletion(TreeNode *&); void displayInOrder(TreeNode *) const; int countLeafNodes(TreeNode *&); int getTreeHeight(TreeNode *); TreeNode *copyTree(TreeNode*);
public: // Constructor IntBinaryTree() { root = nullptr; } // Copy constructor IntBinaryTree(const IntBinaryTree &);
// Destructor ~IntBinaryTree() { destroySubTree(root); }
// Binary tree operations void insertNode(int); bool searchNode(int); void remove(int);
void displayInOrder() const { displayInOrder(root); }
int numLeafNodes(); int treeHeight(); // Overloaded Assignment Operator IntBinarytree operator= (const IntBinaryTree & );
};
//********************************************************* // Copy Constructor // calls the copyTree function IntBinaryTree::IntBinaryTree (const IntBinaryTree &tree ) { copyTree(tree); } //*********************************************** // Assignment Operator // Hint: destroy all nodes in the old tree then use the // copyTree function IntBinaryTree IntBinaryTree::operator= ( const IntBinaryTree &tree ) { } //******************************************************** // copyTree Function: called by copy constructor and // Assignment operator function. This function copies all tree nodes // by visiting the root nodes first (pre-order traversal) IntBinaryTree::TreeNode *IntBinaryTree::copyTree(TreeNode *nPtr) {
}
//*********************************************************** // insert accepts a TreeNode pointer and a pointer to a node. // The function inserts the node into the tree pointed to by // the TreeNode pointer. This function is called recursively. void IntBinaryTree::insert(TreeNode *&nodePtr, TreeNode *&newNode) { if (nodePtr == nullptr) { nodePtr = newNode; // Insert the node. } else if (newNode->value < nodePtr->value) { insert(nodePtr->left, newNode); // Search the left branch } else { insert(nodePtr->right, newNode);// Search the right branch } } //********************************************************** // insertNode creates a new node to hold num as its value, // and passes it to the insert function. void IntBinaryTree::insertNode(int num) { TreeNode *newNode = nullptr; // Pointer to a new node. // Create a new node and store num in it. newNode = new TreeNode; newNode->value = num; newNode->left = newNode->right = nullptr;
// Insert the node. insert(root, newNode); } //*************************************************** // destroySubTree is called by the destructor. It // deletes all nodes in the tree. void IntBinaryTree::destroySubTree(TreeNode *nodePtr) { if (nodePtr) { if (nodePtr->left) { destroySubTree(nodePtr->left); }
if (nodePtr->right) { destroySubTree(nodePtr->right); }
delete nodePtr; } }
//*************************************************** // searchNode determines if a value is present in // the tree. If so, the function returns true. // Otherwise, it returns false. bool IntBinaryTree::searchNode(int num) { bool status = false; TreeNode *nodePtr = root; while (nodePtr) { if (nodePtr->value == num) { status = true; } else if (num < nodePtr->value) { nodePtr = nodePtr->left; } else { nodePtr = nodePtr->right; } } return status; } //********************************************** // remove calls deleteNode to delete the // node whose value member is the same as num. void IntBinaryTree::remove(int num) { deleteNode(num, root); } //******************************************** // deleteNode deletes the node whose value // member is the same as num. void IntBinaryTree::deleteNode(int num, TreeNode *&nodePtr) { if (num < nodePtr->value) { deleteNode(num, nodePtr->left); } else if (num > nodePtr->value) { deleteNode(num, nodePtr->right); } else { makeDeletion(nodePtr); } } //*********************************************************** // makeDeletion takes a reference to a pointer to the node // that is to be deleted. The node is removed and the // branches of the tree below the node are reattached. void IntBinaryTree::makeDeletion(TreeNode *&nodePtr) { // Define a temporary pointer to use in reattaching // the left subtree. TreeNode *tempNodePtr = nullptr; if (nodePtr == nullptr) { cout << "Cannot delete empty node. "; } else if (nodePtr->right == nullptr) { tempNodePtr = nodePtr; nodePtr = nodePtr->left; // Reattach the left child delete tempNodePtr; } else if (nodePtr->left == nullptr) { tempNodePtr = nodePtr; nodePtr = nodePtr->right; // Reattach the right child delete tempNodePtr; } // If the node has two children. else { // Move one node the right. tempNodePtr = nodePtr->right; // Go to the end left node. while (tempNodePtr->left) { tempNodePtr = tempNodePtr->left; }
// Reattach the left subtree. tempNodePtr->left = nodePtr->left; tempNodePtr = nodePtr; // Reattach the right subtree. nodePtr = nodePtr->right; delete tempNodePtr; } } //**************************************************************** // The displayInOrder member function displays the values // in the subtree pointed to by nodePtr, via inorder traversal. void IntBinaryTree::displayInOrder(TreeNode *nodePtr) const { if (nodePtr) { displayPreOrder(nodePtr->left); cout << nodePtr->value << endl; displayPreOrder(nodePtr->right); }
} //******************************************************** // numNodes Function: calls countLeafNodes and returns the number // of leaf nodes in the tree int IntBinaryTree::numLeafNodes() { int leafCount = 0; //Reset to 0 each time a count is made countLeafNodes(root); return leafCount; } //**************************************************************** // The countLeafNodes function uses recursion to count the number // of leaf nodes in the tree. This function is called by the public // member function numLeafNodes. It visits all the root nodes first // (pre-order traversal) int IntBinaryTree::countLeafNodes(TreeNode *&nodePtr) { if (nodePtr) { countLeafNodes(nodePtr->left); countLeafNodes(nodePtr->right); if (nodePtr->left == NULL && nodePtr->right == NULL) { leafCount++; } } } //************************************************************* // Function TreeHeight // Calls getTreeHeight and displays the height or depth of tree. int IntBinaryTree::treeHeight() { return getTreeHeight(root); } //************************************************************* // Function getTreeHeight // This function uses recursion to count the height of // the tree. int IntBinaryTree::getTreeHeight(TreeNode* nodePtr) { int leftHeight, rightHeight; if (nodePtr) { leftHeight = getTreeHeight(nodePtr->left); rightHeight = getTreeHeight(nodePtr->right); if (leftHeight > rightHeight) return leftHeight + 1; else return rightHeight + 1; } else { return 0; } } }
int main() { // Create 3 binary trees to hold integers. IntBinaryTree tree, tree1, tree2; // Show the initial height, with no nodes. cout << "Height: " << tree.treeHeight() << endl; cout << endl; // Insert some nodes into the tree. cout << "Inserting nodes. "; tree.insertNode(5); tree.insertNode(8); tree.insertNode(3); tree.insertNode(12); tree.insertNode(9); cout << endl; // Display the nodes. cout << "Here are the values in the tree: "; tree.displayInOrder(); cout << endl; // Display the tree height. cout << "Height: " << tree.treeHeight() << endl; cout << endl; // Display the number of leaf nodes in tree. cout << "Number of leaf nodes: " << tree.numLeafNodes() << endl; // Delete some nodes. cout << "Deleting 8... "; tree.remove(8); cout << "Deleting 12... "; tree.remove(12); cout << endl; // Display the nodes that are left. cout << "Now, here are the nodes: "; tree.displayInOrder(); cout << endl; // Display the tree height. cout << "Height: " << tree.treeHeight() << endl; cout << endl; // Display the number of leaf nodes in tree. cout << "Number of leaf nodes: " << tree.numLeafNodes() << endl; // Insert some nodes into tree1. tree1.insertNode(55); tree1.insertNode(5); tree1.insertNode(8); // Display the nodes in tree1. cout << " Here are the nodes of tree1: "; tree1.displayInOrder(); cout << endl; // Assign tree1 to tree2. tree2 = tree1; // Display the nodes in tree2. cout << "Now, here are the nodes of tree2: "; tree2.displayInOrder(); cout << endl; // Modify tree 1 by adding 3 nodes to tree 1 tree1.insertNode(10); tree1.insertNode(3); tree1.insertNode(12); // Display the nodes in tree1. cout << " Here are the nodes of tree1 after adding 3 nodes: "; tree1.displayInOrder(); cout << endl; // Display the nodes in tree2. cout << " Here are the nodes of tree2: "; tree2.displayInOrder(); cout << endl; // Define another IntBinaryTree object and initialize it // with tree1. This will invoke the copy constructor. IntBinaryTree tree3 = tree1; // Display the nodes in tree3. cout << "Now, here are the nodes of tree3: "; tree3.displayInOrder(); cout << endl; // Remove one node from tree 1 tree1.remove(5); // Display the nodes in tree1. cout << " Here are the nodes of tree1 after removing node 5: "; tree1.displayInOrder(); cout << endl; // Display the nodes in tree3. cout << "Now, here are the nodes of tree3: "; tree3.displayInOrder(); cout << endl; return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
