Jump to content
xisto Community
varalu

Data Structures -- Binary Tree -- Structurally Same Find if 2 binary tress are structurally same??

Recommended Posts

Given two binary trees, find out whether a tree is structurally same to the other.

Structurally same means that the two trees look alike or a tree looks alike a subtree of the other tree.

 

Look for time and space complexity.

 

 

Some more for readers

What is a binary tree?

A binary tree is made of nodes, where each node contains a "left" pointer, a "right" pointer, and a data element. The "root" pointer points to the topmost node in the tree. The left and right pointers recursively point to smaller "subtrees" on either side. A null pointer represents a binary tree with no elements -- the empty tree. The formal recursive definition is: a binary tree is either empty (represented by a null pointer), or is made of a single node, where the left and right pointers (recursive definition ahead) each point to a binary tree.

 

post-50968-1193484596_thumb.gifA "binary search tree" (BST) or "ordered binary tree" is a type of binary tree where the nodes are arranged in order: for each node, all elements in its left subtree are less-or-equal to the node (<=), and all the elements in its right subtree are greater than the node (>). The tree shown above is a binary search tree -- the "root" node is a 5, and its left subtree nodes (1, 3, 4) are <= 5, and its right subtree nodes (6, 9) are > 5. Recursively, each of the subtrees must also obey the binary search tree constraint: in the (1, 3, 4) subtree, the 3 is the root, the 1 <= 3 and 4 > 3. Watch out for the exact wording in the problems -- a "binary search tree" is different from a "binary tree".

 

The nodes at the bottom edge of the tree have empty subtrees and are called "leaf" nodes (1, 4, 6) while the others are "internal" nodes (3, 5, 9).

For more about binary trees... Please follow the below links:

https://en.wikipedia.org/wiki/Binary_tree

http://cslibrary.stanford.edu/110/BinaryTrees.html

https://xlinux.nist.gov/dads//HTML/binarytree.html

Share this post


Link to post
Share on other sites

Two solutions spring to mind. Firstly, you could pass the root nodes of both trees into a recursive function and compare their children. If they have an identical number & position of children, continue to recurse, otherwise unravel and report "different". If the function returns with no report of difference you have identical trees.Alternatively, if you don't have both trees at the same time to compare, you could create a model of a tree using bit strings to denote the presense or absence of nodes. E.g. a fully populated three-level tree would be represented as 1111111 ... leave off the bottom leftmost child and you get 1110111. Then run your final bit string through a hash (e.g. md5sum) and you have a fingerprint of your tree structure. This way you'd be able to store the structure of a tree in a few bytes for future use.

Share this post


Link to post
Share on other sites

Hi Pointybirds...

Your solution works for trees that are exactly the same. But structurally same also means that the sub-tree can also be the same... So we have to do some changes in your algorithm...may be instead of passing the root to both the trees...the node which has the probability of becoming a structurally sam ecan be passed.

Here is one solution suggested by one of my friends.

bool checksimilarity(node *p1, node *p2){	  		if (checkequal (p1, p2))			 return true;		if (checksimilarity(p1->left, p2))			 return true;		if (checksimilarity(p1->right, p2))			 return true;		return false;}

bool checkequal(node *p1, node *p2){	   //checks whether the two nodes are pointing to null	   if (p1 == null && p2 == null)				return true;	   //checks whether the two nodes in the trees are not pointing to another node.	   //If they are not pointing, then the similarity of the tree goes wrong. so, return false.	   if ((p1 == null && p2 != null) || (p1 != null && p2 == null)				return false;	   //The trees are similar till this node, so check for their left and right subtrees.	   return (checkequal(p1->left, p2->left) && checkequal(p1->right, p2->right));}

No additional space required.
Time complexity O(nlogn).

This piece of code works good in all case... do post your comments.

Share this post


Link to post
Share on other sites

The first option that comes to my mind is send the root node into the one tree. Search the tree for this node. If that node is found, check the children to see if they are the same. If they are not, you can end. Otherwise continue to traverse. This will determine if the trees are the same or one is a subset of the other. Overall, it is time O(n) since there is no guarentee the binary tree is balanced.

Share this post


Link to post
Share on other sites

The first option that comes to my mind is send the root node into the one tree. Search the tree for this node. If that node is found, check the children to see if they are the same. If they are not, you can end. Otherwise continue to traverse. This will determine if the trees are the same or one is a subset of the other. Overall, it is time O(n) since there is no guarentee the binary tree is balanced.

I am not sure it will work : it is based on the assumption that you can 'find' a node from one tree in another tree based on this node's value alone. This would actually be the case if we were looking for an algorithm which checks that two binary tree are equal or that one is equal to a subtree of the other.

However, the problem here is to find whether the two trees are structurally equal, meaning that two nodes should be compared only by checking that they have the same children structure (none, only left, only right, or both) regardless of the value.

 

For me, I cannot think of a quicker solution than Varalu :)

Share this post


Link to post
Share on other sites

sorry I have many questions :D:D

1.How to write the code of the Add function that takes a letter as a parameter and adds it to the tree in the right place.

2.How to write the code of the Search function that takes a letter as a parameter and returns true if the letter exists and returns false otherwise.

3. How to write the code of the Traverse_Left_Root_Right function the traverse the tree in the order left-root-right and writes the letters in the tree. 

4. How towrite the code of the Traverse_Root_Left_Right function the traverse the tree in the order root-left-right and writes the letters in the tree. 

5. How to write the code of the Traverse_Left_Right_Root function the traverse the tree in the order left-right-root and writes the letters in the tree. 

6. How to write the code of the Delete function that takes a letter as a parameter and deletes it from the tree.  

sorry if I asked to much but I hope to answer me quickly as soon as possible

-question by mona

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.