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
Let us assume a file of 5 records that means n = 5 And k is a sorted array of keys of those 5 records. Let key = 55, low = 0, high = 4 Iteration 1: mid = (0+4)/2 = 2

In what ways we can get the context sensitive F1 help on a field?' Data element documentation. Data element additional text in screen painter. Using the process on help r

Q. Give the algorithm to build a binary tree where the yields of preorder and post order traversal are given to us.

One of the main problems with the linear queue is the lack of appropriate utilization of space. Assume that the queue can store 100 elements & the complete queue is full. Thus, it

Records are mapped onto a computer store by simply juxtaposing their elements. The address of a component (field) r relative to the origin address of the record r is named the fiel

Prove that uniform cost search and breadth- first search with constant steps are optimal when used with the Graph-Search algorithm (see Figure). Show a state space with varying ste

Adjacency list representation An Adjacency list representation of Graph G = {V, E} contains an array of adjacency lists mentioned by adj of V list. For each of the vertex u?V,

Prim's algorithm employs the concept of sets. Rather than processing the graph by sorted order of edges, this algorithm processes the edges within the graph randomly by building up

Indexed Sequential Files An index is inserted to the sequential file to provide random access. An overflow area required to be maintained to permit insertion in sequence. I

implementation of fast fourier transforms for non power of 2