代码拉取完成,页面将自动刷新
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>排序二叉树</title>
</head>
<body>
<script>
var BinaryTree = function() {
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 (newNode.key > node.key) {
if (node.right === null) {
node.right = newNode
} else {
insertNode(node.right, newNode)
}
}
}
this.insert = function(key) {
var node = new Node(key)
if (root === null) {
root = node
} else {
insertNode(root, node)
}
}
this.show_root = function() {
console.log(JSON.stringify(root))
}
var inOrderTraverseNode = function(node, callback) {
if (node !== null) {
inOrderTraverseNode(node.left, callback);
callback(node.key);
inOrderTraverseNode(node.right, callback);
}
}
var preOrderTraverse = function(node, callback) {
if (node !== null) {
callback(node.key);
preOrderTraverse(node.left, callback);
preOrderTraverse(node.right, callback);
}
}
var postOrderTraverse = function(node, callback) {
if (node !== null) {
postOrderTraverse(node.left, callback);
postOrderTraverse(node.right, callback);
callback(node.key);
}
}
this.inOrderTraverse = function(callback) {
inOrderTraverseNode(root, callback); // 从小到大排序
}
this.preOrderTraverse = function(callback) {
preOrderTraverse(root, callback); // 复制二叉树
}
this.postOrderTraverse = function(callback) {
postOrderTraverse(root, callback); // 文件系统遍历
}
var minNode = function(node) {
if (node) {
while (node && node.left !== null) {
node = node.left
}
return node.key
}
return null;
}
var maxNode = function(node) {
if (node) {
while (node && node.right !== null) {
node = node.right
}
return node.key
}
return null;
}
var findNode = function(node, key) {
if (node) {
if (node.key === key) {
return node
}
if (key < node.key) {
return findNode(node.left, key)
}
if (key > node.key) {
return findNode(node.right, key)
}
}
return null
}
var findMinNode = function(node){
if(node){
while(node && node.left !== null){
node = node.left;
}
return node
}
return null
}
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;
}
if(node.right === null){
node = node.left;
return node;
}
var minNode = findMinNode(node.right);
node.key = minNode.key;
node.right = removeNode(node.right,minNode.key);
return node;
}
}
// 最小
this.min = function() {
return minNode(root)
}
// 最大
this.max = function() {
return maxNode(root)
}
// 查找
this.findNode = function(key) {
return findNode(root, key)
}
// 删除
this.remove = function(key){
return removeNode(root,key)
}
}
var nodes = [8, 3, 10, 1, 6, 14, 4, 7, 13];
var binaryTree = new BinaryTree();
nodes.forEach((item, index) => {
binaryTree.insert(item)
})
binaryTree.show_root();
var callback = console.log;
// binaryTree.inOrderTraverse(callback)
// binaryTree.preOrderTraverse(callback)
// binaryTree.postOrderTraverse(callback)
console.log('min:', binaryTree.min())
console.log('max:', binaryTree.max())
console.log(binaryTree.findNode(14))
console.log('remove',binaryTree.remove(8))
binaryTree.show_root();
</script>
</body>
</html>
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。