二叉排序树(BinarySortTree),又称二叉查找树、二叉搜索树。它或者是一棵空树;或者是具有下列性质的二叉树:若左子树不空,则左子树上所有结点的值均小于它的根结点的值;若右子树不空,则右子树上所有结点的值均大于它的根结点的值;左、右子树也分别为二叉排序树。若子树为空,查找不成功。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<script> // 二叉树 function BinaryTree() { var Node = function (key) { this.key = key this.left = null this.right = null } // 根节点 this.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 (this.root === null) { this.root = newNode } else { insertNode(this.root, newNode) } } // 遍历节点值: 中序遍历 var inOrderTraverseNode = function (node, callback) { if(node != null) { inOrderTraverseNode(node.left, callback) callback(node.key) inOrderTraverseNode(node.right, callback) } } this.inOrderTraverse = function (callback) { inOrderTraverseNode(this.root, callback) } // 遍历节点值:前序遍历 var preOrderTraverseNode = function (node, callback) { if(node != null) { callback(node.key) preOrderTraverseNode(node.left, callback) preOrderTraverseNode(node.right, callback) } } this.preOrderTraverse = function (callback) { preOrderTraverseNode(this.root, callback) } // 遍历节点值:后序遍历 var postOrderTraverseNode = function (node, callback) { if(node != null) { postOrderTraverseNode(node.left, callback) postOrderTraverseNode(node.right, callback) callback(node.key) } } this.postOrderTraverse = function (callback) { postOrderTraverseNode(this.root, callback) } // 查找最小值 var minNode = function (node) { if(node) { while (node && node.left != null) { node = node.left } return node.key } return null } this.min = function () { return minNode(this.root) } // 查找最大值 var maxNode = function (node) { if(node) { while (node && node.right != null) { node = node.right } return node.key } return null } this.max = function () { return maxNode(this.root) } // 查找节点值 var searchNode = function (node, key) { if(!node) { return false } if(node.key < key) { return searchNode(node.right, key) } else if (node.key > key) { return searchNode(node.left, key) } else { return true } } this.search = function (key) { return searchNode(this.root, key) } // 查找最小节点 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) { if(node.key > key) { node.left = removeNode(node.left, key) return node } else if (node.key < key) { node.right = removeNode(node.right, key) return node } else { // 叶子节点则直接删除 if (node.left == null && node.right == null) { return null } if (node.left == null) { // 节点没有左子树,则返回右子树 return node.right } else if(node.right == null) { // 节点没有右子树,则返回左子树 return node.left } // 节点存在左右节点,则把右子树中最小的节点和当前节点替换同时删除右子树中的最小节点 var minNode = findMinNode(node.right) node.key = minNode.key node.right = removeNode(node.right, minNode.key) return node } } } this.remove = function (key) { removeNode(this.root, key) } } var nodes = [8, 3, 10, 1, 6, 14, 4, 7, 13]; var binaryTree = new BinaryTree(); nodes.forEach((node) => { binaryTree.insert(node) }) var callback = function (key) { console.log(key) } console.log('--------中序遍历---------') binaryTree.inOrderTraverse(callback) //1 3 4 6 7 8 10 13 14 console.log('--------前序遍历---------') binaryTree.preOrderTraverse(callback) //8 3 1 6 4 7 10 14 13 console.log('--------后序遍历---------') binaryTree.postOrderTraverse(callback) //1 4 7 6 3 13 14 10 8 console.log('最小值为:' + binaryTree.min() + ',最大值为:' + binaryTree.max()) // 最小值为:1,最大值为:14 console.log(binaryTree.search(17) ? '17 存在' : '17 不存在') // 17 不存在 binaryTree.remove(3) console.log(binaryTree) </script>
</body>
</html>