如何使用 JavaScript 实现红黑树的增删改查操作? - 项越资源网-html css js 用法分享社区-开发交流-项越资源网

如何使用 JavaScript 实现红黑树的增删改查操作?

/* 如何使用 JavaScript 实现红黑树的增删改查操作? */
var RedBlackTree = function() {
  this.root = null;
};
var Node = function(key, value, color) {
  this.key = key;
  this.value = value;
  this.color = color;
  this.left = null;
  this.right = null;
};
RedBlackTree.prototype.insert = function(key, value) {
  var newNode = new Node(key, value, 'red');
  if (this.root === null) {
    this.root = newNode;
    this.root.color = 'black';
    return;
  }
  var current = this.root;
  var parent = null;
  while (current) {
    parent = current;
    if (key < current.key) {
      current = current.left;
    } else {
      current = current.right;
    }
  }
  if (key < parent.key) {
    parent.left = newNode;
  } else {
    parent.right = newNode;
  }
  this.fixInsert(newNode);
};
RedBlackTree.prototype.fixInsert = function(node) {
  var parent = null;
  var grandParent = null;
  while (node !== this.root && node.color !== 'black' && node.parent.color === 'red') {
    parent = node.parent;
    grandParent = node.parent.parent;
    if (parent === grandParent.left) {
      var uncle = grandParent.right;
      if (uncle && uncle.color === 'red') {
        grandParent.color = 'red';
        parent.color = 'black';
        uncle.color = 'black';
        node = grandParent;
      } else {
        if (node === parent.right) {
          this.rotateLeft(parent);
          node = parent;
          parent = node.parent;
        }
        this.rotateRight(grandParent);
        parent.color = 'black';
        grandParent.color = 'red';
        node = parent;
      }
    } else {
      var uncle = grandParent.left;
      if (uncle && uncle.color === 'red') {
        grandParent.color = 'red';
        parent.color = 'black';
        uncle.color = 'black';
        node = grandParent;
      } else {
        if (node === parent.left) {
          this.rotateRight(parent);
          node = parent;
          parent = node.parent;
        }
        this.rotateLeft(grandParent);
        parent.color = 'black';
        grandParent.color = 'red';
        node = parent;
      }
    }
  }
  this.root.color = 'black';
};
RedBlackTree.prototype.rotateLeft = function(node) {
  var right = node.right;
  node.right = right.left;
  if (right.left) {
    right.left.parent = node;
  }
  right.parent = node.parent;
  if (node.parent === null) {
    this.root = right;
  } else if (node === node.parent.left) {
    node.parent.left = right;
  } else {
    node.parent.right = right;
  }
  right.left = node;
  node.parent = right;
};
RedBlackTree.prototype.rotateRight = function(node) {
  var left = node.left;
  node.left = left.right;
  if (left.right) {
    left.right.parent = node;
  }
  left.parent = node.parent;
  if (node.parent === null) {
    this.root = left;
  } else if (node === node.parent.left) {
    node.parent.left = left;
  } else {
    node.parent.right = left;
  }
  left.right = node;
  node.parent = left;
};
RedBlackTree.prototype.search = function(key) {
  var current = this.root;
  while (current) {
    if (key === current.key) {
     
请登录后发表评论

    没有回复内容