From 25e9f3657702676457baae830cfc7698cc4695c9 Mon Sep 17 00:00:00 2001 From: Revone Date: Tue, 16 Jan 2024 11:26:43 +0800 Subject: [PATCH] feat: Remove the meaningless Tree data structure. refactor: Adjust the logic of isRealNode for a slight performance improvement. docs: table fixed --- CHANGELOG.md | 2 +- README.md | 3 + .../binary-tree/binary-tree.ts | 4 +- src/data-structures/index.ts | 1 - src/data-structures/tree/index.ts | 1 - src/data-structures/tree/tree.ts | 115 ------------------ .../data-structures/tree/tree.test.ts | 0 test/unit/data-structures/tree/tree.test.ts | 39 ------ 8 files changed, 6 insertions(+), 159 deletions(-) delete mode 100644 src/data-structures/tree/index.ts delete mode 100644 src/data-structures/tree/tree.ts delete mode 100644 test/performance/data-structures/tree/tree.test.ts delete mode 100644 test/unit/data-structures/tree/tree.test.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 87586e5..345a049 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,7 +8,7 @@ All notable changes to this project will be documented in this file. - [Semantic Versioning](https://semver.org/spec/v2.0.0.html) - [`auto-changelog`](https://github.com/CookPete/auto-changelog) -## [v1.51.2](https://github.com/zrwusa/data-structure-typed/compare/v1.35.0...main) (upcoming) +## [v1.51.3](https://github.com/zrwusa/data-structure-typed/compare/v1.35.0...main) (upcoming) ### Changes diff --git a/README.md b/README.md index 29cfcce..535a129 100644 --- a/README.md +++ b/README.md @@ -291,6 +291,7 @@ We provide data structures that are not available in JS/TS View + Linked List @@ -325,12 +326,14 @@ We provide data structures that are not available in JS/TS View + Binary Indexed Tree View + diff --git a/src/data-structures/binary-tree/binary-tree.ts b/src/data-structures/binary-tree/binary-tree.ts index 85cb4e1..4b07d8a 100644 --- a/src/data-structures/binary-tree/binary-tree.ts +++ b/src/data-structures/binary-tree/binary-tree.ts @@ -316,8 +316,8 @@ export class BinaryTree< * @returns a boolean value. */ isRealNode(node: KeyOrNodeOrEntry): node is NODE { - if (!this.isNode(node)) return false; - return node !== this.NIL; + if (node === this.NIL || node === null || node === undefined) return false; + return this.isNode(node); } /** diff --git a/src/data-structures/index.ts b/src/data-structures/index.ts index 3cd0a72..60d00e4 100644 --- a/src/data-structures/index.ts +++ b/src/data-structures/index.ts @@ -4,7 +4,6 @@ export * from './stack'; export * from './queue'; export * from './graph'; export * from './binary-tree'; -export * from './tree'; export * from './heap'; export * from './priority-queue'; export * from './matrix'; diff --git a/src/data-structures/tree/index.ts b/src/data-structures/tree/index.ts deleted file mode 100644 index 50842b5..0000000 --- a/src/data-structures/tree/index.ts +++ /dev/null @@ -1 +0,0 @@ -export * from './tree'; diff --git a/src/data-structures/tree/tree.ts b/src/data-structures/tree/tree.ts deleted file mode 100644 index c0e85e5..0000000 --- a/src/data-structures/tree/tree.ts +++ /dev/null @@ -1,115 +0,0 @@ -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/test/performance/data-structures/tree/tree.test.ts b/test/performance/data-structures/tree/tree.test.ts deleted file mode 100644 index e69de29..0000000 diff --git a/test/unit/data-structures/tree/tree.test.ts b/test/unit/data-structures/tree/tree.test.ts deleted file mode 100644 index 318fbda..0000000 --- a/test/unit/data-structures/tree/tree.test.ts +++ /dev/null @@ -1,39 +0,0 @@ -import { TreeNode } from '../../../../src'; - -describe('TreeNode', () => { - it('should create a TreeNode with the given key and value', () => { - const node = new TreeNode('1', 'Node 1'); - expect(node.key).toBe('1'); - expect(node.value).toBe('Node 1'); - expect(node.children).toEqual([]); - }); - - it('should add children to the TreeNode', () => { - const parentNode = new TreeNode('1', 'Parent Node'); - const child1 = new TreeNode('2', 'Child 1'); - const child2 = new TreeNode('3', 'Child 2'); - - parentNode.addChildren([child1, child2]); - - expect(parentNode.children).toEqual([child1, child2]); - }); - - it('should calculate the height of the tree correctly', () => { - const rootNode = new TreeNode('1', 'Root Node'); - const child1 = new TreeNode('2', 'Child 1'); - const child2 = new TreeNode('3', 'Child 2'); - const grandchild1 = new TreeNode('4', 'Grandchild 1'); - const grandchild2 = new TreeNode('5', 'Grandchild 2'); - - rootNode.addChildren([child1, child2]); - child1.addChildren([grandchild1]); - child2.addChildren([grandchild2]); - - expect(rootNode.getHeight()).toBe(2); // Height of the tree should be 2 - }); - - it('should handle nodes without children when calculating height', () => { - const rootNode = new TreeNode('1', 'Root Node'); - expect(rootNode.getHeight()).toBe(0); // Height of a single node should be 0 - }); -});