简体   繁体   中英

Find Closest Value in Binary Search Tree?

I have this algorithm structure problem; write a function that takes in a BST and a target interget value and returns the closet value to that target value contained in the BST.

The algorithm site gave me this to work with;

function findClosestValueInBst(tree, target) {
  // Write your code here.
}

// This is the class of the input tree. Do not edit.
class BST {
  constructor(value) {
    this.value = value;
    this.left = null;
    this.right = null;
  }
}

I am not asking for the answer but how can I test this out in my local visual studio code? What values do i need to pass in the tree/target argument parameters of the function for me to even test things or console.log things out?

You need to build valid search tree.

At first implement operation Add(root, value)

Then make operations Find that looks for exact coincidence, and Successor and Predecessor to get closest values larger and smaller than current one.

Then provide sequence of values to make a tree (example: 6,3,9,0,15,5,7,14 ) and test above functions.

To implement findClosestValueInBst , you can modify the code of Find but when you discover element absence (standing at the last tree leaf), call one of Successor or Predecessor function to check a neighbor and find what value is close to target.

You probably want to build your own BST (or a few) to test your code out. You could build them by hand by just creating a bunch of nodes one by and and linking them together, or write an insert function to pass values in to and build your trees like that. You could use an array of values and map over it to build nodes if you'd like. Just keep track of the root node for your BST(s).

Then call your function with a root of a BST that you've created and several different target values. See if your function returns the value you'd expect. Try to cover all common and edge cases: the target is exactly in the tree; the target is in the root node, an internal node, a leaf node; the target is not in the tree but is between two values (see that you return the closer one); not in the tree and larger or greater than everything in the tree (see that you return the max or min value actually in the tree); see that it returns something sensible for an empty BST.

Do this for a BST or two and you should be pretty confident that your function works.

Hints:

Think about how things could go wrong. Typical mistakes are

  • gross programming errors (binary search plain wrong, typos);

  • subtle limit cases (empty tree, tree of a single node, two nodes; equal nodes; target equal to a node, target in the middle; binary search wrong on few nodes...).

For each of those situations, how can you check?


Rather than only trying a few test cases, I would advise to instrument the code with assertions that you know must hold at strategic places. For instance, check that the solution is still found among the subtrees that you consider.

Also make sure to implement a bulletproof function that returns the correct answer in all cases (efficiency does not matter).


As the exercise does not seem to be about how to build a BST, it is acceptable to copy existing code for this part of the task. Make sure that the source is reliable. (For double safety, you can validate the trees that are built.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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