[英]JavaScript Recursive Tree Searching Function Not Detecting Nested Children
嘗試創建一個遞歸函數,以正確地在Tree類及其所有后代中搜索一個值,如果找到該值,則返回true,否則返回false。
特別重要的是遞歸的contains()函數。 試圖讓代碼通過lint。 我只收到一個關於不檢測嵌套子項的錯誤。 其他一切都過去了。
我的代碼:
/* 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);
});
});
這是棉絨錯誤:
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)
您的問題在以下這段代碼中:
if (node.children.length > 0) {
for (let i = 0; i < node.children.length; i++) {
return checkNode(node.children[i]);
}
}
無論checkNode對於第一個孩子是true還是false,這行代碼都將從函數返回。 如果結果為假,則需要繼續檢查。
嘗試以下方法:
if (node.children.length > 0) {
for (let i = 0; i < node.children.length; i++) {
if (checkNode(node.children[i])) {
return true;
}
}
}
您無條件返回for循環,因此您僅檢查第一個孩子。
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;
}
我認為,這就是您的代碼應如下所示:
for (let childIndex = 0; childIndex < node.children.length; childIndex++) {
const foundInChildren = checkNode(node.children[childIndex]);
if (foundInChildren)
return true;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.