-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathNode.ts
45 lines (41 loc) · 980 Bytes
/
Node.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/**
* 二叉树的辅助类: 用于存储二叉树的每个节点
*/
export class Node<K> {
public left?: Node<K> | undefined;
public right?: Node<K> | undefined;
public parent?: Node<K> | undefined;
constructor(public key: K, parent?: Node<K>) {
this.left = undefined;
this.right = undefined;
this.parent = parent;
}
toString(): string {
return `${this.key}`;
}
}
export enum Colors {
RED = 0,
BLACK = 1
}
/**
* 红黑树辅助节点
* 1. 添加parent和color节点
* 2. 节点的默认颜色为红色
*/
export class RedBlackNode<K> extends Node<K> {
public left: RedBlackNode<K> | undefined;
public right: RedBlackNode<K> | undefined;
public parent: RedBlackNode<K> | undefined;
public color: number;
constructor(public key: K) {
super(key);
this.left = undefined;
this.right = undefined;
this.parent = undefined;
this.color = Colors.RED;
}
isRed() {
return this.color === Colors.RED;
}
}