Question

Write a c++ function that will a. determine whether two binary trees are mirror similar. b....

Write a c++ function that will

a. determine whether two binary trees are mirror similar.

b. determine whether two binary trees are isomorphic.

c. replace a given value in a binary search tree with a new value keeping the tree as a binary search tree.

d. create a new tree composed of words in another tree in reverse order

e. create a 0-2 binary search tree based on existing non 0-2 binary search tree

0 0
Add a comment Improve this question Transcribed image text
Answer #1

a)

CODE

/* Given two trees, return true if they are

   mirror of each other */

int areMirror(Node* a, Node* b)

{

    /* Base case : Both empty */

    if (a==NULL && b==NULL)

        return true;

  

    // If only one is empty

    if (a==NULL || b == NULL)

        return false;

  

    /* Both non-empty, compare them recursively

     Note that in recursive calls, we pass left

     of one tree and right of other tree */

    return a->data == b->data &&

            areMirror(a->left, b->right) &&

            areMirror(a->right, b->left);

}

b)

CODE

/* Given a binary tree, print its nodes in reverse level order */

bool isIsomorphic(node* n1, node *n2)

{

// Both roots are NULL, trees isomorphic by definition

if (n1 == NULL && n2 == NULL)

    return true;

  

// Exactly one of the n1 and n2 is NULL, trees not isomorphic

if (n1 == NULL || n2 == NULL)

    return false;

  

if (n1->data != n2->data)

    return false;

  

// There are two possible cases for n1 and n2 to be isomorphic

// Case 1: The subtrees rooted at these nodes have NOT been "Flipped".

// Both of these subtrees have to be isomorphic, hence the &&

// Case 2: The subtrees rooted at these nodes have been "Flipped"

return

(isIsomorphic(n1->left,n2->left) && isIsomorphic(n1->right,n2->right))||

(isIsomorphic(n1->left,n2->right) && isIsomorphic(n1->right,n2->left));

}

  

NOTE: As per Chegg policy, I am allowed to answer only 2 coding questions (including sub-parts) on a single post. Kindly post the remaining questions separately and I will try to answer them. Sorry for the inconvenience caused.

Add a comment
Know the answer?
Add Answer to:
Write a c++ function that will a. determine whether two binary trees are mirror similar. b....
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
  • Fill a tree called Pine with 25 elements from an input file. Traverse the tree using...

    Fill a tree called Pine with 25 elements from an input file. Traverse the tree using each of the following methods. Print the smallest element in the binary search tree, Pine. Find the number of edges between the root of the tree and the node that contains the smallest value in the tree. Return the count to the calling unit. Count the number of internal nodes in the original tree, Pine. Print the count and return it to the calling...

  • 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...

  • C++ Binary Search trees Depth first traversal is the same as  _____ order traversal. When we add...

    C++ Binary Search trees Depth first traversal is the same as  _____ order traversal. When we add a new node to an existing binary search tree, it will always become a ______. When we delete a node with 2 children from a binary search tree, we replace the node with ______.

  • 1. Given the two binary trees below: 14 16 Write a method called swapSubtrees, which swaps all of...

    in java ..write all complete program from a- e 1. Given the two binary trees below: 14 16 Write a method called swapSubtrees, which swaps all of the left and right subtrees in the above binary trees. Add this method to the class BinaryTree and create a program to test this method for these 2 trees. Show the original trees and the resulting trees. Note: To test your algorithm, first create a binary search tree. Write a method called singleParent,...

  • Write a recursive function that compares two given binary trees. It returns 1 if the two...

    Write a recursive function that compares two given binary trees. It returns 1 if the two trees are different, and it returns 0 otherwise. The function signature is: int treeDiff (node "a, node *b); Write a recursive function that counts how many nodes in a tree have a single child. The function signature is: int countoneChild(node "root);

  • C++ Write a function, singleParent, that returns the number of nodes in a binary tree that...

    C++ Write a function, singleParent, that returns the number of nodes in a binary tree that have only one child. Add this function to the class binaryTreeType and create a program to test this function. (Note: First create a binary search tree.)

  • Trees Traversals Please write a Java program to traverse a binary tree in in-order and pre-order,...

    Trees Traversals Please write a Java program to traverse a binary tree in in-order and pre-order, and to plot a binary tree into a 2-dimensional array. You may write many recursive methods for this project. You are not allowed to use any existing Java classes such as ArrayList or Vector or Tree. Please stop your program if the user enters 0 as the tree selection. Your program must run the following Test Case 1 plus two more test cases to...

  • Trees Traversals Please write a Java program to traverse a binary tree in in-order and pre-order, and to plot a binar...

    Trees Traversals Please write a Java program to traverse a binary tree in in-order and pre-order, and to plot a binary tree into a 2-dimensional array. You may write many recursive methods for this project. You are not allowed to use any existing Java classes such as ArrayList or Vector or Tree. Please stop your program if the user enters 0 as the tree selection. Your program must run the following Test Case 1 plus two more test cases to...

  • Having code issues wth my C++ program. My program checks if two binary trees are similar...

    Having code issues wth my C++ program. My program checks if two binary trees are similar and if they're not they return false. My program is return true with different binary trees. Could use some help thanks #include <iostream> #include <string> using namespace std; //Struct of Nodes struct BinarySearchTree { int data; BinarySearchTree *left; BinarySearchTree *right; }; // Inserting nodes into BST BinarySearchTree* insert( BinarySearchTree* node, int val) { if (node == NULL) { BinarySearchTree *newNode = new BinarySearchTree(); newNode->data...

  • Problem 2 [35 points (155 15)]: Let Ti and T2 be two binary search trees containing the same elem...

    Problem 2 [35 points (155 15)]: Let Ti and T2 be two binary search trees containing the same elements. In this problem, you will show how to transform Ti into T2 (i.e. Ti is altered to now have the same structure as T2) through a series of rotation operations. (a) Define a binary tree to be a right-going chain if no node in the tree has a left child (in other words, the tree is a linked list formed through...

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