JavaScript递归树搜索功能未检测到嵌套子项

时间:2017-10-13 18:09:08

标签: javascript recursion tree lint

尝试创建一个递归函数,正确搜索Tree类及其所有后代的值,如果找到该值则返回true,否则返回false。

特别重要的是递归contains()函数。试图让代码通过linter。我只收到一个关于不检测嵌套子项的错误。其他一切都在流逝。

我的代码:

/* eslint-disable no-trailing-spaces */
/* eslint-disable no-unused-vars */
class Tree {
  constructor(value) {
    this.value = value;
    this.children = [];
  }
  // Adds a new Tree node with the input value to the current Tree node 
  addChild(value) {
    this.children.push(new Tree(value));
  }
  // Checks this node's children to see if any of them matches the given value
  // Continues recursively until the value has been found or all of the children
  // have been checked
  contains(value) {
    const thisNode = this;
    function checkNode(node) {
      if (node.value === value) {
        return true;
      }
      if (node.children.length > 0) {
        for (let i = 0; i < node.children.length; i++) {
          return checkNode(node.children[i]);
        }
      }
      return false;
    }
    return checkNode(thisNode);
  }
}

module.exports = Tree;

以下是测试它的文件:

/* eslint-disable no-undef */
const Tree = require('../src/tree');

describe('Tree', () => {
  let tree;

  beforeEach(() => {
    tree = new Tree(true);
  });

  it('should have methods named "addChild" and "contains"', () => {
    expect(typeof tree.addChild).toBe('function');
    expect(typeof tree.contains).toBe('function');
  });

  it('should add children to the tree', () => {
    tree.addChild(5);
    expect(tree.children[0].value).toBe(5);
  });

  it('should return true for a value that the tree contains', () => {
    tree.addChild(5);
    expect(tree.contains(5)).toBe(true);
  });

  it('should return false for a value that was not added', () => {
    tree.addChild(5);
    expect(tree.contains(6)).toBe(false);
  });

  it('should be able to add children to a tree\'s child', () => {
    tree.addChild(5);
    tree.children[0].addChild(6);
    expect(tree.children[0].children[0].value).toBe(6);
  });

  it('should correctly detect nested children', () => {
    tree.addChild(5);
    tree.addChild(6);
    tree.children[0].addChild(7);
    tree.children[1].addChild(8);
    expect(tree.contains(7)).toBe(true);
    expect(tree.contains(8)).toBe(true);
  });
});

这是linting错误:

Tree
    ✓ should have methods named "addChild" and "contains" (5ms)
    ✓ should add children to the tree (1ms)
    ✓ should return true for a value that the tree contains (3ms)
    ✓ should return false for a value that was not added (1ms)
    ✓ should be able to add children to a tree's child (1ms)
    ✕ should correctly detect nested children (9ms)

3 个答案:

答案 0 :(得分:3)

你的问题出现在这段代码中:

  if (node.children.length > 0) {
    for (let i = 0; i < node.children.length; i++) {
      return checkNode(node.children[i]);
    }
  }

这行代码将从函数返回,无论checkNode的结果是第一个子节点,true或false。如果结果为false,则需要继续检查。

请改为尝试:

  if (node.children.length > 0) {
    for (let i = 0; i < node.children.length; i++) {
      if (checkNode(node.children[i])) {
        return true;
      }
    }
  }

答案 1 :(得分:2)

你无条件地在for-loop中返回,所以你只检查第一个孩子。

    for (let i = 0; i < node.children.length; i++) {
      return checkNode(node.children[i]);
    }

应该是

    for (let i = 0; i < node.children.length; i++) {
      if (checkNode(node.children[i])) return true;
    }

答案 2 :(得分:2)

我认为,这就是您的代码应该是这样的:

for (let childIndex = 0; childIndex < node.children.length; childIndex++) {
   const foundInChildren = checkNode(node.children[childIndex]);
   if (foundInChildren)
     return true;
}