//二叉树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)
return true
}
//插入节点的辅助函数
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)
}
}
//先序遍历
preOrderTraverse (callback) {
this.perOrderTraverseNode(this.root, callback)
}
perOrderTraverseNode (node, callback) {
if (node) {
callback(node.data)
this.perOrderTraverseNode(node.left, callback)
this.perOrderTraverseNode(node.right, callback)
}
}
//中序遍历
inOrderTraverse () {
this.inOrderTraverseNode(this.root)
}
inOrderTraverseNode (node) {
if (node) {
this.inOrderTraverseNode(node.left)
console.log(node.data)
this.inOrderTraverseNode(node.right)
}
}
//后续遍历
postOrderTraverse () {
this.postOrderTraverseNode(this.root)
}
postOrderTraverseNode (node) {
if (node) {
this.postOrderTraverseNode(node.left)
this.postOrderTraverseNode(node.right)
console.log(node.data)
}
}
//最小值
getMin () {
let current = this.root
while (current.left) {
current = current.left
}
return current.data
}
//最大值
getMax () {
let current = this.root
while (current.right) {
current = current.right
}
return current.data
}
//查找特定值
searchValue (data) {
let current = this.root
while (current) {
if (current.data < data) {
current = current.right
}else if (current.data > data) {
current = current.left
}else {
return current
}
}
return null
}
//删除节点
remove (data) {
let current = this.root
let parent = null
let isLeftChild = true
while (current.data != data) {
parent = current
if (current.data < data) {
current = current.right
isLeftChild = false
}else{
current = current.left
isLeftChild = true
}
if (current == null) return false
}
if (current.left == null && current.right == null) {
if (current == this.root) this.root = null
else if (isLeftChild) parent.left = null
else parent.right = null
}else if (current.left == null) { // 只有一个叶子节点,只有左子节点
if (current == this.root) this.root = current.right
else if (isLeftChild) parent.left = current.right
else parent.right = current.right
}else if (current.right == null) { // 只有右子节点
if (current == this.root) this.root = current.left
else if (isLeftChild) parent.left = current.left
else parent.right = current.left
}else { // 有两个子节点的节点
// 知道到后继节点
let successorNode = this.getSuccessor(current)
if (current == this.root) this.root = successorNode
else if (isLeftChild) parent.left = successorNode
else parent.right = successorNode
successorNode.left = current.left
}
return true
}
// 找到被删除节点的后继,被删除节点的右子树
getSuccessor (node) {
let successor = null
let current = node.right
let successorParent = null
while (current) {
successorParent = successor
successor = current
current = current.left
}
if (successor != node.right) {
successorParent.left = successor.right
successor.right = node.right
}
}
}