Re: Binary Tree Depth()
- From: Christian Gollwitzer <Christian.Gollwitzer@xxxxxxxxxxxxxxx>
- Date: Thu, 01 Mar 2007 18:08:03 +0100
Stephen Howe wrote:
I dont see that.
Unless you had absolute height stored at the node.
In that case it is O(1) as you examine the root.
But suppose instead it is relative
And say for the sake of argument you have +1, 0 or -1 stored at the node indicating which side of the tree is heavier. It is all relative.
and lets say the root is 0. If you examine the root's left child, it indicates -1, if you examine the root's right child it indicates +1
Now what?
The depth is the maximum depth of all branches. So, iterate starting from the root node: (0 means equal, -1 right is deeper, +1 left):
depth=0
n=root
while (!leaf(n))
depth=depth+1
if (balance(n)=-1)
n=right(n)
else
n=left(n)
end
end
This is an O(log N) algorithm
Christian
.
- Follow-Ups:
- Re: Binary Tree Depth()
- From: Stephen Howe
- Re: Binary Tree Depth()
- References:
- Re: Binary Tree Depth()
- From: CBFalconer
- Re: Binary Tree Depth()
- From: Stephen Howe
- Re: Binary Tree Depth()
- Prev by Date: Re: What compiler would you recommend?
- Next by Date: Re: What compiler would you recommend?
- Previous by thread: Re: Binary Tree Depth()
- Next by thread: Re: Binary Tree Depth()
- Index(es):
Relevant Pages
|
Loading