当前位置:首页 > 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实现跳转

使用 window.location 跳转 通过修改 window.location.href 实现页面跳转,这是最常用的方法。 window.location.href = 'https://…

js实现全屏

js实现全屏

实现全屏的基本方法 使用JavaScript实现全屏功能主要依赖Element.requestFullscreen()方法。现代浏览器均支持此API,但不同浏览器可能需要添加前缀。 // 触发全屏…

js计算器的实现

js计算器的实现

实现基础结构 HTML部分需要创建计算器的基本框架,包含显示区域和按钮。使用<div>容器包裹整个计算器,内部设置一个显示屏和多个按钮。 <div class="calculato…

js 实现日历

js 实现日历

实现日历的基本思路 日历的核心功能是展示日期,并允许用户进行日期选择或导航。JavaScript 可以动态生成日历的 HTML 结构,并处理用户交互逻辑。 基础日历结构 日历通常包含头部(显示月份和…

js实现截图

js实现截图

使用HTML2Canvas库实现截图 HTML2Canvas是一个流行的JavaScript库,可以将网页元素或整个页面转换为Canvas图像。安装方式: npm install html2canv…

vue.js实现checkbox

vue.js实现checkbox

使用 v-model 绑定复选框 在 Vue.js 中,可以通过 v-model 指令实现复选框的双向数据绑定。复选框的值会与 Vue 实例中的数据属性同步。 <template>…