1

I am a bit lost in JavaScript. I have this structure:

{
    "value": 5,
    "children": [{
        "value": 18,
        "children": [{
            "value": 27,
            "children": []
        }, {
            "value": 4,
            "children": []
        }]
    }, {
        "value": 2,
        "children": []
    }]
}

How can I get the highest value in the tree using JavaScript?

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
iskarlet_
  • 23
  • 2
  • 1
    what height value you want to know? – chandu Jun 17 '14 at 11:15
  • What does this have to do with _"Binary"_? – Cerbrus Jun 17 '14 at 11:15
  • in this example, your desired end result would be - 27? or am I not understanding your question? – webkit Jun 17 '14 at 11:16
  • well, maybe i am mistaken to use the binary(just used this based on my searches, i have no background in dealing with this). -Cerbrus, – iskarlet_ Jun 17 '14 at 11:17
  • i guess 27, since the way i look at it, it is the highest value, -webkit – iskarlet_ Jun 17 '14 at 11:17
  • @Cerbrus: A [binary tree](http://en.wikipedia.org/wiki/Binary_tree) is a tree structure in which each node can have at most two children. – T.J. Crowder Jun 17 '14 at 11:18
  • @user2641282: To notify a user you've replied, use `@`, not `-`. – T.J. Crowder Jun 17 '14 at 11:18
  • Do you only want the value `27`, or the actual element that has the highest value? (In this case: `{"value":27, "children":[]}`) @T.J.Crowder: ooooh, okay. – Cerbrus Jun 17 '14 at 11:18
  • Highest value of Parent or Childern? – Dastagir Jun 17 '14 at 11:20
  • @Cerbrus yeah, i was instructed to look for the highest value, and have a javascript code to display through an alert message. Hope things are still clear, i really have no idea with json and trees, but i did some searches still not clear – iskarlet_ Jun 17 '14 at 11:21
  • @user2641282: You're almost certainly not using JSON (unless you're reading text from a file or URL). That's just JavaScript. – T.J. Crowder Jun 17 '14 at 11:21
  • @T.J.Crowder it was saved on a .json format, but still, how can i display the highest value via alert box? – iskarlet_ Jun 17 '14 at 11:23

4 Answers4

1

In this specific case you'd might want to use this:

var baseObject = {
    "value": 5,
    "children": [{
        "value": 18,
        "children": [{
            "value": 27,
            "children": []
        }, {
            "value": 4,
            "children": []
        }]
    }, {
        "value": 2,
        "children": []
    }]
};

function getHighestValue(obj) {
    var res = obj.value;
    for(var i in obj.children) {
        res = Math.max(res, getHighestValue(obj.children[i]));
    }
    return res;
}

alert(getHighestValue(baseObject));

http://jsfiddle.net/qc9R4/1/

Hauke P.
  • 2,695
  • 1
  • 20
  • 43
0

if understand you correct should look something like this.

var highestValue = JSONresponse.value;

HighVal(JSONresponse);

function HighVal(JSON) 
{ 
   if(highestValue < JSON.value)
   {
     highestValue = JSON.value
   }
   for(i=0;i<JSON.children.lenght;i++)
   {
     HighVal(JSON.children[i]);
   }
 }
Kris Georgiev
  • 140
  • 1
  • 8
0

Another way of doing this, if your object tree pattern is the same,

Stringify object and do regular expression to fetch all the integer values of this pattern "value: {n}" and then find the max value.

  var jsono = {
        "value": 5,
        "children": [{
            "value": 18,
            "children": [{
                "value": 27,
                "children": []
            }, {
                "value": 4,
                "children": []
            }]
        }, {
            "value": 2,
            "children": []
        }]
    }
    var maxvalue;
    JSON.stringify(jsono).match(/\"value\":\s*(\d+)/g).map(function(value){ return value.match(/(\d+)/g).map(function(value){ maxvalue =  Math.max(maxvalue || 0,value);});});
    alert(maxvalue);

http://jsfiddle.net/6R9p3/1/

Dastagir
  • 982
  • 8
  • 14
-1

I won't write the code for you, but basically, it's the same with any other language.

You traverse through every right child until you know that it's the end node.

How to use JavaScript to access JSON?

var tree = 
{
  parent : [
    {
      //child1 details
    },
    {
      //child2 details
    },
  ]
}

For JSON key access, use tree.<key> (dot) or tree['key']. In this case, tree.parent or tree["parent"].

For Array access, use indices. Since parent is an array, you can access the children by tree.parent[0] or tree['parent'][0].

I prefer the dot method though, to distinguish visually JSONs and arrays.

Also, you need something to distinguish a right child and a left child. You can either make it a convention to make the right child as the [0] index in the array, or you can add another value per node that says that its right or left.

Zaenille
  • 1,384
  • 9
  • 16