Skip to content

Commit

Permalink
V1.5.0 Finish RedBlack.
Browse files Browse the repository at this point in the history
  • Loading branch information
hwc0919 committed Mar 11, 2020
1 parent 9268f53 commit 0bf5388
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 667 deletions.
633 changes: 6 additions & 627 deletions docs/bundle.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/components/binnode.vue
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
computed: {
title() {
if (this.$parent.curTreeType === "RedBlack")
return `blackH: ${this.node.blackH + 1}\nsize: ${this.node.size()}`
return `blackH: ${this.node.height + 1}\nsize: ${this.node.size()}`
return `height: ${this.node.height}\nsize: ${this.node.size()}`
// return `npl:${this.node.npl}\nsize: ${this.node.size()}`
},
Expand Down
3 changes: 2 additions & 1 deletion src/components/top-binnode.vue
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@
}, computed: {
showTopSearch() { return this.$parent.curTreeType !== "BinTree"; }, showTopInsert() {
return this.$parent.curTreeType !== "BinTree";
}, showTopBuild() { return true; }
},
showTopBuild() { return this.$parent.curTreeType !== "RedBlack"; }
},
watch: {
data() { this.sequence = this.data.toString(); }
Expand Down
13 changes: 3 additions & 10 deletions src/js/BinNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,13 @@ export class TreeUtil {
static isRed(x) {
return !this.isBlack(x);
}
static statureB(x) {
if (x === null)
return -1;
else
return x.blackH;
}
static isBlackUpdated(x) {
return this.statureB(x.lc) == this.statureB(x.rc) &&
this.statureB(x) == (this.isBlack(x) ? this.statureB(x.lc) + 1 : this.statureB(x.lc));
return this.stature(x.lc) == this.stature(x.rc) &&
this.stature(x) == (this.isBlack(x) ? this.stature(x.lc) + 1 : this.stature(x.lc));
}
}
export class BinNode {
constructor(e = null, p = null, lc = null, rc = null, height = 0, blackH = -1, npl = 0, c = RBColor.Red) {
constructor(e = null, p = null, lc = null, rc = null, height = 0, npl = 0, c = RBColor.Red) {
this.x = 0;
this.y = 0;
this.active = false;
Expand All @@ -59,7 +53,6 @@ export class BinNode {
this.lc = lc;
this.rc = rc;
this.height = height;
this.blackH = blackH;
this.npl = npl;
this.color = c;
this.nid = ++BinNode.N;
Expand Down
9 changes: 5 additions & 4 deletions src/js/BinTree.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ export class BinTree {
this._size = 1;
this._root = new BinNode(e);
this._root.color = RBColor.Black;
this._root.blackH = 0;
}
}
updateHeight(x) {
Expand Down Expand Up @@ -188,10 +187,12 @@ export class BinTree {
let curLevel = levels[i];
for (let j = 0; j < curLevel.length; j++) {
curLevel[j].x -= deltaX;
// if (curLevel[j].lc !== undefined) { this.updateHeight(curLevel[j]); }
if (curLevel[j].lc !== undefined) {
this.updateHeight(curLevel[j]);
}
}
}
// this.updateHeight(this._root);
this.updateHeight(this._root);
// 添加内部边和外部边
for (let i = levels.length - 1; i >= 1; i--) {
let curLevel = levels[i];
Expand All @@ -217,7 +218,7 @@ export class BinTree {
}
return structInfo;
}
// Build tree from JSON object retracted from LocalStorage
// Build tree from JSON object retracted from LocalStorage. ! Caution: Can not update Black Height!
static buildFromTreeJsonObj(treeObj) {
if (treeObj._root === null)
return new this();
Expand Down
8 changes: 4 additions & 4 deletions src/js/RedBlack.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { BST } from "./BST";
import { RBColor, TreeUtil, BinNode } from "./BinNode";
let statureB = TreeUtil.statureB;
let stature = TreeUtil.stature;
export class RedBlack extends BST {
solveDoubleRed(x) {
if (TreeUtil.isRoot(x)) {
Expand Down Expand Up @@ -74,15 +74,15 @@ export class RedBlack extends BST {
}
}
updateHeight(x) {
x.blackH = statureB(x.lc) > statureB(x.rc) ? statureB(x.lc) : statureB(x.rc);
x.height = stature(x.lc) > stature(x.rc) ? stature(x.lc) : stature(x.rc);
if (TreeUtil.isBlack(x))
x.blackH++;
x.height++;
}
insert(e) {
let x = this.search(e);
if (x)
return x;
x = new BinNode(e, this._hot);
x = new BinNode(e, this._hot, null, null, -1);
if (!this._root)
this._root = x;
else
Expand Down
13 changes: 3 additions & 10 deletions src/ts/BinNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,9 @@ export class TreeUtil<T> {
return !this.isBlack(x);
}

static statureB<T>(x: BinNode<T>): number {
if (x === null) return -1;
else return x.blackH;
}

static isBlackUpdated<T>(x: BinNode<T>): boolean {
return this.statureB(x.lc) == this.statureB(x.rc) &&
this.statureB(x) == (this.isBlack(x) ? this.statureB(x.lc) + 1 : this.statureB(x.lc));
return this.stature(x.lc) == this.stature(x.rc) &&
this.stature(x) == (this.isBlack(x) ? this.stature(x.lc) + 1 : this.stature(x.lc));
}
}

Expand All @@ -53,7 +48,6 @@ export class BinNode<T> {
lc: BinNode<T>;
rc: BinNode<T>;
height: number;
blackH: number;
npl: number;
color: RBColor;
nid: number;
Expand All @@ -65,13 +59,12 @@ export class BinNode<T> {
static N: number = 0;

constructor(e: T = null, p: BinNode<T> = null, lc: BinNode<T> = null, rc: BinNode<T> = null,
height: number = 0, blackH: number = -1, npl: number = 0, c: RBColor = RBColor.Red) {
height: number = 0, npl: number = 0, c: RBColor = RBColor.Red) {
this.data = e;
this.parent = p;
this.lc = lc;
this.rc = rc;
this.height = height;
this.blackH = blackH;
this.npl = npl;
this.color = c;
this.nid = ++BinNode.N;
Expand Down
7 changes: 3 additions & 4 deletions src/ts/BinTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ export class BinTree<T> {
this._size = 1;
this._root = new BinNode<T>(e);
this._root.color = RBColor.Black;
this._root.blackH = 0;
}
}

Expand Down Expand Up @@ -212,10 +211,10 @@ export class BinTree<T> {
let curLevel = levels[i];
for (let j: number = 0; j < curLevel.length; j++) {
curLevel[j].x -= deltaX;
// if (curLevel[j].lc !== undefined) { this.updateHeight(curLevel[j]); }
if (curLevel[j].lc !== undefined) { this.updateHeight(curLevel[j]); }
}
}
// this.updateHeight(this._root);
this.updateHeight(this._root);

// 添加内部边和外部边
for (let i: number = levels.length - 1; i >= 1; i--) {
Expand All @@ -237,7 +236,7 @@ export class BinTree<T> {
return structInfo;
}

// Build tree from JSON object retracted from LocalStorage
// Build tree from JSON object retracted from LocalStorage. ! Caution: Can not update Black Height!
static buildFromTreeJsonObj<T>(treeObj: ITreeJsonObj<T>): BinTree<T> {
if (treeObj._root === null) return new this();

Expand Down
8 changes: 4 additions & 4 deletions src/ts/RedBlack.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { BST } from "./BST";
import { RBColor, TreeUtil, BinNode } from "./BinNode"

let statureB: (BinNode) => number = TreeUtil.statureB;
let stature: (BinNode) => number = TreeUtil.stature;

export class RedBlack<T> extends BST<T> {
protected solveDoubleRed(x: BinNode<T>): void {
Expand Down Expand Up @@ -70,15 +70,15 @@ export class RedBlack<T> extends BST<T> {
}

protected updateHeight(x: BinNode<T>): void {
x.blackH = statureB(x.lc) > statureB(x.rc) ? statureB(x.lc) : statureB(x.rc);
if (TreeUtil.isBlack(x)) x.blackH++;
x.height = stature(x.lc) > stature(x.rc) ? stature(x.lc) : stature(x.rc);
if (TreeUtil.isBlack(x)) x.height++;
}

public insert(e: T): BinNode<T> {
let x: BinNode<T> = this.search(e);
if (x) return x;

x = new BinNode<T>(e, this._hot);
x = new BinNode<T>(e, this._hot, null, null, -1);
if (!this._root) this._root = x;
else e < this._hot.data ? this._hot.lc = x : this._hot.rc = x;

Expand Down
4 changes: 2 additions & 2 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ module.exports = {
filename: "bundle.js"
},
watch: true,
mode: "development",
// mode: "production",
// mode: "development",
mode: "production",
plugins: [
new VueLoaderPlugin(),
],
Expand Down

0 comments on commit 0bf5388

Please sign in to comment.