1. 程式人生 > >js構建二叉樹,實現深度遍歷

js構建二叉樹,實現深度遍歷

最近研究了一下二叉樹,閒來沒事用js自己動手實現了一遍,歡迎各位大牛拍磚 

一個二叉樹資料結構的屬性一般包含:節點及節點的值(程式碼中的node);節點之間的邊關係(連線關係,程式碼中的line)

// 二叉樹物件實現
    function binaryTree (opt) {
      let tree = {
        node: opt.node,
        line: opt.line,
        isVisited: [],
        findFather: function (x) {
          for (let i = 0; i < this.node.length; i++) {
            if (this.line[i][x] === 1) {
              console.log('找到父物件為' + i)
              return i
            }
          }
        },
        findFirstChild: function (x) {
          for (let i = 0; i < this.node.length; i++) {
            if (this.line[x][i]) {
              // console.log('找到第一個子物件' + i)
              return i
            }
          }
          return -1
        },
        findSecondChild: function (x) {
          let y = -1
          for (let i = 0; i < this.node.length; i++) {
            if (this.line[x][i]) {
              if (y === -1) {
                y = i
              } else {
                // console.log('找到第二個子物件' + i)
                return i
              }
            }
          }
          return -1
        },
        findLeaf: function () {
          let result = []
          for (let i = 0; i < this.node.length; i++) {
            if (this.findFirstChild(i) === -1) {
              result.push(i)
            }
          }
          return result
        },
        // 深度迴圈
        deepLoop: function (isVisited, root, result) {
          isVisited[root] = true
          console.log(root + '深度遍歷')
          result.push(root)
          root = this.findFirstChild(root)
          while (root !== -1) {
            if (!isVisited[root]) {
              this.deepLoop(isVisited, root, result)
            }
            root = this.findSecondChild(root)
          }
        },
        // 深度優先遍歷
        deepTraversing: function (isVisited) {
          let result = []
          for (let i = 0; i < this.node.length; i++) {
            if (!isVisited[i]) {
              this.deepLoop(isVisited, i, result)
            }
          }
          console.log(result)
        }
      }
      return tree
    }

題目:從二叉樹的根到葉子節點稱為一條路徑,路徑上的每個節點的value之和為路徑和值,是否存在一條和值為N的。

// 找到一個等於N的鏈路
    function find (N) {
      let opt = { node: [1, 4, 2, 2, 5, 2, 7],
        line: [
          [0, 1, 1, 0, 0, 0, 0],
          [0, 0, 0, 1, 1, 0, 0],
          [0, 0, 0, 0, 0, 1, 1],
          [0, 0, 0, 0, 0, 0, 0],
          [0, 0, 0, 0, 0, 0, 0],
          [0, 0, 0, 0, 0, 0, 0],
          [0, 0, 0, 0, 0, 0, 0]] }
      let tree = this.binaryTree(opt)
      let isVisited = [false, false, false, false, false, false, false]
      tree.deepTraversing(isVisited)
      let leaf = tree.findLeaf()
      let result = []
      for (let i = 0; i < leaf.length; i++) {
        let x = leaf[i]
        let value = tree.node[x]
        let res = []
        res.push(x)
        while (x) {
          x = tree.findFather(x)
          value += tree.node[x]
          res.push(x)
        }
        if (value === N) {
          result = res
        }
      }
      console.log(result)
      return result
    }