6 Replies - 3818 Views - Last Post: 28 November 2008 - 03:33 PM Rate Topic: -----

#1 edirol  Icon User is offline

  • New D.I.C Head

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

Calculating tree height for random tree

Post icon  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

#2 Sadaiy  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • 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;
}
Was This Post Helpful? 0
  • +
  • -

#3 edirol  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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

View PostSadaiy, 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;
}

Was This Post Helpful? 0
  • +
  • -

#4 Sadaiy  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • 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).
Was This Post Helpful? 0
  • +
  • -

#5 edirol  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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?

View PostSadaiy, 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).

Was This Post Helpful? 0
  • +
  • -

#6 edirol  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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?

View Postedirol, 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?

View PostSadaiy, 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).

Was This Post Helpful? 0
  • +
  • -

#7 Sadaiy  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1