3 Replies - 6792 Views - Last Post: 27 May 2007 - 08:10 AM Rate Topic: -----

#1 waqaraziz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 24-May 07

Find Depth of a Tree in C++ Code is required

Posted 24 May 2007 - 05:22 AM

Please help in writting C++ code to find the depth of a tree

This post has been edited by waqaraziz: 24 May 2007 - 05:23 AM

Is This A Good Question/Topic? 0
  • +

Replies To: Find Depth of a Tree in C++ Code is required

#2 William_Wilson  Icon User is offline

  • lost in compilation
  • member icon

Reputation: 205
  • View blog
  • Posts: 4,807
  • Joined: 23-December 05

Re: Find Depth of a Tree in C++ Code is required

Posted 24 May 2007 - 07:53 AM

we here at DIC have a strict policy that you must provide us with some code, otherwise known as "good faith" code, in order to recieve help. Please show us what you have attempted and we will be glad to help.
Was This Post Helpful? 0
  • +
  • -

#3 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Find Depth of a Tree in C++ Code is required

Posted 26 May 2007 - 02:38 PM

To find the depth of a tree find its deepest node. How do you find its deepest node. Traverse the tree and count how far you had to go to get there.
Was This Post Helpful? 0
  • +
  • -

#4 chris.tkd  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 74
  • Joined: 26-September 06

Re: Find Depth of a Tree in C++ Code is required

Posted 27 May 2007 - 08:10 AM

This is how to find the heigh of a tree abstractly

 

heigh(t: node)
-->
	if t = null
	   return 0
	else
	   return (1 + max(height(t.left), height(t.right))




where max is a function that finds the maximum of two inputs , eg

max(t1, t2)
--> 
	if t1.val > t2.val
	   return t1
	else
	   return t2



youll have to translate this into c++ but it shouldnt be hard.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1