d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Binary Tree Algorithm > Simple - Help Appreciated
Add Reply New Topic New Poll
Member
Posts: 19,514
Joined: Feb 21 2011
Gold: 3,877.57
Jan 17 2013 07:53pm
Remember, the only restriction on the structure of the tree is for each node to have 2 children - the balance of the tree is not restricted.
Questions so far:
How do you find the height of a binary tree?

Criteria:
Non-recursive


You may use any programming language or explain the idea behind it - thank you!
I'm willing to pay for a solution that will help me while fulfilling the criteria.
Member
Posts: 5,641
Joined: Apr 13 2006
Gold: 2.00
Jan 17 2013 08:29pm
Are you kidding? You can't google the answer to this question? Or perhaps read the book I'm sure they had you buy for the class? I assure you it is not difficult, I would suggest drawing one up or just look at the wiki page on binary tree's I'm sure that has the answer you are looking for.

This post was edited by SilverMice on Jan 17 2013 08:29pm
Member
Posts: 19,514
Joined: Feb 21 2011
Gold: 3,877.57
Jan 17 2013 08:58pm
Quote (SilverMice @ Jan 17 2013 09:29pm)
Are you kidding? You can't google the answer to this question? Or perhaps read the book I'm sure they had you buy for the class? I assure you it is not difficult, I would suggest drawing one up or just look at the wiki page on binary tree's I'm sure that has the answer you are looking for.


Sorry, it's not for a class or I would have put this probably in the homework help line - I just happen to read something on binary trees and I wondered how this could be solved in an algorithm.
Edit: Also, I have done some googling and I have read recursive solutions - hence my criteria.

This post was edited by MsRailgun on Jan 17 2013 09:00pm
Member
Posts: 5,641
Joined: Apr 13 2006
Gold: 2.00
Jan 17 2013 09:03pm
Quote (MsRailgun @ Jan 17 2013 08:58pm)
Sorry, it's not for a class or I would have put this probably in the homework help line - I just happen to read something on binary trees and I wondered how this could be solved in an algorithm.
Edit: Also, I have done some googling and I have read recursive solutions - hence my criteria.


http://stackoverflow.com/questions/11925631/non-recursive-program-to-find-minimum-height-of-binary-tree

First site in google search. Does it adequately answer your question?
Member
Posts: 10,812
Joined: Oct 15 2009
Gold: Locked
Warn: 20%
Jan 17 2013 09:12pm
Quote (MsRailgun @ Jan 17 2013 06:53pm)
Remember, the only restriction on the structure of the tree is for each node to have 2 children - the balance of the tree is not restricted.
Questions so far:
How do you find the height of a binary tree?

Criteria:
Non-recursive


well the number of bits required to number a 'row' in the tree, is equal to the number of rows above it, for example:

here the final row requires 3 bits to enumerate the row, so there are 3 rows above it


Member
Posts: 19,514
Joined: Feb 21 2011
Gold: 3,877.57
Jan 17 2013 09:39pm
Quote (SilverMice @ Jan 17 2013 10:03pm)
http://stackoverflow.com/questions/11925631/non-recursive-program-to-find-minimum-height-of-binary-tree

First site in google search. Does it adequately answer your question?


Thanks, I've also looked at it but I'd prefer the idea behind it instead of the code.

@
Quote (Azrad @ Jan 17 2013 10:12pm)
well the number of bits required to number a 'row' in the tree, is equal to the number of rows above it, for example:

here the final row requires 3 bits to enumerate the row, so there are 3 rows above it

http://s1.postimage.org/bxex2i5m7/Untitled.png


This is an interesting approach - how would you label a node that is not existent? Because what you drew is balanced on both sides; but, what if a tree has many branches with only 1 node that has 3 bits - that would mean I'd have to transcend the tree to find it :o.
Member
Posts: 10,812
Joined: Oct 15 2009
Gold: Locked
Warn: 20%
Jan 17 2013 11:34pm
Quote (MsRailgun @ Jan 17 2013 08:39pm)

This is an interesting approach - how would you label a node that is not existent? Because what you drew is balanced on both sides;  but, what if a tree has many branches with only 1 node that has 3 bits - that would mean I'd have to transcend the tree to find it :o.

Binary trees don't have nodes with 3 children. Additionally you said 'every node to have 2 children', so there won't be any "missing nodes".
Member
Posts: 19,514
Joined: Feb 21 2011
Gold: 3,877.57
Jan 18 2013 07:23am
Quote (Azrad @ Jan 18 2013 12:34am)
Binary trees don't have nodes with 3 children. Additionally you said 'every node to have 2 children', so there won't be any "missing nodes".


Take the first node, the left node is null - the right node is something. The ride node then branches off into two non-null nodes - etc.?
Member
Posts: 4,605
Joined: Sep 15 2011
Gold: 9,464.00
Jan 18 2013 05:37pm
iterative DFS/BFS, with or without backtracking, with a maxheight variable

This post was edited by irimi on Jan 18 2013 05:38pm
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll