* 比较大小的类 ./utils/Comparator.js
https://blog.csdn.net/fareast_mzh/article/details/81810822
* BinarySearchTree.js
基本版: https://blog.csdn.net/fareast_mzh/article/details/81746269 元素只能是Number类型
/**
* Created by PhpStorm.
* User: Mch
* Date: 8/16/18
* Time: 23:26
*/
var Comparator = require('./utils/Comparator.js').Comparator;
function Node(key) {
this.key = key;
this.left = null;
this.right = null;
}
function BinarySearchTree(comparatorFunction) {
this.root = null;
this.comparator = new Comparator(comparatorFunction);
}
BinarySearchTree.prototype.insert = function(key) {
var newNode = new Node(key);
if (this.root === null) {
this.root = newNode;
} else {
BinarySearchTree.insertNode(this.root, newNode, this.comparator);
}
};
BinarySearchTree.insertNode = function(node, newNode, cmp) {
if (cmp.lessThan(newNode.key, node.key)) {
if (node.left === null) {
node.left = newNode;
} else {
BinarySearchTree.insertNode(node.left, newNode, cmp);
}
} else {
if (node.right === null) {
node.right = newNode;
} else {
BinarySearchTree.insertNode(node.right, newNode, cmp);
}
}
};
BinarySearchTree.inOrderTraverseNode = function(node, callback) {
if (node !== null) {
BinarySearchTree.inOrderTraverseNode(node.left, callback);
callback(node.key);
BinarySearchTree.inOrderTraverseNode(node.right, callback);
}
};
BinarySearchTree.prototype.inOrderTraverse = function(callback) {
BinarySearchTree.inOrderTraverseNode(this.root, callback);
};
BinarySearchTree.findMinNode = function(node) {
if (node) {
while (node && node.left !== null) {
node = node.left;
}
return node;
}
return null;
};
BinarySearchTree.prototype.min = function() {
return BinarySearchTree.minNode(this.root);
};
BinarySearchTree.minNode = function(node) {
if (node) {
while (node && node.left !== null) {
node = node.left;
}
return node.key;
}
return null;
};
BinarySearchTree.prototype.max = function() {
return BinarySearchTree.maxNode(this.root);
};
BinarySearchTree.maxNode = function(node) {
if (node) {
while (node && node.right !== null) {
node = node.right;
}
return node.key;
}
return null;
};
BinarySearchTree.prototype.search = function(key) {
return BinarySearchTree.searchNode(this.root, key, this.comparator);
};
BinarySearchTree.searchNode = function(node, key, cmp) {
if (node === null) {
return false;
}
if (cmp.lessThan(key, node.key)) {
return BinarySearchTree.searchNode(node.left, key, cmp);
} else if (this.comparator.greaterThan(key, node.key)) {
return BinarySearchTree.searchNode(node.right, key, cmp);
}
return true;
};
BinarySearchTree.prototype.remove = function(key) {
this.root = BinarySearchTree.removeNode(this.root, key, this.comparator);
};
BinarySearchTree.removeNode = function(node, key, cmp) {
if (node === null) {
return null;
}
if (cmp.lessThan(key, node.key)) {
node.left = BinarySearchTree.removeNode(node.left, key, cmp);
return node;
} else if (cmp.greaterThan(key, node.key)) {
node.right = BinarySearchTree.removeNode(node.right, key, cmp);
return node;
} else {
if (node.left === null && node.right === null) {
node = null;
return node;
}
if (node.left === null) {
node = node.right;
return node;
} else if (node.right === null) {
node = node.left;
return node;
}
var aux = BinarySearchTree.findMinNode(node.right);
node.key = aux.key;
node.right = BinarySearchTree.removeNode(node.right, aux.key, cmp);
return node;
}
};
BinarySearchTree.prototype.toArray = function() {
var a = [];
this.inOrderTraverse(function(el) {
a.push(el);
});
return a;
};
exports.BinarySearchTree = BinarySearchTree;
test:
var BinarySearchTree = require('./BinarySearchTree.js').BinarySearchTree;
var tree = new BinarySearchTree(function(a, b) {
return a-b;
});
[7,15,5,3,9,8,10,13,11,12,14,20,18,25].forEach(function(el) {
tree.insert(el);
});
console.log(tree.toArray());
console.log(tree.min());
console.log(tree.max());
tree.remove(14);
console.log(tree.toArray());
tree.remove(11);
tree.remove(3);
tree.remove(25);
console.log(tree.toArray());
$ node app.js
[ 3, 5, 7, 8, 9, 10, 11, 12, 13, 14, 15, 18, 20, 25 ]
3
25
[ 3, 5, 7, 8, 9, 10, 11, 12, 13, 15, 18, 20, 25 ]
[ 5, 7, 8, 9, 10, 12, 13, 15, 18, 20 ]