简体   繁体   English

树递归:如何获取所选树节点的父根

[英]Tree Recursion: How to get the parent root of the selected tree node

I have a tree-like structure of a json object 我有一个json对象的树状结构

{
  "saxena": {
    "chewning": {
      "betten": {},
      "ching": {},
      "kelley": {}
    },
    "kobrinsky": {
      "karniely": {},
      "naveh": {},
      "rozenfeld": {},
      "shalom": {}
    },
    "schriever": {
      "brinker": {},
      "mcleland": {},
      "merrick": {}
    },
    "vacant": {
      "akers": {},
      "carlton": {
        "marvin": {}
      },
      "fox": {
        "glover": {
          "clements": {},
          "koya": {}
        },
        "holden": {}
      }
    }
  },
  "bill": {
    "phil": {
      "bob": {},
      "smith": {},
      "hello": {}
    },
    "bye": {
      "ok": {},
      "hmm": {},
      "no": {},
      "alright": {}
    }
  }
}

The root names are saxena and bill. 根名称为saxena和bill。 I would like to create a function that can determine the root name of who the user searches for. 我想创建一个函数,该函数可以确定用户搜索的用户的根名称。

For the most simplest case, if they search for saxena, it returns saxena. 在最简单的情况下,如果他们搜索saxena,则返回saxena。 If they return bill, it returns bill. 如果他们退还账单,它将退还账单。

For a more complex case, saxena will be returned if the user searches for any of the names under her. 对于更复杂的情况,如果用户搜索其下的任何名称,将返回saxena。

For example, if I search for betten, akers, glovers, or koya, saxena will be returned. 例如,如果我搜索下注者,面包师,手套手或koya,则将返回saxena。

And if I search for bob, smith, or alright, bill will be returned. 而且,如果我搜索鲍勃,史密斯或好的,帐单将被退回。

This is my work so far. 到目前为止,这是我的工作。 I tried using recursion, but for some reason when I find the selected name, I return an undefined. 我尝试使用递归,但是由于某种原因,当我找到所选名称时,我返回了未定义的名称。

var findRootName = function(data, ltmName) {
    for (var key in data) {
        if (key == ltmName) {
            return key;
        } else {
            findNode(data[key], ltmName);
        }
    }
}

var findNode = function(data, ltmName) {
    for (var key in data) {
        if (key == ltmName) {
            return key;
        } else {
            findNode(data[key], ltmName);
        }
    }
}

http://jsfiddle.net/gthnfta7/7/ http://jsfiddle.net/gthnfta7/7/

Can somebody help me and figure out why my recursive function isn't working? 有人可以帮我弄清楚为什么我的递归函数不起作用吗?

An alternative technique, if you need to make many calls over the same data, is something like the following: 如果您需要对同一数据进行多次调用,则另一种方法是类似于以下内容:

function makeSearcher(data) {
    var paths = (function makePaths(data, parentPath, store) {
        var path = parentPath || [];
        results = store || {};
        Object.keys(data).forEach(function(key) {
            var newPaths = path.concat(key);
            results[key] = newPaths;
            makePaths(data[key], newPaths, results);
        });
        return results;
    })(data);
    return function(key) {
        var path = paths[key];
        return path && path[0];
    };
}

var search = makeSearcher(data);

search('clements'); //=> 'savena'

Note that the internal makePaths function is broader than the use here, as it could also be use to return a result like 请注意,内部makePaths函数的作用范围比此处使用的范围要广,因为它也可以用于返回类似

[ "saxena", "vacant", "fox", "glover", "clements" ]

The problem is that you're not returning anything in the event that the node is found. 问题在于,在找到节点的情况下,您不会返回任何内容。 You can simplify your function by writing it like this: 您可以通过这样编写来简化函数:

var findParent = function(data, childName) {
  for (var key in data) {
      if (key === childName || findParent(data[key], childName)) {
        return key;
      }
  }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM