DFS输出所有匹配的元素及其祖先:javascript

时间:2020-02-08 02:33:07

标签: recursion tree depth-first-search ancestor

给予

//  tree data structure
Elephant --> Duck
         --> Hamster -->  Elephant1
                     -->  Elephant2

Hamster --> Elephant --> Fish
        --> Dog -------> Elephant

Dog --> Unicorn
    --> Fish --> Hamster
             --> Elephant --> Elephant

当用户键入术语“ Ele”时,要输出结果,以便将所有树都简化为以下格式(其结果与祖先匹配)

Elephant --> Hamster -->  Elephant1
                     -->  Elephant2  
Hamster --> Elephant 
        --> Dog -------> Elephant
Dog --> Fish  --> Elephant --> Elephant

给予

this.tree = [
    { id:1, name: 'Elephant',children:[ 
          { id:2, name: 'Duck' },
          { id:3, name: 'Hamster', children: [
                   { id: 4, name: 'Elephant1', id: 5, name: 'Elephant2' }]
          }
    ]},
    { id:5A, name: 'Hamster', children: [
          { id:6, name: 'Elephant', children: [
                  { id:7, name: 'Fish' }
          ]},
          { id:8, name: 'Dog', children: [
                  { id:9, name: 'Elephant' }
          ]}
    ]},
    { id:10, name: 'Dog', children: [
          { id:11, name: 'Unicorn' },
          { id:12, name: 'Fish', children: [
                  { id:13, name: 'Hamster' },
                  { id:14, name: 'Elephant', children: 
                         [{ id:15, name: 'Elephant' }
                  ]},
          ]}
    ]},
    { id:16, name: 'Elephant', children: [
          { id:17, name: 'Duck' }, 
          { id:18, name: 'Hamster', children: [
                 { id:19, name: 'Elephant' }, 
                 { id:20, name: 'Fish' }
          ]}
    ]}
]

我的尝试:

我尝试使用“深度优先搜索”遍历进行以下操作,但是当其子项具有匹配项时,问题包括其祖先。当前,该代码仅显示子项的匹配结果,而不显示其祖先。我已经完成了一半的解决方案(可能需要对我的尝试解决方案进行一些修改),但是回溯时遇到了麻烦。一直在DFS上搜索资源,如果有人知道这一点,将不胜感激。

onSearch(term) {
    let found = this.search(term, JSON.parse(JSON.stringify(this.tree)));
    console.log(found);
}

search(term, parent, path, basket) {
    let fork = path.slice(0);

    let found, { name, id, children } = parent;
    let nameId = name + ':' +id;

    fork.push(nameId);
    if (name.toLowerCase().indexOf(term) !== -1) {
        basket.push(fork);
        fork = [];
        return basket
    } 
    if (!!children && children.length > 0) {
        for (let child of parent.children) {
            this.search(term, child, fork, basket)  
        }
        return basket;
    } 

    if (children.length === 0) {
        return [];
    }
  }

this.onSearch('elep');

但是上述尝试解决方案返回的结果并不完全正确。

Elephant  --> Hamster -->  Elephant1 

Hamster --> Elephant  
        --> Dog -------> Elephant

Dog  --> Fish  --> Elephant  

理想的解决方案是输出

Elephant --> Hamster -->  Elephant1
                     -->  Elephant2  
Hamster --> Elephant 
        --> Dog -------> Elephant
Dog --> Fish  --> Elephant --> Elephant

1 个答案:

答案 0 :(得分:0)

经过几次尝试,找到了以下解决方案,它将输出所需解决方案

onSearch(term) {
   let found = this.search(term, JSON.parse(JSON.stringify(this.tree)));
   console.log(found);
}
search(term, parent, path, basket) {
    let found, { name, id, children } = parent;
    let nameId = name + ':' +id;

    let fork = path.slice(0);
    fork.push(nameId);

    if (name.toLowerCase().indexOf(term) !== -1) {
        basket.push([fork]);
    } 
    if (!!children && children.length > 0) {
        for (let child of parent.children) {
            this.search(term, child, fork, basket)
        }
        return basket;
    } 
}