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
Algorithm for determining strongly connected components of a Graph: Strongly Connected Components (G) where d[u] = discovery time of the vertex u throughout DFS , f[u] = f

Define a sparse metrics. A matrix in which number of zero entries are much higher than the number of non zero entries is known as sparse matrix. The natural method of showing m

Define Complete Binary Tree Complete Binary Tree:- A whole binary tree of depth d is that strictly binary tree all of whose leaves are at level D.

Explain the term totalling To add up a series numbers the subsequent type of statement must be used: Total = total + number  This literally means (new) total = (old) t

This question is based on the requirements of a system to record band bookings at gigs. (A 'gig' is an event at which one or more bands are booked to play). You do not need to know

A graph is a mathematical structure giving of a set of vertexes (v1, v2, v3) and a group of edges (e1, e2, e3). An edge is a set of vertexes. The two vertexes are named the edge en

(1) Sort a list of distinct numbers in ascending order, using the following divide- and-conquer strategy (Quicksort): divide the list of numbers into two lists: one that contains a

How will you represent a max-heap sequentially? Max heap, also known as the descending heap, of size n is an almost complete binary tree of n nodes such that the content of eve

whate is meant by the term heuristic

What is an unreachable code assertion An unreachable code assertion can be placed at the default case; if it's every executed, then program is in an erroneous state. A loop in