当前位置:首页 > JavaScript

js实现红

2026-04-03 20:17:46JavaScript

红黑树的基本概念

红黑树是一种自平衡的二叉搜索树,具有以下特性:

  1. 每个节点是红色或黑色。
  2. 根节点是黑色。
  3. 每个叶子节点(NIL节点)是黑色。
  4. 红色节点的子节点必须是黑色(不能有连续的红色节点)。
  5. 从任一节点到其叶子节点的路径包含相同数量的黑色节点。

红黑树的节点结构

用JavaScript定义红黑树节点的结构:

class RBNode {
  constructor(key, color = 'RED') {
    this.key = key;
    this.left = null;
    this.right = null;
    this.parent = null;
    this.color = color; // 'RED' or 'BLACK'
  }
}

红黑树的插入操作

插入新节点后,可能破坏红黑树的性质,需要通过旋转和重新着色修复:

  1. 标准二叉搜索树插入:按二叉搜索树规则插入新节点,初始颜色为红色。
  2. 修复红黑树性质:检查父节点和叔节点的颜色,根据情况旋转或重新着色。
class RedBlackTree {
  constructor() {
    this.NIL = new RBNode(null, 'BLACK'); // 叶子节点
    this.root = this.NIL;
  }

  insert(key) {
    const newNode = new RBNode(key);
    newNode.left = this.NIL;
    newNode.right = this.NIL;

    let parent = null;
    let current = this.root;

    while (current !== this.NIL) {
      parent = current;
      if (newNode.key < current.key) {
        current = current.left;
      } else {
        current = current.right;
      }
    }

    newNode.parent = parent;
    if (parent === null) {
      this.root = newNode;
    } else if (newNode.key < parent.key) {
      parent.left = newNode;
    } else {
      parent.right = newNode;
    }

    this.fixInsert(newNode);
  }

  fixInsert(node) {
    while (node.parent && node.parent.color === 'RED') {
      if (node.parent === node.parent.parent.left) {
        const uncle = node.parent.parent.right;
        if (uncle.color === 'RED') {
          node.parent.color = 'BLACK';
          uncle.color = 'BLACK';
          node.parent.parent.color = 'RED';
          node = node.parent.parent;
        } else {
          if (node === node.parent.right) {
            node = node.parent;
            this.leftRotate(node);
          }
          node.parent.color = 'BLACK';
          node.parent.parent.color = 'RED';
          this.rightRotate(node.parent.parent);
        }
      } else {
        // 对称逻辑,处理右子树情况
      }
    }
    this.root.color = 'BLACK';
  }

  leftRotate(x) {
    const y = x.right;
    x.right = y.left;
    if (y.left !== this.NIL) {
      y.left.parent = x;
    }
    y.parent = x.parent;
    if (x.parent === null) {
      this.root = y;
    } else if (x === x.parent.left) {
      x.parent.left = y;
    } else {
      x.parent.right = y;
    }
    y.left = x;
    x.parent = y;
  }

  rightRotate(y) {
    const x = y.left;
    y.left = x.right;
    if (x.right !== this.NIL) {
      x.right.parent = y;
    }
    x.parent = y.parent;
    if (y.parent === null) {
      this.root = x;
    } else if (y === y.parent.right) {
      y.parent.right = x;
    } else {
      y.parent.left = x;
    }
    x.right = y;
    y.parent = x;
  }
}

红黑树的删除操作

删除操作更复杂,需处理多种情况以保持平衡:

  1. 标准二叉搜索树删除:找到后继节点并替换。
  2. 修复红黑树性质:根据兄弟节点和其子节点的颜色调整。
// 删除和修复逻辑较长,此处省略,可参考经典算法实现

应用场景

红黑树用于高效维护有序数据,常见于:

js实现红

  • JavaScript引擎(如V8)的MapSet实现。
  • 数据库索引结构。
  • 时间复杂度的保证:插入、删除、查找均为O(log n)。

以上代码展示了红黑树的核心逻辑,实际实现需补充完整删除和对称情况的处理。

标签: js
分享给朋友:

相关文章

js实现图片上传

js实现图片上传

图片上传的基本实现 使用HTML的<input type="file">元素配合JavaScript的File API可以实现图片上传功能。HTML部分需要创建一个文件选择输入框和一个用于…

js实现全选

js实现全选

实现全选功能的方法 在JavaScript中实现全选功能通常涉及监听全选复选框的点击事件,并根据其状态控制其他复选框的选中状态。以下是几种常见的实现方式: 基础DOM操作实现 通过获取所有目标复选框…

js实现瀑布流

js实现瀑布流

实现瀑布流布局 瀑布流布局是一种常见的网页布局方式,常用于图片展示、商品列表等场景。以下是使用 JavaScript 实现瀑布流布局的几种方法。 纯 JavaScript 实现 通过计算元素的位置和…

js实现点击显示和隐藏

js实现点击显示和隐藏

实现点击显示和隐藏的JavaScript方法 使用classList.toggle切换类名 通过添加/移除CSS类控制元素的显示与隐藏,需提前在CSS中定义隐藏样式(如display: none)。…

js实现搜索

js实现搜索

实现搜索功能的方法 在JavaScript中实现搜索功能可以通过多种方式完成,以下是几种常见的实现方法。 使用数组的filter方法 通过数组的filter方法可以筛选出符合条件的数据项。假设有一个…

js分页实现

js分页实现

分页的基本原理 分页的核心是通过计算当前页码和数据偏移量,从服务器或本地数据中截取对应范围的数据进行展示。通常需要以下参数:当前页码(currentPage)、每页条数(pageSize)、总数据量(…