Home  |  About  | Last |  Submit  |  Contact
AllQuests.com

Previous Question:  Need transfer software for my Ipod  Audio, Video and AnimationNext Question:  Data storage in MySQL  Algorithms Data Structures

Question Function to calculate closest leaf in binary tree ( CodeGuru Forums Algorithms Data Structures )
Updated: 2008-08-12 06:10:05 (2)
Function to calculate closest leaf in binary tree

I have to implement a method in java that calculates the depth of the closest leaf in a binary tree.

In PSEUDOCode can someone show me how this would be done, I need to understand it step by step so that I can start coding it, but right now i dont know where to start, and Pseudocode would be helpful.

I undertstand that the height of a tree is the path from the root to the furthest leaf, to calculate this you workout the height for the left subtree + height of Right subtree plus 1 for the root node. How would I calculate the closest leaf,oh yeah and the root level must be 1.


Thank You. PLEASE HELP PLEASE PLEASE AND PLEASE

Answers: Function to calculate closest leaf in binary tree ( CodeGuru Forums Algorithms Data Structures )
Function to calculate closest leaf in binary tree

WOULD THIS FUNCTION BE CORRECT

int depth_of_closest_leaf(Node *root) {
int result = +Infinity;
if(root->left!=0)
result = 1+depth_of_closest_leaf(root->left);
if(root->right!=0)
result = min(result, 1+depth_of_closest_leaf(root->right));
if(result==+Infinity)
result = 1; // the root is a leaf
return result;
}

And how would I represent it in java.

MIbrahim

Function to calculate closest leaf in binary tree

What, precisely, do you mean by the closest leaf? If I understand right, it's the lowest-depth node which does not point to any other nodes.
If that is correct, then you code is correct.
unfortunately, I'm not a java programmer, so I can't actually contribute anything to the solution, except that as I believe you can't use pointers, you'll need to use
Code:
if (Root.Left != NULL) {


Hope that's of some help.



Previous Question:  Need transfer software for my Ipod  SitePoint Forums  Audio, Video and AnimationNext Question:  Data storage in MySQL  CodeGuru Forums  Algorithms Data Structures

- Source: Function to calculate closest leaf in binary tree CodeGuru Forums Algorithms Data Structures
- Previous Question: Need transfer software for my Ipod SitePoint Forums Audio, Video and Animation
- Next Question: Data storage in MySQL CodeGuru Forums Algorithms Data Structures





AllQuests.com


Best dedicated servers   Top dedicated servers   Cheap dedicated servers   Linux dedicated servers   Windows dedicated servers   Unmetered dedicated servers