数据结构_二叉树

//二叉树BST
  
  class Node {
    constructor (data) {
      this.data = data
      this.left = null
      this.right = null
    }
  }

  class BST {
    constructor () {
      this.root = null
    }
    insert (data) {
      let newNode = new Node(data)
      if (!this.root) this.root = newNode
      else {
        this.insertNode(this.root, newNode)
      }
    }
    //插入节点的辅助函数
    insertNode (root, newNode) {
      if (newNode.data < root.data) {
        if (root.left == null) root.left = newNode
        else this.insertNode(root.left, newNode) 
      }else {
        if (root.right == null) root.right = newNode
        else this.insertNode(root.right, newNode)
      }
    }
  }
  let tree = new BST()

 

上一篇:Hackerank- 30 days of code-linked list


下一篇:数据结构值平衡二叉树