Solved

Binary Trees question.  Checking for similar shape

Posted on 2009-04-12
6
259 Views
Last Modified: 2013-11-23
Hi I'm stuck doing this, not sure how to go about it.

If I have two binary trees, how would I check if the have the same shape? Data in the nodes doesn't matter, just if the tree structures are equal.

Any ideas on how to solve this problem?
0
Comment
Question by:ubuntuguy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 25

Accepted Solution

by:
InteractiveMind earned 280 total points
ID: 24126028
How exactly do you define 'same shape'? Suppose two trees are the reflection of each other (so they're symmetric), would you consider them the same?

You could just start with the first node on each tree; count the number of sub-nodes. If they're the same, then for each sub-node, count their sub-nodes, are they the same in each case? And so on.
0
 
LVL 1

Author Comment

by:ubuntuguy
ID: 24126073
well i meant the structures are the same. data in the nodes can be different. i.e.

                      3
                   /       \
                4        23
              /              
          34  

and
                      5
                    /       \
                 9         99
               /              
           44

have the same structure.  on the other hand

                      3
                    /       \
                 4        23
               /              
           34  


and

                      3
                    /       \
                 4        23
               /  \            
           34   55

dont have the same structure.  
               
0
 
LVL 6

Expert Comment

by:crossdev
ID: 24126089
If you want a binary tree, meaning that it could be unbalanced you'll need to create one.  Otherwise there is an implmenetation of a Red / Black tree which will stay balanced called TreeMap

http://java.sun.com/j2se/1.4.2/docs/api/java/util/TreeMap.html
0
Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

 
LVL 6

Expert Comment

by:crossdev
ID: 24126096
Ignore that last post.

You could create an algorithm that counted the number of leaf nodes and then checked the length of each leaf.  

So the top exampl has 2 leafs with a depth os 3 & 2 :  [3,2]

The fourth one has three leafs with depths of [3,3,2]

You could use this signature to determine.
0
 
LVL 6

Assisted Solution

by:crossdev
crossdev earned 20 total points
ID: 24126106
Keep in mind this sort of exercise is to test your ability to trouble shoot.  The solution itself has little usefulness.  however your ability to solve it is what matters.  If you find such a problem difficult or impossible, consider another career path.  

I should not that the solution I gave will get you on the right track but is not the entire solution.  I don;t like solving peoples homework.  Iwill nudge you in the right direction.  If anyone gives you the solution, I hope they employ you caue I'll weed you out.  Short cuts only affect you.  If you take the time to figure it out you'll be doing yourself no harm, but if you try to short cut the process you will only hurt your learning process.

Good luck.
0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 200 total points
ID: 24126898
same( a,b){
  if( a is a terminal node and b is a terminal node ) return true
  if( a is an interior node and b is an interior node and same(a.left,b.left) and same(a.right,b.right) ) return true
  else return false;
}
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The greatest common divisor (gcd) of two positive integers is their largest common divisor. Let's consider two numbers 12 and 20. The divisors of 12 are 1, 2, 3, 4, 6, 12 The divisors of 20 are 1, 2, 4, 5, 10 20 The highest number among the c…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This video teaches viewers about errors in exception handling.

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question