function BinarySearchTree() {
this.root = null;
function Node(key) {
this.key = key;
this.right = null;
this.left = null;
}
BinarySearchTree.prototype.insert = function(key) {
const newNode = new Node(key);
if(!this.root) {
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)
}
}
if(newNode.key > node.key) {
if(node.right == null) {
node.right = newNode;
} else {
this.insertNode(node.right,newNode)
}
}
}
BinarySearchTree.prototype.preOrderTraverse = function(handler) {
this.preOrderTraverseNode(this.root,handler)
}
BinarySearchTree.prototype.preOrderTraverseNode = function(node,handler) {
if(node !== null) {
handler(node.key)
this.preOrderTraverseNode(node.left,handler)
this.preOrderTraverseNode(node.right,handler)
}
}
BinarySearchTree.prototype.midOrderTraverse = function(handler) {
this.midOrderTraverseNode(this.root,handler)
}
BinarySearchTree.prototype.midOrderTraverseNode = function(node,handler) {
if(node !==null) {
this.midOrderTraverseNode(node.left,handler);
handler(node.key);
this.midOrderTraverseNode(node.right,handler);
}
}
BinarySearchTree.prototype.postOrderTraverse = function(handler) {
this.postOrderTraverseNode(this.root,handler)
}
BinarySearchTree.prototype.postOrderTraverseNode = function(node,handler) {
if(node !==null) {
this.postOrderTraverseNode(node.left,handler);
this.postOrderTraverseNode(node.right,handler);
handler(node.key);
}
}
BinarySearchTree.prototype.min = function() {
let node = this.root;
while(node.left !== null) {
node = node.left;
}
return node.key;
}
BinarySearchTree.prototype.max = function() {
let node = this.root;
while(node.right !== null) {
node = node.right
}
return node.key
}
BinarySearchTree.prototype.remove = function(key) {
let current = this.root;
let parent = null;
let isLeft = false;
while(current.key != key) {
parent = current;
if(key < current.key) {
current = current.left;
isLeft = true;
} else {
current = current.right;
isLeft = false
}
if(current == null) return false;
}
if(current.right == null && current.left == null) {
if(current == this.root) {
this.root = null;
} else if(isLeft) {
parentleft = null;
} else {
parent.right = null;
}
}
else if(current.right == null) {
if(current == this.root) {
this.root = current.left
} else if(isLeft) {
parent.left = current.left
} else {
parent.right = current.left
}
} else if(current.left == null) {
if(current == this.root) {
this.root = current.right
} else if(isLeft) {
parent.left = current.right
} else {
parent.right =current.right
}
} else {
let successor = this.getSuccessor(current);
if(current == this.root) {
this.root = successor
} else if(isLeft) {
parent.left = successor
} else {
parent.right = successor
}
}
}
BinarySearchTree.prototype.getSuccessor = function(delNode) {
let current = delNode.right;
let successorParent = delNode;
let successor = delNode.right;
while(current.left != null) {
successorParent = successor;
current = current.left;
successor = current;
console.log(successor)
}
if (successor != delNode.right) {
successorParent.left = successor.right;
successor.right = delNode.right;
}
return successor;
}
}
const BST = new BinarySearchTree();
BST.insert(11);
BST.insert(7);
BST.insert(15);
BST.insert(5);
BST.insert(3);
BST.insert(9);
BST.insert(8);
BST.insert(10);
BST.insert(13);
BST.insert(12);
BST.insert(14);
BST.insert(20);
BST.insert(18);
BST.insert(25);
BST.insert(6);
BST.remove(9)
console