# Calculating tree height for random tree

Page 1 of 1

## 6 Replies - 4401 Views - Last Post: 28 November 2008 - 03:33 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=73990&amp;s=0990dc95d2f3a18fa1d026f2d87a67a1&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 edirol

• New D.I.C Head

Reputation: 0
• Posts: 8
• Joined: 18-October 08

# Calculating tree height for random tree

Posted 28 November 2008 - 01:16 PM

I'm writing a random tree and would like to track the height of the subtrees in the node. What method should this count be tracked in? Should this be a recursive call everytime a node is added?
Is This A Good Question/Topic? 0

## Replies To: Calculating tree height for random tree

Reputation: 3
• Posts: 107
• Joined: 03-October 08

## Re: Calculating tree height for random tree

Posted 28 November 2008 - 01:27 PM

Like so:

findHeight(BinaryTree aBinaryTree)
{
if (aTree.isEmpty())
return 0;
else
{
int heightLeft=height(aTree.getLeftSubtree());
int heightRight=height(aTree.getRightSubtree());
if (heightLeft >= heightRight)
return heightLeft+1;
else
return heightRight+1;
}

### #3 edirol

• New D.I.C Head

Reputation: 0
• Posts: 8
• Joined: 18-October 08

## Re: Calculating tree height for random tree

Posted 28 November 2008 - 01:31 PM

name edirol: date 28, Nov, 2008 12:31 pm

so i'll need a function findHeight that get calls everytime I add a node?

edirol

Sadaiy, on 28 Nov, 2008 - 12:27 PM, said:

Like so:

findHeight(BinaryTree aBinaryTree)
{
if (aTree.isEmpty())
return 0;
else
{
int heightLeft=height(aTree.getLeftSubtree());
int heightRight=height(aTree.getRightSubtree());
if (heightLeft >= heightRight)
return heightLeft+1;
else
return heightRight+1;
}

Reputation: 3
• Posts: 107
• Joined: 03-October 08

## Re: Calculating tree height for random tree

Posted 28 November 2008 - 02:50 PM

No, you create the tree first. Then when you want to find the height you run the findHeight function once(it is recursive).

### #5 edirol

• New D.I.C Head

Reputation: 0
• Posts: 8
• Joined: 18-October 08

## Re: Calculating tree height for random tree

Posted 28 November 2008 - 02:56 PM

I don't have a clear conception of when you should find height in a random binary tree. Are you able to explain this?

Sadaiy, on 28 Nov, 2008 - 01:50 PM, said:

No, you create the tree first. Then when you want to find the height you run the findHeight function once(it is recursive).

### #6 edirol

• New D.I.C Head

Reputation: 0
• Posts: 8
• Joined: 18-October 08

## Re: Calculating tree height for random tree

Posted 28 November 2008 - 03:20 PM

How do I know when I want to find the height?

edirol, on 28 Nov, 2008 - 01:56 PM, said:

I don't have a clear conception of when you should find height in a random binary tree. Are you able to explain this?

Sadaiy, on 28 Nov, 2008 - 01:50 PM, said:

No, you create the tree first. Then when you want to find the height you run the findHeight function once(it is recursive).

Reputation: 3
• Posts: 107
• Joined: 03-October 08

## Re: Calculating tree height for random tree

Posted 28 November 2008 - 03:33 PM

Well, I don't know when you want to find the height.. Usually after the tree has been comepletely built (all nodes have been added), then you can find the height of it if you want too.