Algorithm: Give a number, and try to find it or its nearest value from a Binary Search Tree

Question

PS: This is an interview question in real. After the interview, I optimized my answer of the interview and share with you guys here.

Give you a number, and a Binary Search Tree, please find the number or get its nearest node value out of the tree. Can check the UT as examples.

UT

No need to Copy and Paste here. There is a full version below for your Copy and Paste. Keep scroll down.

  QUnit.module('Search BSTree', function() {
    const run = (tree, search) => new Solution(tree, search).solve()

    QUnit.test('binary search tree', function(assert) {
      const root = createBTree([10,5,15,1,6,13,17])
      //          10
      //        /    \
      //       5      15
      //     /   \    /  \
      //    1     6  13   17
      assert.equal(run(root, 9), 10)
      assert.equal(run(root, 4), 5)
      assert.equal(run(root, 6), 6)
      assert.equal(run(root, 7), 6)
      assert.equal(run(root, 20), 17)
      assert.equal(run(root, 14), 15)
      assert.equal(run(root, 17), 17)
      assert.equal(run(root, 12), 13)
    });
  });

Answer

Thinking: We have to walk through the BSTree, and find the value. If fount, then return the value directly, of course, it’s the nearest already. Otherwise, check the child’s tree nearest value vs current node’s value who is nearer to the supplied number.

Main logic

Just for read. Scroll down to see full code.

class Solution {
    constructor(tree, num) {
      this.tree = tree
      this.num = num
    }
    bs(root, num) {
      let nearest = null

      if (num === root.val) return root.val

      if (num < root.val) {
        if (root.left) {
          nearest = this.bs(root.left, num)
        }
      } else {
        if (root.right) {
          nearest = this.bs(root.right, num)
        }
      }

      if (Math.max(nearest - num, num - nearest) < Math.max(root.val - num, num - root.val)) {
        return nearest
      } else {
        return root.val
      }
    }
    solve() {
      return this.bs(this.tree, this.num)
    }
  }

Full code with UT

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Binary search tree</title>
  <link href="https://code.jquery.com/qunit/qunit-git.css" rel="stylesheet" type="text/css" />
  <script src="https://code.jquery.com/qunit/qunit-git.js"></script>
</head>
<body>

<div id="qunit"></div>
<div id="qunit-fixture"></div>

<script>
  class Solution {
    constructor(tree, num) {
      this.tree = tree
      this.num = num
    }
    bs(root, num) {
      let nearest = null

      if (num === root.val) return root.val

      if (num < root.val) {
        if (root.left) {
          nearest = this.bs(root.left, num)
        }
      } else {
        if (root.right) {
          nearest = this.bs(root.right, num)
        }
      }

      if (Math.max(nearest - num, num - nearest) < Math.max(root.val - num, num - root.val)) {
        return nearest
      } else {
        return root.val
      }
    }
    solve() {
      return this.bs(this.tree, this.num)
    }
  }

  QUnit.module('Search BSTree', function() {
    const run = (tree, search) => new Solution(tree, search).solve()

    QUnit.test('binary search tree', function(assert) {
      const root = createBTree([10,5,15,1,6,13,17])
      //          10
      //        /    \
      //       5      15
      //     /   \    /  \
      //    1     6  13   17
      assert.equal(run(root, 9), 10)
      assert.equal(run(root, 4), 5)
      assert.equal(run(root, 6), 6)
      assert.equal(run(root, 7), 6)
      assert.equal(run(root, 20), 17)
      assert.equal(run(root, 14), 15)
      assert.equal(run(root, 17), 17)
      assert.equal(run(root, 12), 13)
    });
  });

  // Tools
  class Node {
    constructor(val, left = null, right = null) {
      this.val = val
      this.left = left
      this.right = right
    }
  }

  function createBTree(arr) {
    const nodes = arr.map(val => val === null ? null : new Node(val))
    for (let i = 0; i < nodes.length; i++) {
      if (nodes[i] === null) continue
      nodes[i].left = nodes[i*2+1] || null
      nodes[i].right = nodes[i*2+2] || null
    }
    return nodes[0]
  }
</script>

</body>
</html>

PS: Copy and paste this code in your own HTML file, don’t forget to serve it as a server. You might know that I mean. (CORS won’t allow you to load QUnit.

Result

在这里插入图片描述

Finally

You are welcome to tell me if any bugs or thoughts or discussions.
Please free to leave message to me.
_

Fighting!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值