here is the soln.....
http://ideone.com/Qu0a0#view_edit_box

however dere's a problem which needs to be resolved....
the prob is if the diff is 0 then due to the use of abs() its getting
overriden....
i'm not able to find way out of it...
ny suggestns are welcum..

Regards,
PAYAL GUPTA.

On Mon, Feb 20, 2012 at 4:54 PM, Supraja Jayakumar <suprajasank...@gmail.com
> wrote:

> Hi
>
> Question is given a binary tree and a key K, code to find the node with
> the closest value.
>
> I'd be happy to receive some feedback about my solution too.
>
> Pls find the code below:
>
> class FindingClosestNodeInTree {
> private static double difference = 0.0;
> private static doule key = 0.0;
> int main() {
>     BinaryTree bt;
>     bt.insert(20.43);
>     bt.insert(12.78);
>     bt.insert(19.89);
>     bt.insert(32.69);
>     bt.insert(2.54);
>     cout << "Please provide the key value" << endl;
>     cin >> key;
>     const Node &closestNode = closestValue(bt);
>     cout << << "Node that has the closest value to " <<  <<
> closestNode.value;
> return 1;
> }
> const Node & closestValue(const BinaryTree &node) {
> if(node==null)
>     return;
>
> int val = node.value;
> int currDiff = val > key ? val-key:key-val;
> difference = currDiff > difference ? currDiff:difference;
> if(node.left!=null)
>     closestValue(node.left);
> if(node.right!=null)
>     closestValue(node.right);
> return difference;
> }
> }
>
> Thanks
> Supraja J
>
> --
> You received this message because you are subscribed to the Google Groups
> "Algorithm Geeks" group.
> To post to this group, send email to algogeeks@googlegroups.com.
> To unsubscribe from this group, send email to
> algogeeks+unsubscr...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/algogeeks?hl=en.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.

Reply via email to