I have One Root Node "A" in a tree view and "A" node may have any number of child node and so on. I have to search a node in this tree and want to know the level of searched node in given tree. need a logical implementation in java/Java script using Collection framework and core java or JQuery.
bellow is the JSON data in which i need search.
var data=[{"id":"a","children":[{"id":"b","children":[{"id":"c","children":[{"id":"f","children":[{"id":"h"}]}]},{"id":"d"},{"id":"e"}]}]}]
here in above JSON if i want to get object of id f, I am using method findNode(data,"f") that will give me object of node f, but i also want level of node f which is level 3, how could i get it. and suggest me another way to search node that will increase search performance, this approach is very slow because recursively i have to hit the database to get next record in java and in java script the amount of data/JSON will become large that may cause crash the browser.
function findNode(node, nodeId) {
var childrenLen, found;
if (node.id === nodeId) {
return node;
} else if (node.children) {
childrenLen = node.children.length;
while(childrenLen--) {
found = findNode(node.children[childrenLen], nodeId);
if(found) {
return found;
}
}
}
}