JavaScript樹結構——二叉搜索樹
function BinarySearchTree(){
function Node(key, value=key){
this.key = key
this.value = value
this.left = null
this.right = null
}
this.root = null
BinarySearchTree.prototype.insert = function(key, value=key){
let newNode = new Node(key, value=key)
if (this.root == null){
this.root = newNode
}
else{
this.insertNode(this.root , newNode)
}
}
BinarySearchTree.prototype.insertNode = function(node, newNode){
if (newNode.key < node.key){
if (node.left == null){
node.left = newNode
}
else{
this.insertNode(node.left, newNode)
}
}
else if (newNode.key > node.key){
if (node.right == null){
node.right = newNode
}
else {
this.insertNode(node.right, newNode)
}
}
else{
node.value = newNode.value
}
}
BinarySearchTree.prototype.preOrderTraversal = function(){
let result = ''
this.preOrderTraversalNode(this.root, function(key, value){
result += `${key} => ${value}\n`
})
return result
}
BinarySearchTree.prototype.preOrderTraversalNode = function(node, callback){
if (node != null){
callback(node.key,node.value)
this.preOrderTraversalNode(node.left, callback)
this.preOrderTraversalNode(node.right, callback)
}
}
BinarySearchTree.prototype.midOrderTraversal = function(){
let result = ''
this.midOrderTraversalNode(this.root, function(key, value){
result += `${key} => ${value}\n`
})
return result
}
BinarySearchTree.prototype.midOrderTraversalNode = function(node, callback){
if (node != null){
this.midOrderTraversalNode(node.left, callback)
callback(node.key, node.value)
this.midOrderTraversalNode(node.right, callback)
}
}
BinarySearchTree.prototype.postOrderTraversal = function(){
let result = ''
this.postOrderTraversalNode(this.root, function(key, value){
result += `${key} => ${value}\n`
})
return result
}
BinarySearchTree.prototype.postOrderTraversalNode = function(node, callback){
if (node != null){
this.postOrderTraversalNode(node.left)
this.postOrderTraversalNode(node.right)
callback(node.key, node.value)
}
}
BinarySearchTree.prototype.max = function(){
let node = this.root
let key = null
while (node != null){
key = node.key
node = node.right
}
return key
}
BinarySearchTree.prototype.min = function(){
let node = this.root
let key = null
while (node != null){
key = node.key
node = node.left
}
return key
}
BinarySearchTree.prototype.search = function(key){
let node = this.root
while (node != null){
if (key < node.key){
node = node.left
}
else if (key > node.key){
node = node.right
}
else {
return node.value
}
}
return null
}
BinarySearchTree.prototype.remove = function(key){
let current = this.root
let parent = null
let isLeftChild = true
while (current.key != key){
parent = current
if (key < current.key){
isLeftChild = true
current = current.left
}
else{
isLeftChild = false
current = current.right
}
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 successor = this.getSuccessor(current)
if (current == this.root){
this.root = successor
successor.left = current.left
}
else if (isLeftChild){
parent.left = successor
successor.left = current.left
}
else {
parent.right = successor
successor.left = current.left
}
}
}
BinarySearchTree.prototype.getSuccessor = function(delNode){
let successor = delNode
let successorParent = delNode
let current = delNode.right
while (current != null){
successorParent = successor
successor = current
current = current.left
}
if (successor != delNode.right){
successorParent.left = successor.right
successor.right = delNode.right
}
return successor
}
}