简体   繁体   English

试图在 JavaScript 的 BST 中找到最接近的值

[英]Trying to find closest value in BST in JavaScript

Trying to follow along with this write up on Find the Closest Value in a Binary Search Tree .尝试跟随这篇关于Find the Closest Value in a Binary Search Tree 的文章。 However, I am getting undefined returned instead of 13. Below is the code that also includes the tree data.但是,我返回的是未定义而不是 13。下面是还包含树数据的代码。 The write up has an alternate more optimized solution but I'm just trying to first understand the less optimized version first.这篇文章有一个更优化的替代解决方案,但我只是想先了解优化程度较低的版本。 I'm missing a step but can't find it.我错过了一个步骤,但找不到它。 What am I missing.我错过了什么。 Thank you.谢谢你。

[ [

 const treeData = { "target": 12, "tree": { "nodes": [ {"id": "10", "left": "5", "right": "15", "value": 10}, {"id": "15", "left": "13", "right": "22", "value": 15}, {"id": "22", "left": null, "right": null, "value": 22}, {"id": "13", "left": null, "right": "14", "value": 13}, {"id": "14", "left": null, "right": null, "value": 14}, {"id": "5", "left": "2", "right": "5-2", "value": 5}, {"id": "5-2", "left": null, "right": null, "value": 5}, {"id": "2", "left": "1", "right": null, "value": 2}, {"id": "1", "left": null, "right": null, "value": 1} ], "root": "10" } }; class BST { constructor(value) { this.value = value; this.left = null; this.right = null; } }; const findClosestValueInBst = (tree, target) => { // define current node as input tree let currentNode = tree; //console.log(tree); // create array to store BST values const values = []; // traverse the tree and place all values in the above array const traverse = node => { if (node) { traverse(node.left); values.push(node.value); traverse(node.right); } } traverse(tree); // create closestProximity variable let closestProximity = Number.POSITIVE_INFINITY; //console.log(closestProximity); // create undefined closestValue variable let closestValue; // iterate over the values array, checking each value's proximity to the target value for (let i = 0; i < values.length; i++) { let proximity = Math.abs(values[i] - target); // if closer in proximity than closestProximity, replace closestProximity if (proximity < closestProximity) { // update closestValue closestValue = values[i]; } } return closestValue; }; const result = findClosestValueInBst(treeData.tree, treeData.target); console.log(result);

][2] ][2]

After the corrections made in your question, there are still these issues:对您的问题进行更正后,仍然存在以下问题:

  • The code never updates closestProximity so the if condition on this variable will always be true.代码从不更新closestProximity的距离,因此该变量的if条件将始终为真。
  • The main call passes treeData.tree to the function, but that function then treats that reference as a node object, never looking into the nodes property.主调用将treeData.tree传递给 function,但 function 然后将该引用视为节点 object,从不查看nodes属性。
  • The input data is not transformed into instances of BST , which means that you cannot navigate efficiently through the tree.输入数据不会转换为BST的实例,这意味着您无法有效地在树中导航。 Even if you would have a node object, then node.left is just a string, not an object, and you'd have to scan the tree.nodes array to locate the corresponding object.即使你有一个node object,那么node.left只是一个字符串,而不是 object,你必须扫描tree.nodes数组以找到相应的 object。
  • By first collecting the nodes into an array through traverse you lose any benefit from the binary search tree.通过首先通过traverse将节点收集到一个数组中,您将失去二叉搜索树的任何好处。 The purpose of this data structure is to avoid having to visit all values.这种数据结构的目的是避免必须访问所有值。

Here is a correction of your code that addresses these issues:这是解决这些问题的代码的更正:

 class BST { constructor(value) { this.value = value; this.left = null; this.right = null; } } function loadTree(tree) { const nodeMap = new Map(tree.nodes.map(node => [node.id, new BST(node.value)])); for (const node of tree.nodes) { nodeMap.get(node.id).left = nodeMap.get(node.left)?? null; nodeMap.get(node.id).right = nodeMap.get(node.right)?? null; } return nodeMap.get(tree.root); } function findClosestValueInBst(root, target) { let currentNode = root; let high = Infinity; let low = -Infinity; while (currentNode) { if (target < currentNode.value) { high = currentNode.value; currentNode = currentNode.left; } else { low = currentNode.value; currentNode = currentNode.right; } } return high - target < target - low? high: low; } const treeData = {"target": 12,"tree": {"nodes": [{"id": "10", "left": "5", "right": "15", "value": 10},{"id": "15", "left": "13", "right": "22", "value": 15},{"id": "22", "left": null, "right": null, "value": 22},{"id": "13", "left": null, "right": "14", "value": 13},{"id": "14", "left": null, "right": null, "value": 14},{"id": "5", "left": "2", "right": "5-2", "value": 5},{"id": "5-2", "left": null, "right": null, "value": 5},{"id": "2", "left": "1", "right": null, "value": 2},{"id": "1", "left": null, "right": null, "value": 1}],"root": "10"}}; const result = findClosestValueInBst(loadTree(treeData.tree), treeData.target); console.log(result);

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

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