class BinarySearchTree {
constructor() {
// 节点类
class Node {
constructor(key) {
this.key = key;
this.left = null;
this.right = null;
}
}
// 二叉搜索树的根
this.root = null;
// 1.插入数据
BinarySearchTree.prototype.insert = function (key) {
var newNode = new Node(key);
// 判断有无根节点
if (this.root == null) this.root = newNode;
else {
//递归查询
this.insertNode(this.root, newNode)
}
}
// 将已有的节点和新节点进行比较
BinarySearchTree.prototype.insertNode = function (node, newNode) {
// 如果新插入节点比原节点小,则向左查找
if (newNode.key < node.key) {
//如果node的左子树为空,则将newnode作为node的左子树存储起来
if (node.left == null) node.left = newNode;
// 否则将继续递归
else {
this.insertNode(node.left, newNode)
}
}
//如果新插入的节点比原节点大,则向右查找
else {
// 如果node的右子树为空,则将newnode作为node的右子树
if (node.right == null) node.right = newNode
// 否则将继续递归查询
else {
this.insertNode(node.right, newNode)
}
}
}
//2.树的遍历
// 先序遍历:最先处理根节点
// handler为调用该方法时传入的某个回调函数(处理函数)
BinarySearchTree.prototype.preOrderTraversal = function (handler) {
this.preOrderTraversalNode(this.root, handler)
}
BinarySearchTree.prototype.preOrderTraversalNode = function (node, handler) {
if (node != null) {
//先处理传入的节点
handler(node.key);
//以递归的形式先序遍历所有左子树
this.preOrderTraversalNode(node.left, handler);
// 接着先序遍历所有的右子树
this.preOrderTraversalNode(node.right, handler)
}
}
// 中序遍历:先遍历左子树,再访问根节点,再遍历右子树
BinarySearchTree.prototype.midOrderTraversal = function (handler) {
this.midOrderTraversalNode(this.root, handler)
}
BinarySearchTree.prototype.midOrderTraversalNode = function (node, handler) {
if (node != null) {
//以递归的形式中序遍历所有左子树
this.midOrderTraversalNode(node.left, handler);
// 处理传入的节点
handler(node.key);
// 接着中序遍历所有的右子树
this.midOrderTraversalNode(node.right, handler)
}
}
// 后序遍历:后序遍历左子树,再遍历右子树,最后访问根节点
BinarySearchTree.prototype.postOrderTraversal = function (handler) {
this.postOrderTraversalNode(this.root, handler)
}
BinarySearchTree.prototype.postOrderTraversalNode = function (node, handler) {
if (node != null) {
//以递归的形式后序遍历所有左子树
this.postOrderTraversalNode(node.left, handler);
// 接着后序遍历所有的右子树
this.postOrderTraversalNode(node.right, handler);
// 处理传入的节点
handler(node.key);
}
}
// 3.获取最小值的方法
BinarySearchTree.prototype.min = function () {
var node = this.root;
while (node.left != null) {
node = node.left
}
return node.key
}
// 4.获取最大值的方法
BinarySearchTree.prototype.max = function () {
var node = this.root;
while (node.right != null) {
node = node.right
}
return node.key
}
// 5.搜索某一个key
BinarySearchTree.prototype.search = function (key) {
var node = this.root;
while (node != null) {
// 判断传入的key值大小与node.key的关系,以此决定往哪个方向查询
if (key > node.key) {
node = node.right
} else if (key < node.key) {
node = node.left
} else { return false }
return true
}
}
// 6.删除某个指定节点
BinarySearchTree.prototype.remove=function(key){
// 寻找要删除的节点,先定义相关变量
var current=this.root;
var parent=null;
var isLeftChild=true;
// 开始查询节点
while(current.key!=key){
parent=current;
if(key<current.key){
isLeftChild=true;
current=current.left
}
else{
isLeftChild=false;
current=current.right
}
// 如果找到最后也没有找到节点
if(current==null) return false
}
// 根据对应的情况采用不同的方式删除节点
// 如果删除的节点是叶子节点,即没有子节点
if(current.left==null&¤t.right==null){
//如果只有一个单独的根
if(current==this.root){
this.root=null
}else if(isLeftChild){
parent.left=null
}else{
parent.right=null
}
}
// 如果删除的节点只有一个子节点
// 先判断该节点的子节点是父节点的左子节点还是右子节点
// 如果为父节点的左子节点
else if(current.right==null){
// 判断当前节点是否是根节点
if(current==this.root){
this.root=current.left
}
// 如果当前节点的子节点是左子节点
else if(isLeftChild){
parent.left=current.left
}
// 如果当前节点的子节点是右子节点
else{
parent.left=current.right
}
}
// 如果当前节点为父节点的右子节点
else if(current.left==null){
// 判断当前节点是否是根节点
if(current==this.root){
this.root=current.right
}
// 如果当前节点的子节点是左子节点
else if(isLeftChild){
parent.right=current.left
}
// 如果当前节点的子节点是右子节点
else{
parent.right=current.right
}
}
// 如果删除的节点有两个子节点,该情况比较复杂,考虑的情况也比较多
}
}
}
JavaScript实现二叉搜索树结构以及部分方法的封装
最新推荐文章于 2023-12-04 09:41:16 发布