Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

UsingC++ Write a member function , swapSubtrees , of class binaryTreeType , that

ID: 3648453 • Letter: U

Question

UsingC++


Write a member function, swapSubtrees, of class binaryTreeType, that swaps all of the left and right subtrees of a binary tree.


This will be a driver function that will call a recursive member function with the following header:


void swapSubtreesOfNode(binaryTreeNode<elemType> *p;

Explanation / Answer

template void binaryTreeType::swapSubtrees(nodeType * p) { if (p != NULL) { // if node has 2 children, swap them if (p->llink != NULL && p->rlink != NULL) { nodeType * temp = p->llink; p->llink = p->rlink; p->rlink = temp; delete temp; } // has left child but no right child // go down the left subtree if (p->llink != NULL && p->rlink == NULL) { swapSubtrees(p->llink); } // has right child but no left child // go down the right subtree if (p->rlink != NULL && p->llink == NULL) { swapSubtrees(p->rlink); } } else { return; } }