Let us insert several numbers in an empty red-black tree, using two different implementations.
Tree.prototype.balance2 = function() {
if(this.color == black && this.left != null && this.left.left != null &&
this.left.color == red && this.left.left.color == red)
return new Tree(red, new Tree(black, this.left.left.left, this.left.left.value,
this.left.left.right),
this.left.value,
new Tree(black, this.left.right, this.value, this.right));
if(this.color == black && this.left != null && this.left.right != null &&
this.left.color == red && this.left.right.color == red)
return new Tree(red, new Tree(black, this.left.left, this.left.value,
this.left.right.left),
this.left.right.value,
new Tree(black, this.left.right.right, this.value, this.right));
if(this.color == black && this.right != null && this.right.left != null &&
this.right.color == red && this.right.left.color == red)
return new Tree(red, new Tree(black, this.left, this.value, this.right.left.left),
this.right.left.value,
new Tree(black, this.right.left.right, this.right.value,
this.right.right));
if(this.color == black && this.right != null && this.right.right != null &&
this.right.color == red && this.right.right.color == red)
return new Tree(red, new Tree(black, this.left, this.value, this.right.left),
this.right.value,
new Tree(black, this.right.right.left, this.right.right.value,
this.right.right.right));
return this;
};
Tree.prototype.balance = function() {
var s = matchAny(this, ["[((%a %x %b) %y %c) %z %d]",
"[(%a %x (%b %y %c)) %z %d]",
"[%a %x ((%b %y %c) %z %d)]",
"[%a %x (%b %y (%c %z %d))]"]);
if(s)
return new Tree(red, new Tree(black, s.a, s.x, s.b),
s.y, new Tree(black, s.c, s.z, s.d));
return this;
};
named = false;
Tree.prototype.um = [matcher(Tree, "[((% % %) % %) % %]"),
matcher(Tree, "[(% % (% % %)) % %]"),
matcher(Tree, "[% % ((% % %) % %)]"),
matcher(Tree, "[% % (% % (% % %))]")];
Tree.prototype.ubalance3 = function() {
s = Tree.prototype.um[0](this) || Tree.prototype.um[1](this) || Tree.prototype.um[2](this) || Tree.prototype.um[3](this);
if(!s) return this;
return new Tree(red, new Tree(black, s[0], s[1], s[2]),
s[3], new Tree(black, s[4], s[5], s[6]));
};
Example:
Therefore, everyone using regular expressions in JavaScript may also consider using myPatterns/JS.