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
What is a Spanning tree of a graph? A Spanning Tree is any tree having of vertices of graph tree and some edges of graph is known as a spanning tree.

Can a Queue be shown by circular linked list with only single pointer pointing to the tail of the queue? Yes a Queue can be shown by a circular linked list with only single p

DEPTH FIRST SEARCH (DFS) The approach adopted into depth first search is to search deeper whenever possible. This algorithm frequently searches deeper through visiting unvisite

Explain All-pair shortest-paths problem Given a weighted linked graph (undirected or directed), the all pairs shortest paths problem asks to find the distances (the lengths of

Byteland county is very famous for luminous jewels. Luminous jewels are used in making beautiful necklaces. A necklace consists of various luminous jewels of particular colour. Nec

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

insertion and deletion in a tree

what are the charaterstics to determine weather an algorithm is good or not? explain in detail

Q. Assume that we have separated n elements in to m sorted lists. Explain how to generate a single sorted list of all n elements in time O (n log m )?

A Red-Black Tree (RBT) is a type of Binary Search tree with one extra bit of storage per node, i.e. its color that can either be red or black. Now the nodes can have any of the col