Question

Write a recursive function that compares two given binary trees. It returns 1 if the two trees are different, and it returns

0 0
Add a comment Improve this question Transcribed image text
Answer #1
struct node {
    node *left;
    node *right;
    int data;
};
int treeDiff(node *a, node *b) {
    if (a == NULL && b == NULL) {
        return 0;
    } else if (a == NULL || b == NULL) {
        return 1;
    } else {
        if (a->data == b->data && treeDiff(a->left, b->left) == 0 && treeDiff(a->right, b->right) == 0) {
            return 0;
        } else {
            return 1;
        }
    }
}

int countOneChild(node *root) {
    if (root == NULL) {
        return 0;
    } else {
        int count = 0;
        if ((root->left == NULL && root->right != NULL) || (root->left != NULL && root->right == NULL)) {
            count = 1;
        }
        return count + countOneChild(root->left) + countOneChild(root->right);
    }
}
Add a comment
Know the answer?
Add Answer to:
Write a recursive function that compares two given binary trees. It returns 1 if the two...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 1) (10 pts) Write a recursive function that counts and returns the number of nodes in...

    1) (10 pts) Write a recursive function that counts and returns the number of nodes in a binary tree with the root root, that store an even value. Please use the struct shown and function prototype shown below. (For example, if the tree rooted at root stored 2, 3, 4, 8, 13, 18 and 20, the function should return 5, since there are five even values [2,4,8,18,20] stored in the tree. typedef struct node { int data; struct node* left;...

  • 1. Write a recursive function that returns the sum of all even integers in a LinkedBinaryTree. Yo...

    please explain each line of code! ( in python ) 1. Write a recursive function that returns the sum of all even integers in a LinkedBinaryTree. Your function should take one parameter, root node. You may assume that the tree only contains integers. You may not call any methods from the LinkedBinaryTree class. Specifically, you should traverse the tree in your function def binary tree even sum (root): Returns the sum of al1 even integers in the binary tree 2....

  • Answer the following question(s) concerning implementing recursive functions to perform operations on linked lists of nodes...

    Answer the following question(s) concerning implementing recursive functions to perform operations on linked lists of nodes arranged as binary trees. For these questions, use the following struct definition for the nodes of the tree (we will not templatize the node here, so you do not have to write template functions for these questions, just assume trees of <int> values): struct BinaryTreeNode { int item; BinaryTreeNode* left; BinaryTreeNode* right; }; ---------------------------------------------- Given a node for a Binary Tree and an (integer)...

  • Write a recursive function (C++) that searches a binary tree that is NOT ordered like a...

    Write a recursive function (C++) that searches a binary tree that is NOT ordered like a BST. In other words, there is no ordering relationship among the parent, child or sibling node values. Use the following prototype and data structure: struct node { node *left; node *right; int val; }; // First parameter: pointer to a node // Second parameter: the value to find bool searchValue(node *, int); The function searchValue() should return true if the integer value in the...

  • 1) Extend the Binary Search Tree ADT to include a public method leafCount that returns the...

    1) Extend the Binary Search Tree ADT to include a public method leafCount that returns the number of leaf nodes in the tree. 2) Extend the Binary Search Tree ADT to include a public method singleParent-Count that returns the number of nodes in the tree that have only one child. 3) The Binary search tree ADT is extended to include a boolean method similarTrees that receives references to two binary trees and determines whether the shapes of the trees are...

  • C++ Vectors and Binary Search Trees • Write a program that takes from the user n...

    C++ Vectors and Binary Search Trees • Write a program that takes from the user n integers and stores them a vector of int. Then, create a function insert After that takes first Value and second Value. This function searches for each occurrence of first Value in the vector and insert the second Value after it in the same vector. The first and second values are taken from the user. • Create another function that creates a Binary Search Tree...

  • A binary tree is constructed of nodes that are instances of the following class: public class...

    A binary tree is constructed of nodes that are instances of the following class: public class Node public int val public Node left public Node right) Consider the following method public static Node mystery Node root) rootghtanul return root else return mystery root ) You consult Professor Kennedy and hegves an opinion about what the method does when passed a reference to the root node of a binary tree. Assuming he is correct what does the mystery function do? it...

  • 2. A regular binary tree is a binary tree whose internal nodes all have two subtrees...

    2. A regular binary tree is a binary tree whose internal nodes all have two subtrees (left and right). In other words, all their nodes have either zero subtrees (in which case they are leaves) or two subtrees (in which case they are internal nodes). Suppose that you have a boolean function that tells you, for each node of the tree, whether it is a leaf or not (call it: leaf(n), for node n). a) Write a recursive function that...

  • Write a function, swapSubtrees, that swaps all of the left and right subtrees of a binary...

    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. #include <iostream> using namespace std; //Definition of the Node template <class elemType> struct nodeType { elemType info; nodeType<elemType> *lLink; nodeType<elemType> *rLink; }; //Definition of the class template <class elemType> class binaryTreeType { public: //Overload the assignment operator. const binaryTreeType<elemType>& operator=(const binaryTreeType<elemType>&) { if (this != &otherTree) //avoid self-copy...

  • Write a recursive function that returns the minimum key value in a binary search tree of...

    Write a recursive function that returns the minimum key value in a binary search tree of distinct (positive) integers. Return -1 in case the tree is empty. (b) Write a recursive function that returns the predecessor of the key value k in a binary search tree of distinct (positive) integers. This is the key value that precedes k in an inorder traversal of the tree. If k does not exist, or if k has no predecessor, your function should return...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT