diff --git a/POSTS_zh-CN.md b/POSTS_zh-CN.md new file mode 100644 index 0000000..94f0917 --- /dev/null +++ b/POSTS_zh-CN.md @@ -0,0 +1,54 @@ +Java有java.utils,C++有STL,那JavaScript和TypeScript长期以来没有一个顺手的数据结构和算法库 + +data-structure-typed就应运而生 + +## 性能 + +### 部分超越JavaScript内置的数据结构 + +### 部分超越C++ + + +## 顺手程度 + +### 亲密拥抱JavaScript,TypeScript和ES6 + +#### 提供与JavaScript中Array完全一致的API体验 + +#### 提供生成器 + + +### 基类中统一实现通用方法 + +### 完整的OOP封装 + +#### 可复用性 + +#### 可扩展性 + +为开发者提供继承所有数据结构的可能 + +### 摒弃以往数据结构库中同时存在Set和Map + +#### 原滋原味的基础数据结构 + +不必分别记忆Set和Map的API + + +## 内置算法 + +## 可视化 + +### print方法 + +### 可视化工具 + +#### 数据结构可视化 + +#### 步进式调试 + +## 多种模块系统的支持 + + + + diff --git a/src/data-structures/binary-tree/bst.ts b/src/data-structures/binary-tree/bst.ts index 1368f4f..ea72934 100644 --- a/src/data-structures/binary-tree/bst.ts +++ b/src/data-structures/binary-tree/bst.ts @@ -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; } diff --git a/src/data-structures/index.ts b/src/data-structures/index.ts index 60d00e4..ebd4555 100644 --- a/src/data-structures/index.ts +++ b/src/data-structures/index.ts @@ -9,3 +9,4 @@ export * from './priority-queue'; export * from './matrix'; export * from './trie'; export * from './base'; +export * from './tree'; diff --git a/src/data-structures/tree/index.ts b/src/data-structures/tree/index.ts new file mode 100644 index 0000000..50842b5 --- /dev/null +++ b/src/data-structures/tree/index.ts @@ -0,0 +1 @@ +export * from './tree'; diff --git a/src/data-structures/tree/tree.ts b/src/data-structures/tree/tree.ts new file mode 100644 index 0000000..c0e85e5 --- /dev/null +++ b/src/data-structures/tree/tree.ts @@ -0,0 +1,115 @@ +export class TreeNode { + /** + * 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[]} [children] - The `children` parameter is an optional array of `TreeNode` + * 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[]) { + 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[] | undefined; + + /** + * The function returns an array of TreeNode objects or undefined. + * @returns The `children` property is being returned. It is of type `TreeNode[] | undefined`, + * which means it can either be an array of `TreeNode` objects or `undefined`. + */ + get children(): TreeNode[] | undefined { + return this._children; + } + + /** + * The function sets the value of the children property of a TreeNode object. + * @param {TreeNode[] | undefined} value - The value parameter is of type TreeNode[] | + * undefined. This means that it can accept an array of TreeNode objects or undefined. + */ + set children(value: TreeNode[] | undefined) { + this._children = value; + } + + /** + * The function `addChildren` adds one or more child nodes to the current node. + * @param {TreeNode | TreeNode[]} children - The `children` parameter can be either a single + * `TreeNode` object or an array of `TreeNode` objects. + */ + addChildren(children: TreeNode | TreeNode[]) { + 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, 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; + } +} diff --git a/src/utils/index.ts b/src/utils/index.ts index 04bca77..01fe12a 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -1 +1,2 @@ export * from './utils'; +export * from './number'; diff --git a/src/utils/number.ts b/src/utils/number.ts new file mode 100644 index 0000000..11a4793 --- /dev/null +++ b/src/utils/number.ts @@ -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; +}