Solved

Binary Trees question.  Checking for similar shape

Posted on 2009-04-12
6
252 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
6 Comments
 
LVL 25

Accepted Solution

by:
InteractiveMind earned 280 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 6

Expert Comment

by:crossdev
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
countHi challenge 25 84
json example 39 110
pairs challenge 5 44
sites similar to codingbat to improve coding hanson skills 3 19
Okay. So what exactly is the problem here? How often have we come across situations where we need to know if two strings are 'similar' but not necessarily the same? I have, plenty of times. Until recently, I thought any functionality like that wo…
One of Google's most recent algorithm changes affecting local searches is entitled "The Pigeon Update." This update has dramatically enhanced search inquires for the keyword "Yelp." Google searches with the word "Yelp" included will now yield Yelp a…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

771 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now