定义
function BinarySearchTree(){
function Node(key){
this.key = key
this.left = null
this.right = null
}
this.root = null
}
插入节点
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(node.right == null){
node.right = newNode
}
else{
this.insertNode(node.right,newNode)
}
}
}
先序遍历(递归)
BinarySearchTree.prototype.preOrderTraversal = function(handler){
this.preOrderTraversalNode(this.root,handler)
}
BinarySearchTree.prototype.preOrderTraversalNode = function(node,handler){
if(node != null){
handler(node.key)
this.preOrderTraversalNode(node.left,handler)
this.preOrderTraversalNode(node.right,handler)
}
}
先序遍历(非递归)
BinarySearchTree.prototype.preTraversal = function(head){
if(head != null){
let stack = []
stack.push(head)
while(stack.length!=0){
head = stack.pop()
console.log(head.key)
if(head.right!=null) stack.push(head.right)
if(head.left!=null) stack.push(head.left)
}
}
}
中序遍历(递归)
BinarySearchTree.prototype.midOrderTraversal = function(handler){
this.midOrderTraversalNode(this.root,handler)
}
BinarySearchTree.prototype.midOrderTraversalNode = function(node,handler){
if(node != null){
this.midOrderTraversalNode(node.left,handler)
handler(node.key)
this.midOrderTraversalNode(node.right,handler)
}
}
中序遍历(非递归)
BinarySearchTree.prototype.midTraversal = function(head){
if(head!=null){
let s = []
while(s.length!=0||head!=null){
if(head!=null){
s.push(head)
head= head.left
}else{
head = s.pop()
console.log(head.key)
head = head.right
}
}
}
}
后序遍历(递归)
BinarySearchTree.prototype.aftOrderTraversal = function(handler){
this.aftOrderTraversalNode(this.root,handler)
}
BinarySearchTree.prototype.aftOrderTraversalNode = function(node,handler){
if(node != null){
this.aftOrderTraversalNode(node.left,handler)
this.aftOrderTraversalNode(node.right,handler)
handler(node.key)
}
}
后序遍历(非递归)
BinarySearchTree.prototype.aftTraversal = function(head){
if(head!=null){
let s1 = []
let s2 = []
s1.push(head)
while(s1.length!=0){
head = s1.pop()
s2.push(head)
if(head.left!=null) s1.push(head.left)
if(head.right!=null) s1.push(head.right)
}
while(s2.length!=0){
console.log(s2.pop().key)
}
}
获取最大值
BinarySearchTree.prototype.getMaxNode = function(){
let node = this.root
while(node.right != null){
node = node.right
}
return node.key
}
获取最小值
BinarySearchTree.prototype.getMinNode = function(){
let node = this.root
while(node.left != null){
node = node.left
}
return node.key
}
递归查找key
BinarySearchTree.prototype.search = function(key){
return this.searchNode(this.root,key)
}
BinarySearchTree.prototype.searchNode = function(node,key){
if(node === null){
return false
}
if(node.key > key){
return this.searchNode(node.left,key)
}else if(node.key < key){
return this.searchNode(node.right,key)
}else{
return true
}
}
while查找key
BinarySearchTree.prototype.searchByWhile = 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 true
}
}
return false
}
删除节点
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.right == null && current.left==null){
if(current == this.root){
this.root = null
}else if(isLeftChild){
parent.left = null
}else{
parent.right = null
}
}
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 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{
var successer = this.getSuccssor(current)
if(this.root === current){
this.root = successer
successer.left = current.left
}else if(isLeftChild){
parent.left=successer
}else{
parent.right = successer
}
successer.left = current.left
}
return true
}
BinarySearchTree.prototype.getSuccssor = function(delNode){
let successer = delNode
let current = delNode.right
let successerParent = delNode
while(current != null){
successerParent = successer
successer = current
current = current.left
}
if(successer != delNode.right){
successerParent.left = successer.right
successer.right = delNode.right
}
return successer
}