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

Write the defn of the function, nodecount, that returns the number of nodes in t

ID: 3867713 • Letter: W

Question

Write the defn of the function, nodecount, that returns the number of nodes in the binary tree. Add this function to the class binary tree type and create a program to test this function. Write the defn of the function leavescount, that takes as a parameter a pointer to the root node of a binary tree and returns the number of leaves in a binary tree. Add this function to the class binary tree type and create a program to test this function. Write a function, Swapsubtrees, that swaps all of the left and right subtrees of a binary tree type and create a program to test the function.

Explanation / Answer

Let the node structure be

Struct Node {
     T data;
      Node* left;
      Node* right;
}

1.
int countLeafNodes(Node *root) {
       if (root == NULL)
                return 0;
       return countNode(root->right) + countNode(root->left) + 1;
}

2.
int countLeafNodes(Node *root) {
       if (root == NULL)
                return 0;
       if(root -> right == NULL && root->left == NULL)
               return 1;
       return countLeafNode(root->right) + countLeafNode(root->left);
}

3.

void swapLR(Node *root) {
     if (root == NULL)
          return;
     Node* tmpTree = root->left;
     root->left = root->right;
     root->right = tmpTree;
     swapLR(root->right);
     swapLR(root->left);
}

I cannot provide you the test code since the class description is missing :(