typescript实现二叉搜索树(BST)

class TreeNode<T> {
  public val: T
  public left: TreeNode<T> | null
  public right: TreeNode<T> | null

  constructor(val: T, left: TreeNode<T> | null = null, right: TreeNode<T> | null = null) {
    this.val = val
    this.left = left
    this.right = right
  }
}


class BinarySearchTree<T> {
  private root: TreeNode<T>
  private readonly judge: { (a: T, b: T): boolean }
  private readonly equal: { (a: T, b: T): boolean }

  // judge用于判断大小,a > b 返回true
  constructor(judge: { (a: T, b: T): boolean } = (a, b) => a > b, equal: { (a: T, b: T): boolean } = (a, b) => a === b) {
    this.judge = judge
    this.equal = equal
    this.root = null
  }

  // 通过数组构建bst
  public build(arr: T[]): TreeNode<T> {
    // 遍历数组
    for (let i = 0; i < arr.length; i++) {
      this.add(arr[i])
    }
    return this.root
  }

  // 插入值
  public add(val: T): void {
    this.addNode(new TreeNode<T>(val))
  }

  // 插入节点
  public addNode(valNode: TreeNode<T>): void {
    if (!valNode) return
    if (!this.root) {
      this.root = valNode
      return
    }
    let node = this.root
    while (node) {
      if (this.judge(valNode.val, node.val)) { // 如果arr[i]更大,向右构建
        // node没有右节点,说明找到位置,新建右节点后返回
        if (!node.right) {
          node.right = valNode
          break
        } else node = node.right
      } else { // node更大,向左构建
        // node没有左节点,说明找到位置,新建左节点后返回
        if (!node.left) {
          node.left = valNode
          break
        } else node = node.left
      }
    }
  }

  // 删除值,true为删除成功
  public delete(val: T): boolean {
    if (!this.root) return false
    let node = this.root
    let lastNode = null
    while (node) {
      if (this.equal(node.val, val)) {
        if (lastNode) {
          // 将节点从树上移除
          if (lastNode.right === node)
            lastNode.right = null
          else lastNode.left = null
        }else this.root = null

        // 将被删除节点的左右节点重新插入树中
        this.addNode(node.left)
        this.addNode(node.right)
        return true
      }

      lastNode = node
      if (this.judge(val, node.val)) { // 如果val更大,向右查找
        node = node.right
      } else { // node更大,向左查找
        node = node.left
      }
    }
    return false
  }

  // 搜索值
  public search(val: T): boolean {
    if (!this.root) return false
    let node = this.root
    while (node) {
      // 值相等,找到
      if (this.equal(node.val, val)) return true

      if (this.judge(val, node.val)) { // 如果val更大,向右查找
        node = node.right
      } else { // node更大,向左查找
        node = node.left
      }
    }
    return false
  }
}
/* 测试样例
~function text() {
  let bst: BinarySearchTree<number> = new BinarySearchTree<number>()
  bst.build([3, 5, 6, 2, 6])
  console.log('测试搜索-----------')
  console.log(bst.search(2))// true
  console.log(bst.search(9))// false

  console.log('测试插入-----------')
  bst.add(9)// 插入9
  console.log(bst.search(9))// true

  console.log('测试删除失败-----------')
  console.log(bst.delete(100))// false

  console.log('测试删除成功-----------')
  console.log(bst.delete(3))// 删除3, true
  console.log(bst.search(3))// false
  console.log(bst.search(5))// true
}()
 */
posted @ 2022-07-26 14:26  ぃ往事深处少年蓝べ  阅读(31)  评论(0编辑  收藏  举报