二叉查找树

161 阅读3分钟

数是一种经常用到的一种数据结构。比如文件系统的文件,有序列表。在二叉树上进行查询非常快(链表上查询比较慢),在二叉树上进行添加和删除元素也非常快(在数组上进行添加和删除比较慢)。

定义二叉查找树(Binary Search Tree)

我们先定义一个Node节点和BST类

function Node(data, left, right) {
  this.data = data
  this.left = left
  this.right = right
}
Node.prototype.show = function() {
  return this.data
}

function BST() {
  this.root = null
}

BST首先需要一个inset方法,向树种加入新的节点。算法如下:

  1. 检查BST是否有根节点,如果没有,该节点就是根节点,否则下一步
  2. 设置当前节点指向根节点
  3. 如果插入的节点小于当前节点,则当前节点移动到原节点的左节点,反之,执行第5步。
  4. 如果当前节点的左节点为null,将新的节点插入到这个位置,退出循环;反之继续执行下一次循环。
  5. 设置当前节点为原节点的右节点。
  6. 如果当前节点的右节点为null,将新的节点插入到这个位置,退出循环;反之继续执行下一次循环。
BST.prototype.insert = function(data) {
 var n = new Node(data, null, null)
 if(this.root === null) {
  this.root = n
 } else {
   var current = this.root
   var parent
   while(true) {
     parent = current
     if(data < current.data) {
      current = current.left
      if(current === null) {
        parent.left = n
        break
      }
     } else {
      current = current.right
      if(current === null) {
        parent.right = n
        break
      }
     }
   }
 }
}

遍历二叉查找树

中序

BST.prototype.inOrder = function(node) {
 if(node !== null) {
  this.inOrder(node.left)
  console.log(node.show())
  this.inOrder(node.right)
 } 
}

前序

BST.prototype.preOrder = function(node) {
 if(node !== null) {
  console.log(node.show())
  this.preOrder(node.left)
  this.preOrder(node.right)
 } 
}

后序

BST.prototype.postOrder = function(node) {
 if(node !== null) {
  console.log(node.show())
  this.postOrder(node.left)
  this.postOrder(node.right)
 } 
}

测试

var nums = new BST()
nums.insert(23)
nums.insert(45)
nums.insert(16)
nums.insert(37)
nums.insert(3)
nums.insert(99)
nums.insert(22)
nums.inOrder(nums.root)
nums.preOrder(nums.root)
nums.postOrder(nums.root)

二叉查找树上进行查找

  1. 查找定值
  2. 查找最小值
  3. 查找最大值

最小值: 因为小的值都在左叶子节点上,所以只需找到左子树的最后一个节点

BST.prototype.getMin = function() {
 var current = this.root
 while(current.left !== null) {
  current = current.left
 }
 return current.data
}

相反,最大值在右子树的最后一个节点

BST.prototype.getMax = function() {
 var current = this.root
 while(current.right !== null) {
  current = current.right
 }
 return current.data
}

查找定值

BST.prototype.find = function(data) {
 var current = this.root
 while(current !== null) {
  if(current.data === data) {
    return current
  } else if(data < current.data) {
    current = current.left
  } else {
    current = current.right
  }
 }
}

二叉查找树上删除节点

删除的节点分为三种情况,无子节点,有一个子节点,二个子节点

  1. 当删除节点没有子节点时候,只需要父节点指向null即可
  2. 如果删除节点只有一个子节点时,只需父节点指向该子节点即可。
  3. 如果待删节点有两个子节点,有两种做法
    1. 找到待删节点左子树上的最大值
    1. 找到待删节点右子树上的最小值 我们选择右子树上的最小值复制到待删除节点,同时递归删除右子树上的最小值
BST.prototype.remove = function(data) {
  this.root = removeNode(this.root, data)
}

function getSmallest(node) {
  if(node === null) {
    return null
  }
  var current = node
  while(node.left !== null) {
     current = current.left
  }
  return current
}

function removeNode(node, data) {
  if (node == null) {
    return null
  }
  if (data == node.data) {
    // 没有子节点的节点
    if (node.left == null && node.right == null) {
      return null
    }
    // 没有左子节点的节点
    if (node.left == null) {
      return node.right
    }
    // 没有右子节点的节点
    if (node.right == null) {
      return node.left
    }
    // 有两个子节点的节点
    var tempNode = getSmallest(node.right)
    node.data = tempNode.data
    node.right = removeNode(node.right, tempNode.data)
    return node
  } else if (data < node.data) {
    node.left = removeNode(node.left, data)
    return node
  } else {
    node.right = removeNode(node.right, data)
    return node
  }
}