feat: Restore the TreeNode data structure. Add number utilities to the utils module. docs: Update POSTS_zh-CN.md.

This commit is contained in:
Revone 2024-01-17 22:27:38 +08:00
parent 25e9f36577
commit 18b1fab16d
7 changed files with 182 additions and 1 deletions

54
POSTS_zh-CN.md Normal file
View file

@ -0,0 +1,54 @@
Java有java.utilsC++有STL那JavaScript和TypeScript长期以来没有一个顺手的数据结构和算法库
data-structure-typed就应运而生
## 性能
### 部分超越JavaScript内置的数据结构
### 部分超越C++
## 顺手程度
### 亲密拥抱JavaScriptTypeScript和ES6
#### 提供与JavaScript中Array完全一致的API体验
#### 提供生成器
### 基类中统一实现通用方法
### 完整的OOP封装
#### 可复用性
#### 可扩展性
为开发者提供继承所有数据结构的可能
### 摒弃以往数据结构库中同时存在Set和Map
#### 原滋原味的基础数据结构
不必分别记忆Set和Map的API
## 内置算法
## 可视化
### print方法
### 可视化工具
#### 数据结构可视化
#### 步进式调试
## 多种模块系统的支持

View file

@ -695,7 +695,7 @@ export class BST<
if (!current) return undefined;
if (this._variant === 'STANDARD') {
// For BSTVariant.MIN, find the rightmost node
// For 'STANDARD', find the rightmost node
while (current.right !== undefined) {
current = current.right;
}

View file

@ -9,3 +9,4 @@ export * from './priority-queue';
export * from './matrix';
export * from './trie';
export * from './base';
export * from './tree';

View file

@ -0,0 +1 @@
export * from './tree';

View file

@ -0,0 +1,115 @@
export class TreeNode<V = any> {
/**
* The constructor function initializes a TreeNode object with a key, optional value, and optional
* children.
* @param {string} key - A string representing the key of the tree node.
* @param {V} [value] - The `value` parameter is an optional parameter of type `V`. It represents the
* value associated with the node. If no value is provided, it defaults to `undefined`.
* @param {TreeNode<V>[]} [children] - The `children` parameter is an optional array of `TreeNode<V>`
* objects. It represents the child nodes of the current node. If no children are provided, the
* default value is an empty array.
*/
constructor(key: string, value?: V, children?: TreeNode<V>[]) {
this._key = key;
this._value = value || undefined;
this._children = children || [];
}
protected _key: string;
/**
* The function returns the value of the protected variable _key.
* @returns The value of the `_key` property, which is a string.
*/
get key(): string {
return this._key;
}
/**
* The above function sets the value of a protected variable called "key".
* @param {string} value - The value parameter is a string that represents the value to be assigned
* to the key.
*/
set key(value: string) {
this._key = value;
}
protected _value?: V | undefined;
/**
* The function returns the value stored in a variable, or undefined if the variable is empty.
* @returns The value of the variable `_value` is being returned.
*/
get value(): V | undefined {
return this._value;
}
/**
* The function sets the value of a variable.
* @param {V | undefined} value - The parameter "value" is of type "V | undefined", which means it
* can accept a value of type "V" or it can be undefined.
*/
set value(value: V | undefined) {
this._value = value;
}
protected _children?: TreeNode<V>[] | undefined;
/**
* The function returns an array of TreeNode objects or undefined.
* @returns The `children` property is being returned. It is of type `TreeNode<V>[] | undefined`,
* which means it can either be an array of `TreeNode<V>` objects or `undefined`.
*/
get children(): TreeNode<V>[] | undefined {
return this._children;
}
/**
* The function sets the value of the children property of a TreeNode object.
* @param {TreeNode<V>[] | undefined} value - The value parameter is of type TreeNode<V>[] |
* undefined. This means that it can accept an array of TreeNode objects or undefined.
*/
set children(value: TreeNode<V>[] | undefined) {
this._children = value;
}
/**
* The function `addChildren` adds one or more child nodes to the current node.
* @param {TreeNode<V> | TreeNode<V>[]} children - The `children` parameter can be either a single
* `TreeNode<V>` object or an array of `TreeNode<V>` objects.
*/
addChildren(children: TreeNode<V> | TreeNode<V>[]) {
if (!this._children) {
this._children = [];
}
if (children instanceof TreeNode) {
this._children.push(children);
} else {
this._children = this._children.concat(children);
}
}
/**
* The function `getHeight()` calculates the maximum depth of a tree structure by performing a
* breadth-first search.
* @returns the maximum depth or height of the tree.
*/
getHeight() {
let maxDepth = 0;
if (this) {
const bfs = (node: TreeNode<V>, level: number) => {
if (level > maxDepth) {
maxDepth = level;
}
const { _children } = node;
if (_children) {
for (let i = 0, len = _children.length; i < len; i++) {
bfs(_children[i], level + 1);
}
}
};
bfs(this, 0);
}
return maxDepth;
}
}

View file

@ -1 +1,2 @@
export * from './utils';
export * from './number';

9
src/utils/number.ts Normal file
View file

@ -0,0 +1,9 @@
export function toBinaryString(num: number, digit = 32) {
// Convert number to binary string
let binaryString = (num >>> 0).toString(2); // Use the unsigned right shift operator to ensure you get a binary representation of a 32-bit unsigned integer
// Use pad Start to ensure the string length is 32 bits
binaryString = binaryString.padStart(digit, '0');
return binaryString;
}