diff --git a/README.md b/README.md index c51abe1..f427040 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,14 @@ # data-structure-typed -![npm](https://img.shields.io/npm/v/data-structure-typed) + ![npm](https://img.shields.io/npm/dm/data-structure-typed) +![GitHub contributors](https://img.shields.io/github/contributors/zrwusa/data-structure-typed) ![npm package minimized gzipped size (select exports)](https://img.shields.io/bundlejs/size/data-structure-typed) ![GitHub top language](https://img.shields.io/github/languages/top/zrwusa/data-structure-typed) ![GITHUB Star](https://img.shields.io/github/stars/zrwusa/data-structure-typed) ![eslint](https://aleen42.github.io/badges/src/eslint.svg) ![NPM](https://img.shields.io/npm/l/data-structure-typed) +![npm](https://img.shields.io/npm/v/data-structure-typed) [//]: # (![npm bundle size](https://img.shields.io/bundlephobia/min/data-structure-typed)) @@ -939,43 +941,43 @@ We strictly adhere to computer science theory and software development standards [//]: # (No deletion!!! Start of Replace Section)
avl-tree
-
test nametime taken (ms)executions per secsample deviation
10,000 add randomly119.608.360.00
10,000 add & delete randomly178.175.610.00
10,000 addMany129.037.757.48e-4
10,000 get48.7920.493.13e-4
+
test nametime taken (ms)executions per secsample deviation
10,000 add randomly125.607.960.00
10,000 add & delete randomly181.225.520.00
10,000 addMany134.127.460.01
10,000 get55.0818.160.01
binary-tree-overall
-
test nametime taken (ms)executions per secsample deviation
10,000 RBTree add5.80172.507.88e-5
10,000 RBTree add & delete randomly16.3361.240.00
10,000 RBTree get20.9547.740.00
10,000 AVLTree add131.917.580.01
10,000 AVLTree add & delete randomly202.754.930.04
10,000 AVLTree get1.02984.652.43e-4
+
test nametime taken (ms)executions per secsample deviation
10,000 RBTree add6.17161.950.00
10,000 RBTree add & delete randomly16.0762.222.62e-4
10,000 RBTree get19.8650.362.44e-4
10,000 AVLTree add134.387.440.02
10,000 AVLTree add & delete randomly207.204.830.06
10,000 AVLTree get0.981015.542.73e-5
rb-tree
-
test nametime taken (ms)executions per secsample deviation
100,000 add88.5711.290.01
100,000 add & delete randomly266.593.750.06
100,000 getNode201.814.960.03
100,000 add & iterator116.388.590.02
+
test nametime taken (ms)executions per secsample deviation
100,000 add86.6511.540.02
100,000 add & delete randomly221.024.520.03
100,000 getNode190.545.250.00
100,000 add & iterator122.108.190.01
directed-graph
-
test nametime taken (ms)executions per secsample deviation
1,000 addVertex0.109751.731.85e-6
1,000 addEdge6.08164.611.04e-4
1,000 getVertex0.052.17e+43.55e-7
1,000 getEdge25.9538.530.01
tarjan228.154.380.01
topologicalSort187.155.340.00
+
test nametime taken (ms)executions per secsample deviation
1,000 addVertex0.118896.512.63e-5
1,000 addEdge6.53153.210.00
1,000 getVertex0.052.08e+41.06e-5
1,000 getEdge27.5336.330.01
tarjan224.534.450.01
topologicalSort184.025.430.00
hash-map
-
test nametime taken (ms)executions per secsample deviation
1,000,000 set117.958.480.04
Native Map 1,000,000 set217.094.610.03
Native Set 1,000,000 add168.565.930.02
1,000,000 set & get121.338.240.03
Native Map 1,000,000 set & get268.813.720.02
Native Set 1,000,000 add & has172.465.800.01
1,000,000 ObjKey set & get411.492.430.09
Native Map 1,000,000 ObjKey set & get335.402.980.07
Native Set 1,000,000 ObjKey add & has287.113.480.06
+
test nametime taken (ms)executions per secsample deviation
1,000,000 set126.277.920.05
Native Map 1,000,000 set229.804.350.03
Native Set 1,000,000 add175.835.690.01
1,000,000 set & get121.348.240.03
Native Map 1,000,000 set & get290.803.440.03
Native Set 1,000,000 add & has180.715.530.01
1,000,000 ObjKey set & get357.682.800.07
Native Map 1,000,000 ObjKey set & get310.573.220.06
Native Set 1,000,000 ObjKey add & has278.423.590.05
heap
-
test nametime taken (ms)executions per secsample deviation
100,000 add & poll23.7742.072.92e-4
100,000 add & dfs36.9427.070.01
10,000 fib add & pop374.402.670.04
+
test nametime taken (ms)executions per secsample deviation
100,000 add & poll24.8540.240.00
100,000 add & dfs33.1430.170.00
10,000 fib add & pop366.112.730.00
doubly-linked-list
-
test nametime taken (ms)executions per secsample deviation
1,000,000 push235.154.250.05
1,000,000 unshift221.594.510.08
1,000,000 unshift & shift172.115.810.02
1,000,000 addBefore322.823.100.04
+
test nametime taken (ms)executions per secsample deviation
1,000,000 push217.984.590.07
1,000,000 unshift223.204.480.08
1,000,000 unshift & shift172.875.780.03
1,000,000 addBefore387.132.580.20
singly-linked-list
-
test nametime taken (ms)executions per secsample deviation
1,000,000 push & shift212.644.700.07
10,000 push & pop221.214.520.01
10,000 addBefore251.813.970.01
+
test nametime taken (ms)executions per secsample deviation
1,000,000 push & shift225.134.440.07
10,000 push & pop234.544.260.02
10,000 addBefore252.623.960.00
priority-queue
-
test nametime taken (ms)executions per secsample deviation
100,000 add & poll75.0013.339.50e-4
+
test nametime taken (ms)executions per secsample deviation
100,000 add & poll76.4913.070.00
deque
-
test nametime taken (ms)executions per secsample deviation
1,000,000 push13.2675.430.00
1,000,000 push & pop21.2447.081.57e-4
100,000 push & shift2.20453.655.13e-4
Native Array 100,000 push & shift2165.420.460.19
100,000 unshift & shift2.19455.624.59e-4
Native Array 100,000 unshift & shift4298.710.230.13
+
test nametime taken (ms)executions per secsample deviation
1,000,000 push13.2075.752.79e-4
1,000,000 push & pop22.2145.033.27e-4
100,000 push & shift2.26442.241.43e-4
Native Array 100,000 push & shift2329.510.430.10
100,000 unshift & shift2.16463.838.20e-5
Native Array 100,000 unshift & shift4590.640.220.33
queue
-
test nametime taken (ms)executions per secsample deviation
1,000,000 push46.4421.530.01
100,000 push & shift5.00199.871.37e-4
Native Array 100,000 push & shift2276.160.440.12
Native Array 100,000 push & pop4.33230.721.58e-4
+
test nametime taken (ms)executions per secsample deviation
1,000,000 push49.9220.030.02
100,000 push & shift5.07197.285.86e-4
Native Array 100,000 push & shift2315.780.430.13
Native Array 100,000 push & pop4.37228.721.32e-4
stack
-
test nametime taken (ms)executions per secsample deviation
1,000,000 push47.4321.080.02
1,000,000 push & pop50.6419.750.01
+
test nametime taken (ms)executions per secsample deviation
1,000,000 push44.5022.470.01
1,000,000 push & pop53.5718.670.02
trie
-
test nametime taken (ms)executions per secsample deviation
100,000 push47.8320.910.00
100,000 getWords100.679.930.01
+
test nametime taken (ms)executions per secsample deviation
100,000 push42.9523.286.68e-4
100,000 getWords92.1110.860.01
[//]: # (No deletion!!! End of Replace Section) diff --git a/SPECIFICATION.md b/SPECIFICATION.md new file mode 100644 index 0000000..539193e --- /dev/null +++ b/SPECIFICATION.md @@ -0,0 +1,56 @@ + +[NPM](https://www.npmjs.com/package/data-structure-typed) + +[Github](https://github.com/zrwusa/data-structure-typed) + +## Pain Points Addressed + +### Enhancing the Performance of Simulated Data Structures in JS/TS + +- `Queue`: While many resort to using Arrays to simulate Queues and Deques, the time complexity of Array.shift is O(n). We have tackled this challenge by implementing a Queue and Deque with O(1) time complexity for enqueue and dequeue operations. + +- `HashMap`: Opting for a pure HashMap over the built-in Map (technically a LinkedHashMap) can boost algorithmic speed. However, the performance is compromised due to the necessity of considering insertion order. We have independently implemented an optimized HashMap. + +- `Stack`: In JS, simulating a Stack with an Array is acceptable, and its performance is on par with a genuine Stack. + +### Introducing Missing Native Data Structures in JS/TS + +- `Heap / Priority Queue`: Algorithms with O(log n) time complexity have been pivotal in improving efficiency since the dawn of computers. A Heap supports insertion, deletion, and search with O(log n) time complexity, coupled with the ability to obtain the minimum / maximum value in O(1) time. + +- `Red Black Tree`: Developers well-versed in databases, file systems, Linux virtual memory management, and network routing tables often have a nuanced understanding of Red-Black Trees. It stands out as the least operation-intensive among all balanced binary search trees, offering optimal performance balance in CRUD operations. + +- `Linked List`: In scenarios where insertion or deletion of elements with O(1) time complexity is required at a specific index in an ordered collection, JS lacks a provided data structure. Hence, the need for a LinkedList to implement this functionality. + +- `Trie`: Efficient for fast string queries and space-efficient string storage, yet not commonly found in the standard libraries of most programming languages. + +- `Graph`: This data structure is not commonly found in the standard libraries of most languages, making it a non-issue in JS. + +## Advantages + +### Performance: + +- The performance of some ours data structures has surpassed JS's built-in data structures (`Queue`, `Deque`, `HashMap`), while most are comparable to or even surpass those in other languages. Some are still undergoing refinement (`Graph`, `AVL Tree`). + +### Uniformity + +- 1. Implementation or constraint of `forEach`, `filter`, `map`, `every`, `some`, `reduce`, `find`, `has`, `hasValue`, `get`, `print`, `isEmpty`, `clear`, `clone` methods in the base class. +- 2. Use of generators to uniformly implement `[Symbol.iterator]`, `entries`, `keys`, `values`. Delaying iterator execution prevents performance loss and provides control during traversal. +- 3. All deletion methods uniformly use the widely adopted `delete` in ES6, while addition methods uniformly use `add`. Compatibility with some specifications in Java. +- 4. The first parameter for all constructors is data, and the second parameter is configuration, maintaining uniformity. The first parameter accepts any iterable type for seamless conversion between data structures. +- 5. Uniform return types, for example, the `add` method consistently returns a boolean. + +### Convenience and Simplicity of APIs + +- Inspired by ES6, Java, ESNext, TypeScript, Python, featuring methods like `forEach`, `filter`, `map`, `every`, `some`, `reduce`, `find`, `has`, `hasValue` and `get`. + +### Use of Commonly Understood Industry Standard Naming + +- `enqueue`, `dequeue`, `push`, `pop`, `poll`, `addLast`, `addFirst`, `pollFirst`, `pollLast`, `isEmpty`, `clear`, `print`, `clone`. + +### Implementation of Customizable Features Whenever Possible + +- Such as providing callback functions (lambda expressions) for all traversal methods. + +### Comprehensive Documentation + +- The documentation not only explains the purpose of methods but also annotates time and space complexity across the entire series. diff --git a/SPONSOR-zh-CN.md b/SPONSOR-zh-CN.md new file mode 100644 index 0000000..5fbf74c --- /dev/null +++ b/SPONSOR-zh-CN.md @@ -0,0 +1,90 @@ + +# 我是谁 + +我是一个拥有15年工作经验的软件开发工程师,自大学毕业职于不同行业的软件公司其中也包括一些大厂。 + +# 目前现状 + +为了孩子得到正规教育移居到马来西亚,持有10年居留签证,但是这个签证不允许我被马来西亚公司雇佣。 +由于自己酷爱编程,又正逢JavaScript / TypeScript (以下简称`JS/TS`)没有标准库涵盖数据结构,便决定在开源社区自己实现一套数据结构 [data-structure-typed]()。 + +[NPM](https://www.npmjs.com/package/data-structure-typed) + +[Github](https://github.com/zrwusa/data-structure-typed) + +# 项目介绍 + +## 最终目标 + +- 成为JS/TS的标准库的一部分。 + +## 解决了的痛点 + +### 在JS / TS我们只能基于Array和Map来模拟一些数据结构,例如Queue,Deque,Stack,HashMap + +- `Queue`:大家使用Array来模拟Queue和Deque,但是Array.shift方法的时间复杂度是O(n)的。我们实现了入队和出队时间复杂度为O(1)的Queue,Deque。 + +- `HashMap`:如大家希望使用一个纯正的HashMap来加速自己的算法,而不是使用内置的Map(严格来说是LinkedHashMap),由于需要顾及插入顺序所以性能上逊色于纯正的HashMap。我们单独实现了HashMap + +- `Stack`: 在JS中,用Array模拟Stack并没有什么不妥,性能也不弱于真正的Stack。 + +### JS / TS内置数据结构的缺失。 + +- `Heap / Priority Queue`: +不得不提O(log n),这个算法时间复杂度是人类使用计算机以来最受欢迎的提高效率的方式。Heap本身就支持插入、删除、查找时间复杂度均为O(log n), +还顺带附赠一个动态随时以O(1)的时间复杂度获取最值的功能。 + +- `Red Black Tree`: +相信很多了解数据库、文件系统、Linux的虚拟内存管理和网络路由表底层的开发者都对红黑树有一定了解。 +因为它是在所有平衡二叉搜索树中操作消耗量最少的,而且在CRUD这4个操作上性能平衡得最好的实现。 + +- `Linked List`: +例如我们需要在一个有序集合中,如果已经获取到某个元素的索引的情况下,在这个索引处插入或者删除元素,JS没有提供类似数据结构。我们需要LinkedList来实现。 + +- `Trie`: +字符串高速查询,字符串存储高效性方面有奇效,但是大多数编程语言的标准库中不常见。 + +- `Graph`: +这个数据结构在大多数的语言标准库中也不常见,所以我们不把它作为JS的弱项。 + + +## 优势 + +### 性能: + +- 少部分数据结构的性能已经超越JS内置数据结构(`Queue`,`Deque`, `HashMap`), 大部分已接近或超越其它语言,部分还没达标(`Graph`, `AVL Tree`) + +### 统一性 + +- 1、在基类中实现或约束 `forEach`,`filter`, `map`,`every`,`some`,`reduce`,`find`,`has`,`hasValue`,`get`,`print`,`isEmpty`, `clear`,`clone` 方法。 +- 2、使用生成器,统一实现`[Symbol.iterator]`,`entries`, `keys`, `values`。延迟执行遍历器以防止性能损失,并提供遍历时的可控性。 +- 3、所有的删除方法都统一采用ES6标准中惯用的`delete`。添加方法统一使用`add`。同时兼容Java中的一些规范。 +- 4、所有构造函数的第一个参数都是数据,第二个参数是配置,保持统一。第一个参数接受任意可遍历类型,以实现数据结构间自由转换。 +- 5、统一返回参数类型,如`add`方法统一返回布尔值。 + +### APIs的便捷性和简洁性 + +- 受ES6, Java, ESNext, TypeScript, Python的启发,如`forEach`,`filter`, `map`,`every`,`some`,`reduce`,`find`,`has`,`hasValue`,`get` + +### 使用通俗易懂业界常用命名 + +- `enqueue`, `dequeue`, `push`, `pop`, `poll`, `addLast`, `addFirst`, `pollFirst`, `pollLast`, `isEmpty`, `clear`, `print`, `clone` + +### 尽量实现可定制化功能 + +- 如所有遍历方法都提供回调函数(拉姆达表达式)的设计 + +### 完善的文档 + +- 文档不但阐述方法的用途,而且全系标注时间和空间复杂度 + + +## 社区活跃度 + +- 从社区来说,NPM下载量![npm](https://img.shields.io/npm/dm/data-structure-typed), +GitHub星星![GITHUB Star](https://img.shields.io/github/stars/zrwusa/data-structure-typed) +贡献者![GitHub contributors](https://img.shields.io/github/contributors/zrwusa/data-structure-typed) + +## 赞助我们吧 + +- 以软件工程标准来说,我们的项目目前已经达到至少75%的完整度,我们相信我们在您的赞助下很快将这个项目的完成度提高到95%,这对我的家庭来说算一种援助,对我们项目组来说是一种鼓励,也希望尽早能够实现成为`JS/TS`标准库的目标。 diff --git a/SPONSOR.md b/SPONSOR.md new file mode 100644 index 0000000..5705dbb --- /dev/null +++ b/SPONSOR.md @@ -0,0 +1,54 @@ +# Who am I + +I am a seasoned software development engineer with 15 years of professional experience. Following my graduation from university, I have contributed my expertise to various software companies across diverse industries, including some prominent ones. + +Of course, I have my flaws, I have a nerd-like attitude towards technology and could be described as a perfectionist. For instance, being excessively serious and having a penchant for studying the essence of things, I sometimes come across as a stern boss, which can be intimidating to my wife and daughter. + +Fortunately, my outlook on life is broad-minded. when it comes to life and family, the keywords are love, loyalty, and appreciation of beauty. I also enjoy studying cultural differences in various regions. Deep down, I actually have a bit of an artistic flair. LOL, just writing this makes me want to laugh! + +# Current Situation + +My recent move to Malaysia was motivated by the desire to provide my daughter with a formal education(Yes, you read it correctly, it is the word 'formal', because the education system in some countries is not normal), and it comes alongside holding a 10-year residency visa(MM2H). However, this visa restricts my employment opportunities within Malaysian companies. Fueled by my passion for programming and recognizing the absence of a standardized data structures library in JavaScript/TypeScript, I embarked on the journey of implementing my own set of data structures - `data-structure-typed` within the open-source community. + +# `data-structure-typed` Project Overview + +## Ultimate Goal + +- To seamlessly integrate into the standard library of JS/TS. + +## Pain Points Addressed + +### Enhanced the Performance of Simulated Data Structures in JS/TS + +- `Queue`: While many resort to using Array to simulate Queue and Deque, the time complexity of Array.shift is O(n). We have tackled this by implementing a Queue and Deque with O(1) time complexity. + +- `HashMap`: The built-in Map in JS is actually a LinkedHashMap, as it maintains the insertion order of elements, resulting in performance inferiority compared to a pure HashMap. We have implemented a pure HashMap. + +### Introducing Missing Native Data Structures in JS/TS + +- `Heap / Priority Queue`: Algorithms with O(log n) time complexity have been pivotal in improving efficiency since the dawn of computers. A Heap supports insertion, deletion, and search with O(log n) time complexity, coupled with the ability to obtain the minimum / maximum value in O(1) time. + +- `Red Black Tree`: Developers well-versed in databases, file systems, Linux virtual memory management, and network routing tables often have a nuanced understanding of Red-Black Trees. It stands out as the least operation-intensive among all balanced binary search trees, offering optimal performance balance in CRUD operations. + +## Advantages + +### Performance: + +- The performance of some ours data structures has surpassed JS's built-in data structures, such as `Queue`, `Deque` and `HashMap`, while most are comparable to or even surpass those in other languages. Some are still undergoing refinement, notably the `Graph` and `AVL Tree`. + +### Uniformity + +- 1. Concise and industry-standard naming:Implementation or constraint of `forEach`, `filter`, `map`, `every`, `some`, `reduce`, `find`, `has`, `hasValue`, `get`, `print`, `isEmpty`, `clear`, `clone` methods in the base class. +- 2. Adhere to the ES6 specification: Use of generators to uniformly implement `[Symbol.iterator]`, `entries`, `keys`, `values`. Delaying iterator execution prevents performance loss and provides control during traversal. All deletion methods uniformly use the widely adopted `delete` in ES6, while addition methods uniformly use `add`. Compatibility with some specifications in Java. +- 3. Constructors:The first parameter for all constructors is data, and the second parameter is configuration, maintaining uniformity. The first parameter accepts any iterable type for seamless conversion between data structures. +- 4. Return types:for example, the `add` method consistently returns a boolean. + +We would like to provide a more detailed description of this project, but space is limited here. For specific technical details, please refer to the [README.md](https://github.com/zrwusa/data-structure-typed), [SPECIFICATION.md](https://github.com/zrwusa/data-structure-typed/blob/main/CODE_OF_CONDUCT.md), or [API Docs](https://data-structure-typed-docs.vercel.app/). + +## Community Activity + +- In terms of community engagement, we have noteworthy metrics including NPM download count ![npm](https://img.shields.io/npm/dm/data-structure-typed), GitHub stars ![GITHUB Star](https://img.shields.io/github/stars/zrwusa/data-structure-typed), and contributors ![GitHub contributors](https://img.shields.io/github/contributors/zrwusa/data-structure-typed). + +## Sponsor Us + +- From a software engineering standards perspective, our project has achieved at least 75% completeness. With your sponsorship, we believe we can swiftly elevate the project's completion to 95%, providing valuable assistance to my family and serving as encouragement to our project team. Additionally, we aspire to expedite the goal of becoming an integral part of the `JS/TS` standard library. diff --git a/src/data-structures/base/iterable-base.ts b/src/data-structures/base/iterable-base.ts index 35d6083..fbfff56 100644 --- a/src/data-structures/base/iterable-base.ts +++ b/src/data-structures/base/iterable-base.ts @@ -289,8 +289,14 @@ export abstract class IterableEntryBase { abstract isEmpty(): boolean; + abstract clear(): void; + abstract clone(): any; + abstract map(...args: any[]): any; + + abstract filter(...args: any[]): any; + protected abstract _getIterator(...args: any[]): IterableIterator<[K, V]>; } @@ -497,7 +503,13 @@ export abstract class IterableElementBase { abstract isEmpty(): boolean; + abstract clear(): void; + abstract clone(): C; + abstract map(...args: any[]): any; + + abstract filter(...args: any[]): any; + protected abstract _getIterator(...args: any[]): IterableIterator; } diff --git a/src/data-structures/graph/directed-graph.ts b/src/data-structures/graph/directed-graph.ts index fbaf26d..4bc5afe 100644 --- a/src/data-structures/graph/directed-graph.ts +++ b/src/data-structures/graph/directed-graph.ts @@ -616,6 +616,23 @@ export class DirectedGraph< return this.vertexMap.size === 0 && this.inEdgeMap.size === 0 && this.outEdgeMap.size === 0; } + /** + * Time Complexity: O(1) + * Space Complexity: O(1) + */ + + /** + * Time Complexity: O(1) + * Space Complexity: O(1) + * + * The clear function resets the vertex map, in-edge map, and out-edge map. + */ + clear() { + this._vertexMap = new Map(); + this._inEdgeMap = new Map(); + this._outEdgeMap = new Map(); + } + /** * The clone function creates a new DirectedGraph object with the same vertices and edges as the original. * diff --git a/src/data-structures/graph/undirected-graph.ts b/src/data-structures/graph/undirected-graph.ts index 86b29e2..2a6fdde 100644 --- a/src/data-structures/graph/undirected-graph.ts +++ b/src/data-structures/graph/undirected-graph.ts @@ -377,6 +377,22 @@ export class UndirectedGraph< return this.vertexMap.size === 0 && this.edgeMap.size === 0; } + /** + * Time Complexity: O(1) + * Space Complexity: O(1) + */ + + /** + * Time Complexity: O(1) + * Space Complexity: O(1) + * + * The clear function resets the vertex and edge maps to empty maps. + */ + clear() { + this._vertexMap = new Map(); + this._edgeMap = new Map(); + } + /** * The clone function creates a new UndirectedGraph object and copies the * vertexMap and edgeMap from this graph to the new one. This is done by diff --git a/src/data-structures/tree/tree.ts b/src/data-structures/tree/tree.ts index 9cf04da..c0e85e5 100644 --- a/src/data-structures/tree/tree.ts +++ b/src/data-structures/tree/tree.ts @@ -15,10 +15,10 @@ export class TreeNode { this._children = children || []; } - private _key: string; + protected _key: string; /** - * The function returns the value of the private variable _key. + * The function returns the value of the protected variable _key. * @returns The value of the `_key` property, which is a string. */ get key(): string { @@ -26,7 +26,7 @@ export class TreeNode { } /** - * The above function sets the value of a private variable called "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. */ @@ -34,7 +34,7 @@ export class TreeNode { this._key = value; } - private _value?: V | undefined; + protected _value?: V | undefined; /** * The function returns the value stored in a variable, or undefined if the variable is empty. @@ -53,7 +53,7 @@ export class TreeNode { this._value = value; } - private _children?: TreeNode[] | undefined; + protected _children?: TreeNode[] | undefined; /** * The function returns an array of TreeNode objects or undefined. diff --git a/src/data-structures/trie/trie.ts b/src/data-structures/trie/trie.ts index 8f19808..6844431 100644 --- a/src/data-structures/trie/trie.ts +++ b/src/data-structures/trie/trie.ts @@ -22,7 +22,7 @@ export class TrieNode { protected _key: string; /** - * The function returns the value of the private variable _key. + * The function returns the value of the protected variable _key. * @returns The value of the `_key` property, which is a string. */ get key(): string { @@ -30,7 +30,7 @@ export class TrieNode { } /** - * The above function sets the value of a private variable called "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. */ @@ -214,6 +214,22 @@ export class Trie extends IterableElementBase { return this.size === 0; } + /** + * Time Complexity: O(1) + * Space Complexity: O(1) + */ + + /** + * Time Complexity: O(1) + * Space Complexity: O(1) + * + * The clear function resets the size of the Trie to 0 and creates a new root TrieNode. + */ + clear(): void { + this._size = 0; + this._root = new TrieNode(''); + } + /** * Time Complexity: O(l), where l is the length of the word being deleted. * Space Complexity: O(n) - Due to the recursive DFS approach. diff --git a/test/unit/data-structures/graph/abstract-graph.test.ts b/test/unit/data-structures/graph/abstract-graph.test.ts index 2baa63d..d53105b 100644 --- a/test/unit/data-structures/graph/abstract-graph.test.ts +++ b/test/unit/data-structures/graph/abstract-graph.test.ts @@ -81,6 +81,9 @@ class MyGraph< return true; } + clear(): void { + } + clone(): any { return {}; }