Algorithm for similar binary tree, Data Structure & Algorithms

Q. The two Binary Trees are said to be similar if they are both empty or if they are both non- empty and left and right sub trees are similar. Write down an algorithm to determine if two Binary Trees are similar.                                                                                                       

Ans:

We assume that the two trees are tree1 and tree2. an algorithm to determine if two Binary trees are similar or not is as follows:

similar(*tree1,*tree2)

{

while((tree1!=null) && (tree2!=null))

{

if((tree1->root) == (tree2->root)) similar(tree1->left,tree2->left); similar(tree1->right,tree2->right);

elseif(tree->root!=   tree2->node){printf("Trees

are not equal");

return;}

}

}

Posted Date: 7/10/2012 3:59:35 AM | Location : United States







Related Discussions:- Algorithm for similar binary tree, Assignment Help, Ask Question on Algorithm for similar binary tree, Get Answer, Expert's Help, Algorithm for similar binary tree Discussions

Write discussion on Algorithm for similar binary tree
Your posts are moderated
Related Questions
A LGORITHM (Deletion of an element from the linked list) Step 1  Begin Step 2  if the list is empty, then element cannot be deleted Step 3  else, if the element to be del

Q. The two Binary Trees are said to be similar if they are both empty or if they are both non- empty and left and right sub trees are similar. Write down an algorithm to determine

1. Write a pseudocode algorithm to print the numbers from 1 to 10, and then from 10 to 1, using exactly one loop. 2. The function contains() takes a food as an argument and tell

that will determine the volume of the sphere or the volume of cone or volume of pyramid depending on the choice of the user

If a node having two children is deleted from a binary tree, it is replaced by?? Inorder successor

Step-1: For the current node, verify whether it contain a left child. If it has, then go to step-2 or else go to step-3 Step-2: Repeat step-1 for left child Step-3: Visit (th

how to write a function of area of a circle using python

Q. Execute your algorithm to convert the infix expression to the post fix expression with the given infix expression as input Q = [(A + B)/(C + D) ↑ (E / F)]+ (G + H)/ I

Q. Define a method for keeping two stacks within a single linear array S in such a way that neither stack overflows until entire array is used and a whole stack is never shifted to

1.  You are required to hand in both a hard copy and an electronic copy of the written report on the project described in A, including all the diagrams you have drawn.  2.  You