0
点赞
收藏
分享

微信扫一扫

How to create a Binary Tree by Array of data?

夕阳孤草 2022-01-13 阅读 66

Question

You might want to create a binary tree easily when you resolve algorithm questions, like me. Here is the source codes.

Thinking

I searched some results from Baidu, but their codes in the results are kind of complex… So I tried myself. After drew on page, I figure out that one Binary Tree node has functional relationship with its index. Function is simple, you can check the Main logic below.

Main logic

basic logic codes in Javascript.

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

to support null if the tree has some empty values

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]
}

Full codes

main logic is included, and have UT as well.

<!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 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]
  }

  QUnit.module('Create BTree', function() {
    QUnit.test('full tree check', function(assert) {
      const root = createBTree([])
      assert.equal(root, undefined)
    });
    QUnit.test('one item', function(assert) {
      const root = createBTree([1])
      assert.equal(root.val, 1)
      assert.equal(root.left, null)
      assert.equal(root.right, null)
    });
    QUnit.test('five items', function(assert) {
      const root = createBTree([1,2,3,4,5])
      assert.equal(root.val, 1)
      assert.equal(root.left.val, 2)
      assert.equal(root.right.val, 3)
      assert.equal(root.left.left.val, 4)
      assert.equal(root.left.right.val, 5)
      assert.equal(root.right.left, null)
      assert.equal(root.right.right, null)
    });
    QUnit.test('five items has null item', function(assert) {
      const root = createBTree([1,2,null,4,5])
      assert.equal(root.val, 1)
      assert.equal(root.left.val, 2)
      assert.equal(root.right, null)
      assert.equal(root.left.left.val, 4)
      assert.equal(root.left.right.val, 5)
    });
    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(root.val, 10)
      assert.equal(root.left.val, 5)
      assert.equal(root.right.val, 15)
      assert.equal(root.left.left.val, 1)
      assert.equal(root.left.right.val, 6)
      assert.equal(root.left.right.left, null)
      assert.equal(root.left.right.right, null)
    });
  });
</script>

</body>
</html>

Finally

You are welcome to leave message with me.

举报

相关推荐

0 条评论