手写一个排序二叉树:
function BinaryTree() {
var Node = function (key) {
this.key = key;
this.left = null;
this.right = null;
};
var root = null;
var insertNode = function (node, newNode) {
if (newNode.key < node.key) {
if (node.left === null) {
node.left = newNode;
} else {
insertNode(node.left, newNode);
}
} else {
if (node.right === null) {
node.right = newNode;
} else {
insertNode(node.right, newNode);
}
}
}
this.insert = function (key) {
var newNode = new Node(key);
if (root === null) {
root = newNode;
} else {
insertNode(root, newNode);
}
}
}
var nodes = [8, 3, 10, 1, 6, 14, 4, 7, 13];
var binaryTree = new BinaryTree();
nodes.forEach(function (key) {
binaryTree.insert(key);
});
实现二叉树的前、中、后序遍历:
function BinaryTree() {
var Node = function (key) {
this.key = key;
this.left = null;
this.right = null;
};
var root = null;
var insertNode = function (node, newNode) {
if (newNode.key < node.key) {
if (node.left === null) {
node.left = newNode;
} else {
insertNode(node.left, newNode);
}
} else {
if (node.right === null) {
node.right = newNode;
} else {
insertNode(node.right, newNode);
}
}
}
this.insert = function (key) {
var newNode = new Node(key);
if (root === null) {
root = newNode;
} else {
insertNode(root, newNode);
}
}
var ergodicNode = function (node, callback) {
if (node !== null) {
// callback(node.key); //前序遍历
ergodicNode(node.left, callback);
callback(node.key); //中序遍历
ergodicNode(node.right, callback);
// callback(node.key); //后序遍历
}
}
this.ergodic = function (callback) {
ergodicNode(root, callback);
}
}
var nodes = [8, 3, 10, 1, 6, 14, 4, 7, 13];
var binaryTree = new BinaryTree();
nodes.forEach(function (key) {
binaryTree.insert(key);
});
var callback = function (key) {
console.log(key);
}
binaryTree.ergodic(callback);
// 中序遍历输出结果:1 3 4 6 7 8 10 13 14
// 前序遍历输出结果:8 3 1 6 4 7 10 14 13
// 后序遍历输出结果:1 4 7 6 3 13 14 10 8
二叉树节点的最大值、最小值、查找某个节点、删除某个节点、删除中间节点:
function BinaryTree() {
var Node = function (key) {
this.key = key;
this.left = null;
this.right = null;
};
var root = null;
var insertNode = function (node, newNode) {
if (newNode.key < node.key) {
if (node.left === null) {
node.left = newNode;
} else {
insertNode(node.left, newNode);
}
} else {
if (node.right === null) {
node.right = newNode;
} else {
insertNode(node.right, newNode);
}
}
}
this.insert = function (key) {
var newNode = new Node(key);
if (root === null) {
root = newNode;
} else {
insertNode(root, newNode);
}
}
// 二叉树节点的最小值
this.min = function () {
return minNode(root);
}
var minNode = function (node) {
if (node) {
while (node && node.left !== null) {
node = node.left;
}
return node;
}
return null;
}
// 二叉树节点的最大值
this.max = function () {
return maxNode(root);
}
var maxNode = function (node) {
if (node) {
while (node && node.right !== null) {
node = node.right;
}
return node.key;
}
return null;
}
// 二叉树节点某个指定值
var searchNode = function (node, key) {
if (node === null) {
return false;
}
if (key < node.key) {
return searchNode(node.left, key);
} else if (key > node.key) {
return searchNode(node.right, key);
} else {
return true;
}
}
this.search = function (key) {
return searchNode(root, key);
}
// 删除二叉树的叶子节点
/*
var removeNode = function (node, key) {
if (node === null) {
return null;
}
if (key < node.key) {
node.left = removeNode(node.left, key);
return node;
} else if (key > node.key) {
node.right = removeNode(node.right, key);
return node;
} else {
if (node.left === null && node.right === null) {
node = null;
return node;
}
}
}
*/
// 删除二叉树的某个中间节点
var removeNode = function (node, key) {
if (node === null) {
return null;
}
if (key < node.key) {
node.left = removeNode(node.left, key);
return node;
} else if (key > node.key) {
node.right = removeNode(node.right, key);
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 = minNode(node.right);
node.key = aux.key;
node.right = removeNode(node.right, aux.key);
return node;
}
}
this.remove = function (key) {
root = removeNode(root, key);
}
}
var nodes = [8, 3, 10, 1, 6, 14, 4, 7, 13];
var binaryTree = new BinaryTree();
nodes.forEach(function (key) {
binaryTree.insert(key);
});
// 查找二叉树节点的最小值:
console.log(binaryTree.min().key); // 1
// 查找二叉树节点的最大值:
console.log(binaryTree.max()); // 14
// 查找二叉树某个指定的节点:
console.log(binaryTree.search(7) ? 'key 7 is found' : 'key 7 is not found'); //key 7 is found
console.log(binaryTree.search(9) ? 'key 9 is found' : 'key 9 is not found'); //key 9 is not found
// 删除二叉树某个指定的节点
// binaryTree.remove(1);
// console.log(binaryTree.search(1) ? 'key 1 is found' : 'key 1 is not found'); //key 1 is not found
// 删除二叉树的某个中间节点
// binaryTree.remove(10);
// console.log(binaryTree.search(10) ? 'key 10 is found' : 'key 10 is not found'); //key 10 is not found
binaryTree.remove(3);
console.log(binaryTree.search(3) ? 'key 3 is found' : 'key 3 is not found'); //key 3 is not found
链接:https://www.imooc.com/video/15742【慕课网:Javascript实现二叉树算法】