Skip to content

Commit 0bf5388

Browse files
committed
V1.5.0 Finish RedBlack.
1 parent 9268f53 commit 0bf5388

File tree

10 files changed

+33
-667
lines changed

10 files changed

+33
-667
lines changed

docs/bundle.js

Lines changed: 6 additions & 627 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/components/binnode.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
computed: {
5151
title() {
5252
if (this.$parent.curTreeType === "RedBlack")
53-
return `blackH: ${this.node.blackH + 1}\nsize: ${this.node.size()}`
53+
return `blackH: ${this.node.height + 1}\nsize: ${this.node.size()}`
5454
return `height: ${this.node.height}\nsize: ${this.node.size()}`
5555
// return `npl:${this.node.npl}\nsize: ${this.node.size()}`
5656
},

src/components/top-binnode.vue

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@
7474
}, computed: {
7575
showTopSearch() { return this.$parent.curTreeType !== "BinTree"; }, showTopInsert() {
7676
return this.$parent.curTreeType !== "BinTree";
77-
}, showTopBuild() { return true; }
77+
},
78+
showTopBuild() { return this.$parent.curTreeType !== "RedBlack"; }
7879
},
7980
watch: {
8081
data() { this.sequence = this.data.toString(); }

src/js/BinNode.js

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,13 @@ export class TreeUtil {
3737
static isRed(x) {
3838
return !this.isBlack(x);
3939
}
40-
static statureB(x) {
41-
if (x === null)
42-
return -1;
43-
else
44-
return x.blackH;
45-
}
4640
static isBlackUpdated(x) {
47-
return this.statureB(x.lc) == this.statureB(x.rc) &&
48-
this.statureB(x) == (this.isBlack(x) ? this.statureB(x.lc) + 1 : this.statureB(x.lc));
41+
return this.stature(x.lc) == this.stature(x.rc) &&
42+
this.stature(x) == (this.isBlack(x) ? this.stature(x.lc) + 1 : this.stature(x.lc));
4943
}
5044
}
5145
export class BinNode {
52-
constructor(e = null, p = null, lc = null, rc = null, height = 0, blackH = -1, npl = 0, c = RBColor.Red) {
46+
constructor(e = null, p = null, lc = null, rc = null, height = 0, npl = 0, c = RBColor.Red) {
5347
this.x = 0;
5448
this.y = 0;
5549
this.active = false;
@@ -59,7 +53,6 @@ export class BinNode {
5953
this.lc = lc;
6054
this.rc = rc;
6155
this.height = height;
62-
this.blackH = blackH;
6356
this.npl = npl;
6457
this.color = c;
6558
this.nid = ++BinNode.N;

src/js/BinTree.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ export class BinTree {
1111
this._size = 1;
1212
this._root = new BinNode(e);
1313
this._root.color = RBColor.Black;
14-
this._root.blackH = 0;
1514
}
1615
}
1716
updateHeight(x) {
@@ -188,10 +187,12 @@ export class BinTree {
188187
let curLevel = levels[i];
189188
for (let j = 0; j < curLevel.length; j++) {
190189
curLevel[j].x -= deltaX;
191-
// if (curLevel[j].lc !== undefined) { this.updateHeight(curLevel[j]); }
190+
if (curLevel[j].lc !== undefined) {
191+
this.updateHeight(curLevel[j]);
192+
}
192193
}
193194
}
194-
// this.updateHeight(this._root);
195+
this.updateHeight(this._root);
195196
// 添加内部边和外部边
196197
for (let i = levels.length - 1; i >= 1; i--) {
197198
let curLevel = levels[i];
@@ -217,7 +218,7 @@ export class BinTree {
217218
}
218219
return structInfo;
219220
}
220-
// Build tree from JSON object retracted from LocalStorage
221+
// Build tree from JSON object retracted from LocalStorage. ! Caution: Can not update Black Height!
221222
static buildFromTreeJsonObj(treeObj) {
222223
if (treeObj._root === null)
223224
return new this();

src/js/RedBlack.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { BST } from "./BST";
22
import { RBColor, TreeUtil, BinNode } from "./BinNode";
3-
let statureB = TreeUtil.statureB;
3+
let stature = TreeUtil.stature;
44
export class RedBlack extends BST {
55
solveDoubleRed(x) {
66
if (TreeUtil.isRoot(x)) {
@@ -74,15 +74,15 @@ export class RedBlack extends BST {
7474
}
7575
}
7676
updateHeight(x) {
77-
x.blackH = statureB(x.lc) > statureB(x.rc) ? statureB(x.lc) : statureB(x.rc);
77+
x.height = stature(x.lc) > stature(x.rc) ? stature(x.lc) : stature(x.rc);
7878
if (TreeUtil.isBlack(x))
79-
x.blackH++;
79+
x.height++;
8080
}
8181
insert(e) {
8282
let x = this.search(e);
8383
if (x)
8484
return x;
85-
x = new BinNode(e, this._hot);
85+
x = new BinNode(e, this._hot, null, null, -1);
8686
if (!this._root)
8787
this._root = x;
8888
else

src/ts/BinNode.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,9 @@ export class TreeUtil<T> {
3636
return !this.isBlack(x);
3737
}
3838

39-
static statureB<T>(x: BinNode<T>): number {
40-
if (x === null) return -1;
41-
else return x.blackH;
42-
}
43-
4439
static isBlackUpdated<T>(x: BinNode<T>): boolean {
45-
return this.statureB(x.lc) == this.statureB(x.rc) &&
46-
this.statureB(x) == (this.isBlack(x) ? this.statureB(x.lc) + 1 : this.statureB(x.lc));
40+
return this.stature(x.lc) == this.stature(x.rc) &&
41+
this.stature(x) == (this.isBlack(x) ? this.stature(x.lc) + 1 : this.stature(x.lc));
4742
}
4843
}
4944

@@ -53,7 +48,6 @@ export class BinNode<T> {
5348
lc: BinNode<T>;
5449
rc: BinNode<T>;
5550
height: number;
56-
blackH: number;
5751
npl: number;
5852
color: RBColor;
5953
nid: number;
@@ -65,13 +59,12 @@ export class BinNode<T> {
6559
static N: number = 0;
6660

6761
constructor(e: T = null, p: BinNode<T> = null, lc: BinNode<T> = null, rc: BinNode<T> = null,
68-
height: number = 0, blackH: number = -1, npl: number = 0, c: RBColor = RBColor.Red) {
62+
height: number = 0, npl: number = 0, c: RBColor = RBColor.Red) {
6963
this.data = e;
7064
this.parent = p;
7165
this.lc = lc;
7266
this.rc = rc;
7367
this.height = height;
74-
this.blackH = blackH;
7568
this.npl = npl;
7669
this.color = c;
7770
this.nid = ++BinNode.N;

src/ts/BinTree.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ export class BinTree<T> {
3838
this._size = 1;
3939
this._root = new BinNode<T>(e);
4040
this._root.color = RBColor.Black;
41-
this._root.blackH = 0;
4241
}
4342
}
4443

@@ -212,10 +211,10 @@ export class BinTree<T> {
212211
let curLevel = levels[i];
213212
for (let j: number = 0; j < curLevel.length; j++) {
214213
curLevel[j].x -= deltaX;
215-
// if (curLevel[j].lc !== undefined) { this.updateHeight(curLevel[j]); }
214+
if (curLevel[j].lc !== undefined) { this.updateHeight(curLevel[j]); }
216215
}
217216
}
218-
// this.updateHeight(this._root);
217+
this.updateHeight(this._root);
219218

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

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

src/ts/RedBlack.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { BST } from "./BST";
22
import { RBColor, TreeUtil, BinNode } from "./BinNode"
33

4-
let statureB: (BinNode) => number = TreeUtil.statureB;
4+
let stature: (BinNode) => number = TreeUtil.stature;
55

66
export class RedBlack<T> extends BST<T> {
77
protected solveDoubleRed(x: BinNode<T>): void {
@@ -70,15 +70,15 @@ export class RedBlack<T> extends BST<T> {
7070
}
7171

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

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

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

webpack.config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ module.exports = {
77
filename: "bundle.js"
88
},
99
watch: true,
10-
mode: "development",
11-
// mode: "production",
10+
// mode: "development",
11+
mode: "production",
1212
plugins: [
1313
new VueLoaderPlugin(),
1414
],

0 commit comments

Comments
 (0)