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
Explain about greedy technique The  greedy  method  suggests  constructing  a   solution  to  an  optimization  problem   by  a sequence of steps, every expanding a partially c

A small shop sells 280 different items. Every item is identified by a 3 - digit code. All items which start with a zero (0) are cards, all items which start with a one (1) are swee

Explain about the Structured types - Built-In Types Values of the carrier set are not atomic, consisting rather than several atomic values arranged in some way. Common illu

Define about the Structure - Container - Some containers hold elements in some sort of structure, and some don't. Containers with no structure include bags and sets. Containe

Give an algorithm to find both the maximum and minimum of 380 distinct numbers that uses at most 568 comparisons.

Differentiate between Nonpersistent and 1-persistent Nonpersistent: If the medium is idle, transmit; if the medium is busy, wait an amount of time drawn from a probability dist

What do we mean by algorithm? What are the characteristics of a good and relevant algorithm? An algorithm is "a step-by-step procedure for finishing some task'' An algorithm c

You are given two jugs, a 4-gallon one and a 3-gallon one. Neither has any measuring marker on it. There is a tap that can be used to fill the jugs with water. How can you get exac

What are the properties of an algorithsm?

Algorithm for insertion of any element into the circular queue: Step-1: If "rear" of the queue is pointing at the last position then go to step-2 or else Step-3 Step-2: make