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?

12
  • 1
    what height value you want to know? Commented Jun 17, 2014 at 11:15
  • What does this have to do with "Binary"? Commented Jun 17, 2014 at 11:15
  • in this example, your desired end result would be - 27? or am I not understanding your question? Commented Jun 17, 2014 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, Commented Jun 17, 2014 at 11:17
  • i guess 27, since the way i look at it, it is the highest value, -webkit Commented Jun 17, 2014 at 11:17

4 Answers 4

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/

Sign up to request clarification or add additional context in comments.

2 Comments

thank you, this will help, ill just read on this.
Herp derp, I'm blind o.O This works perfectly, +1.
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]);
   }
 }

Comments

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/

Comments

-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.

3 Comments

thank you, things are getting clear now
You're going to have to mention recursion, there...
@Cerbrus yes, recursion is needed here

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.