function BinarySearchTree(){
function Node(key){
this.key = key
this.left = null
this.right = null
}
this.root = null
BinarySearchTree.prototype.insert = function insert(key){
const newNode = new Node(key)
if(this.root == null){
this.root = newNode
}else{
this.insertNode(this.root,newNode)
}
}
BinarySearchTree.prototype.insertNode = function(node,newNode){
if(node.key<newNode.key){
if(node.right == null){
node.right = newNode
}else{
this.insertNode(node.right,newNode)
}
}else{
if(node.left == null){
node.left = newNode
}else{
this.insertNode(node.left,newNode)
}
}
}
}
BinarySearchTree.prototype.preOverTraverses = function preOverTraverses(){
this.preOverTraversesNode(this.root)
}
BinarySearchTree.prototype.preOverTraversesNode = function(node){
if(node == null) return ;
console.log(node.key);
this.preOverTraversesNode(node.left)
this.preOverTraversesNode(node.right)
}
BinarySearchTree.prototype.inOverTraverses = function inOverTraverses(){
this.OverTraversesNode(this.root)
}
BinarySearchTree.prototype.OverTraversesNode = function(node){
if(node == null) return ;
this.OverTraversesNode(node.left)
console.log(node.key);
this.OverTraversesNode(node.right)
}
BinarySearchTree.prototype.outOverTraverses = function outOverTraverses(){
this.ouTraversesNode(this.root)
}
BinarySearchTree.prototype.ouTraversesNode = function(node){
if(node == null) return ;
this.ouTraversesNode(node.left)
this.ouTraversesNode(node.right)
console.log(node.key);
}
BinarySearchTree.prototype.max = function max(){
let node = this.root
while(node.right != null){
node = node.right
}
return node.key
}
BinarySearchTree.prototype.min = function min(){
let node = this.root
while(node.left != null){
node = node.left
}
return node.key
}
BinarySearchTree.prototype.search = function search(key){
return this.lSearch(this.root,key)
}
BinarySearchTree.prototype.lSearch = function (node,key){
if(node == null) return false
if(node.key>key){
return this.lSearch(node.left,key)
}else if(node.key<key){
return this.lSearch(node.right,key)
}else{
return true
}
}
BinarySearchTree.prototype.remove = function remove(key){
let current = this.root
let parent = null
let isLeft = true
while(current.key != key){
parent = current
if(key<current.key){
isLeft = true
current = current.left
}else{
isLeft = 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(isLeft){
parent.left = null
}else{
parent.right = null
}
}
else if(current.right == null){
if(current == this.root){
this.root = current.left
}else if(isLeft){
current.left = parent.left
}else{
current.left = parent.right
}
}else if(current.left == null){
if(current == this.root){
this.root = current.right
}else if(isLeft){
current.right = parent.left
}else{
parent.right = current.right
}
}else{
let successor = this.getSuccess(current)
if(this.root == current){
this.root = successor
}else if(isLeft){
parent.left = successor
}else{
parent.right = successor
}
successor.left = current.left
}
return true
}
BinarySearchTree.prototype.getSuccess = function getSuccess(delNode){
let successParent = delNode;
let success = delNode;
let current = delNode.right
while(current != null){
successParent = success
success = current;
current = current.left
}
if(success != delNode.right){
successParent.left = success.right
success.right = delNode.right
}
return success
}
const binarysearchtree = new BinarySearchTree()
|