Write a function, swapSubtrees, that swaps all of the left and right subtrees of
ID: 3648787 • Letter: W
Question
Write a function, swapSubtrees, that swaps all of the left and right subtrees of a binary tree. Add this function to the class binaryTreeType and create a program to test this function.Explanation / Answer
Header File Binary Search Tree #ifndef H_binaryTree #define H_binaryTree #include #include using namespace std; //Definition of the node template void binaryTreeSearch::insert(const elemType &insertItem) { struct nodeType { elemType info; nodeType *llink; nodeType *rlink; }; //Definition of the class template class binaryTreeType { public: const binaryTreeType& operator= (const binaryTreeType&); //Overload the assignment operator. bool isEmpty(); //Function to determine if the binary tree is empty. //Postcondition: Returns true if the binary tree is empty; // otherwise, returns false. void inorderTraversal(); //Function to do an inorder traversal of the binary tree. //Postcondition: The nodes of the binary tree are output // in the inorder sequence. void preorderTraversal(); //Function to do a preorder traversal of the binary tree. //Postcondition: The nodes of the binary tree are output // in the preorder sequence. void postorderTraversal(); //Function to do a postorder traversal of the binary tree. //Postcondition: The nodes of the binary tree are output // in the postorder sequence. int treeHeight(); //Function to deteremine the height of the binary tree. //Postcondition: The height of the binary tree is returned. int treeNodeCount(); //Function to determine the number of nodes in the //binary tree. //Postcondition: The number of nodes in the binary tree // is returned. int treeLeavesCount(); //Function to determine the number of leaves in the //binary tree. //Postcondition: The number of leaves in the binary tree // is returned. void printTree(); void printTree(nodeType *p); void printTreeNode(string label, nodeType *p); void swapSubtreeNodes(nodeType *p); void destroyTree(); //Deallocates the memory space occupied by the binary tree. //Postcondition: root = NULL; binaryTreeType(const binaryTreeType& otherTree); //copy constructor binaryTreeType(); //default constructor ~binaryTreeType(); //destructor protected: nodeType *root; private: void copyTree(nodeType* &copiedTreeRoot, nodeType* otherTreeRoot); //Function to make a copy of the binary tree to //which otherTreeRoot points. //Postcondition: The pointer copiedTreeRoot points to // the root of the copied binary tree. void destroy(nodeType* &p); //Function to destroy the binary tree to which p points. //Postcondition: The nodes of the binary tree to which // p points are deallocated. // p = NULL. void inorder(nodeType *p); //Function to do an inorder traversal of the binary //tree to which p points. //Postcondition: The nodes of the binary tree to which p // points are output in the inorder sequence. void preorder(nodeType *p); //Function to do a preorder traversal of the binary //tree to which p points. //Postcondition: The nodes of the binary tree to which p // points are output in the preorder sequence. void postorder(nodeType *p); //Function to do a postorder traversal of the binary //tree to which p points. //Postcondition: The nodes of the binary tree to which p // points are output in the postorder sequence. int height(nodeType *p); //Function to determine the height of the binary tree //to which p points. //Postcondition: The height of the binary tree to which p // points is returned. int max(int x, int y); //Function to determine the larger of x and y. //Postcondition: The larger of x and y is returned. int nodeCount(nodeType *p); //Function to determine the number of nodes in the binary //tree to which p points. //Postcondition: The number of nodes in the binary tree // to which p points is returned. int leavesCount(nodeType *p); //Function to determine the number of leaves in the binary //tree to which p points. //Postcondition: The number of nodes in the binary tree // to which p points is returned. }; //Definition of member functions template binaryTreeType::binaryTreeType() { root = NULL; } template bool binaryTreeType::isEmpty() { return (root == NULL); } template void binaryTreeType::inorderTraversal() { inorder(root); } template void binaryTreeType::preorderTraversal() { preorder(root); } template void binaryTreeType::postorderTraversal() { postorder(root); } template int binaryTreeType::treeHeight() { return height(root); } template int binaryTreeType::treeNodeCount() { return nodeCount(root); } template int binaryTreeType::treeLeavesCount() { return leavesCount(root); } template void binaryTreeType::copyTree (nodeType* &copiedTreeRoot, nodeType* otherTreeRoot) { if(otherTreeRoot == NULL) copiedTreeRoot = NULL; else { copiedTreeRoot = new nodeType; copiedTreeRoot->info = otherTreeRoot->info; copyTree(copiedTreeRoot->llink, otherTreeRoot->llink); copyTree(copiedTreeRoot->rlink, otherTreeRoot->rlink); } } //end copyTree template void binaryTreeType::inorder(nodeType *p) { if(p != NULL) { inorder(p->llink); coutrlink)); } template int binaryTreeType::max(int x, int y) { if(x >= y) return x; else return y; } template int binaryTreeType::nodeCount(nodeType *p) { if(p == NULL) return 0; else return 1 + nodeCount(p->llink) + nodeCount(p->rlink); } template void binaryTreeType::printTree() { printTree(root); } template void binaryTreeType::printTree(nodeType *p) { coutRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.