From ee8a1f15e07de5c6c786a705d345832a7fea10cf Mon Sep 17 00:00:00 2001 From: Kenyetta Griffin Date: Mon, 24 Oct 2022 11:17:33 -0400 Subject: [PATCH] finished --- index.js | 41 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 9e1b487..1d0f1e2 100644 --- a/index.js +++ b/index.js @@ -10,7 +10,46 @@ class Tree { this.root = node; } - findNode(data) {} + findNode(data) { + let currentNode = this.root; + let queue = [this.root]; + + while (queue.length) { + currentNode = queue.shift(); + if (currentNode.data === data) { + return currentNode; + } + + if (currentNode.children) { + for (let child of currentNode.children) { + queue.push(child); + } + } + } + return null + } + + + } +const a = new TreeNode("A"); + const b = new TreeNode("B"); + const c = new TreeNode("C"); + const d = new TreeNode("D"); + const e = new TreeNode("E"); + const f = new TreeNode("F"); + const g = new TreeNode("G"); + const h = new TreeNode("H"); + a.children.push(b, c, d); + b.children.push(e, f, g); + d.children.push(h); + treeA = new Tree(a); + + + +console.log(treeA.findNode("H")) +console.log(TreeNode) +// console.log(treeA) + module.exports = { TreeNode, Tree };