Question

In: Computer Science

Write an algorithm to determine if two binary trees are identical when the ordering of the...

Write an algorithm to determine if two binary trees are identical when the ordering of the subtrees for a node is ignored. For example, if a tree has root node with value R, left child with value A and right child with value B, this would be considered identical to another tree with root node value R, left child value B, and right child value A. Make the algorithm as efficient as you can. Analyze your algorithm’s running time. How much harder would it be to make this algorithm work on a general tree?

Solutions

Expert Solution

/* To identify if two trees are identical, we need to traverse both trees simultaneously,
and while traversing we need to compare data and children of the trees. */
/*
Algorithm:

IdenticalTree(tree1, tree2)
1. If both trees are empty then return 1.
2. Else If both trees are non -empty
(a) Check data of the root nodes (tree1->data == tree2->data)
(b) Check left subtrees recursively i.e., call IdenticalTree(
tree1->left_subtree, tree2->left_subtree)
(c) Check right subtrees recursively i.e., call IdenticalTree(
tree1->right_subtree, tree2->right_subtree)
(d) If a,b and c are true then return 1.
3 Else return 0 (one is empty and other is not)

*/


// C++ Implimentation of above algorithm
  
#include <iostream>
using namespace std;
  
// BST node
struct Node {
int data;
struct Node* left;
struct Node* right;
};
  
// Utility function to create a new Node
struct Node* newNode(int data)
{
struct Node* node = (struct Node*)
malloc(sizeof(struct Node));
node->data = data;
node->left = NULL;
node->right = NULL;
  
return node;
}
  
// Function to perform inorder traversal
void inorder(Node* root)
{
if (root == NULL)
return;
  
inorder(root->left);
  
cout << root->data << " ";
  
inorder(root->right);
}
  
// Function to check if two If two binary trees
// are identical when the ordering of subtrees for the nodes is ignored.
int isIdentical(Node* root1, Node* root2)
{
// Check if both the trees are empty
if (root1 == NULL && root2 == NULL)
return 1;
// If any one of the tree is non-empty
// and other is empty, return false
else if (root1 != NULL && root2 == NULL)
return 0;
else if (root1 == NULL && root2 != NULL)
return 0;
else { // Check if current data of both trees equal
// and recursively check for left and right subtrees
if (root1->data == root2->data && ((isIdentical(root1->left, root2->left)
&& isIdentical(root1->right, root2->right) )||(isIdentical(root1->left, root2->right)&& isIdentical(root1->right, root2->left))))
return 1;
else
return 0;
}
}
  
// Driver code
int main()
{
struct Node* root1 = newNode(5);
struct Node* root2 = newNode(5);
root1->left = newNode(3);
root1->right = newNode(8);
root1->left->left = newNode(2);
root1->left->right = newNode(4);
  
root2->left = newNode(8);
root2->right = newNode(3);
root2->right->left = newNode(4);
root2->right->right = newNode(2);
  
if (isIdentical(root1, root2))
cout << "Both Binary Trees are identical";
else
cout << "Binary Trees are not identical";
  
return 0;
}


/* Complexity of the identicalTree() will be according to
the tree with lesser number of nodes. Let number of nodes in
two trees be m and n then the complexity of identicalTree() is O(m) where m < n.
*/



Related Solutions

Write a python function comparing two binary trees and returns whethere they are same. Input two...
Write a python function comparing two binary trees and returns whethere they are same. Input two binary tress and output true or false. True= They are the same, False= They are not . Test the function .
Write a function in Python that adds two Binary Trees together. The inputs of your function...
Write a function in Python that adds two Binary Trees together. The inputs of your function should be two binary trees (or their roots, depending on how you plan on coding this), and the output will be one Binary Tree that is the sum of the two inputted. To add Binary Trees together: Add the values of nodes with the same position in both trees together, this will be the value assigned to the node of the same position in...
Write a program in C language that implements the logic of Binary Trees with the following...
Write a program in C language that implements the logic of Binary Trees with the following requirements: 1- Elements of the tree should be read from the user. 2- Keep the binary tree heigh-balanced that is the absloute value of ((hight of left sub-tree) - ( height of right sub-tree)) should not be greater than 1. If so, then the binary tree is no longer balanced. Hint: The best approach is to maintain balance during insertion. *Remember, we are talking...
Need in JAVA. You are to use Binary Trees to do this Program. Write a complete...
Need in JAVA. You are to use Binary Trees to do this Program. Write a complete program, which will process several sets of numbers: For each set of numbers you should: 1. Create a binary tree. 2. Print the tree using “inorder”, “preorder”, and “postorder”. 3. Call a method Count which counts the number of nodes in the tree. 4. Call a method Children which prints the number of children each node has. 5. Inset and delete several nodes according...
Research the topic of binary search trees. Write a brief summary of your understanding of this....
Research the topic of binary search trees. Write a brief summary of your understanding of this. Design a simple program, using pseudocode, that performs a search of a binary search tree.. In your own words, explain how a binary search tree works using graph theory terminology. Construct a binary search tree for any alphabetically ordered list of five words. Represent it visually as a graph. Discuss any characteristics you note about the graph you created in part (d). In your...
Write a version of the binary search algorithm that can be used to search a string...
Write a version of the binary search algorithm that can be used to search a string vector object. Also, write a program to test your algorithm. (Use the selection sort algorithm you developed in Programming Exercise 12 to sort the vector.) Your program should prompt the user to input a series of strings, ending the input stream with zzz. The program should then prompt the user to search for a specific string in the list. If the string is found...
Binary Search Algorithm a.) Create a Java application that utilizes the "Binary Search Algorithm" presented in...
Binary Search Algorithm a.) Create a Java application that utilizes the "Binary Search Algorithm" presented in chapter 19 (NOT Java's pre-built binarySearch() method from imported Java library) to search for an integer in a random array of size 30 in the range of 0 to 1000 inclusive. You should use Java's random number generator to randomize the numbers in your array. b.) The application's main() method should display unsorted array and sorted array, prompt user for a search key, allow...
Write a method for binary tree in Python that can determine whether a binary tree is...
Write a method for binary tree in Python that can determine whether a binary tree is a binary search tree or not. The input should be a binary tree. The output should be true or false. True= binary tree meets the criteria to be a binary search tree. False= does not meet the criteria to be a binary search tree.
Write a program in C++ to implement Binary Search Algorithm. Assume the data given in an...
Write a program in C++ to implement Binary Search Algorithm. Assume the data given in an array. Use number of data N at least more than 10. The function Binary Search should return the index of the search key V.
Make a Binary search program for C# and write algorithm and explain it in easy words...
Make a Binary search program for C# and write algorithm and explain it in easy words also show output and input
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT