From 07825744f403ac69f2df521c61a6b8850754ea09 Mon Sep 17 00:00:00 2001 From: Revone Date: Wed, 18 Oct 2023 17:07:27 +0800 Subject: [PATCH 1/2] [tree] getHeight returns faulty height bug fixed --- src/data-structures/tree/tree.ts | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/data-structures/tree/tree.ts b/src/data-structures/tree/tree.ts index be01002..b2167d6 100644 --- a/src/data-structures/tree/tree.ts +++ b/src/data-structures/tree/tree.ts @@ -47,10 +47,8 @@ export class TreeNode { } getHeight() { - // eslint-disable-next-line @typescript-eslint/no-this-alias - const beginRoot = this; - let maxDepth = 1; - if (beginRoot) { + let maxDepth = 0; + if (this) { const bfs = (node: TreeNode, level: number) => { if (level > maxDepth) { maxDepth = level; @@ -62,7 +60,7 @@ export class TreeNode { } } }; - bfs(beginRoot, 1); + bfs(this, 0); } return maxDepth; } From 56e1f05b5999aea4c6384208a07d6d0ca60750f4 Mon Sep 17 00:00:00 2001 From: Revone Date: Wed, 18 Oct 2023 17:10:31 +0800 Subject: [PATCH 2/2] [tree] tests passed --- test/unit/data-structures/tree/tree.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/unit/data-structures/tree/tree.test.ts b/test/unit/data-structures/tree/tree.test.ts index 1ea5e47..5cda52c 100644 --- a/test/unit/data-structures/tree/tree.test.ts +++ b/test/unit/data-structures/tree/tree.test.ts @@ -29,11 +29,11 @@ describe('TreeNode', () => { child1.addChildren([grandchild1]); child2.addChildren([grandchild2]); - expect(rootNode.getHeight()).toBe(3); // Height of the tree should be 3 + 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(1); // Height of a single node should be 1 + expect(rootNode.getHeight()).toBe(0); // Height of a single node should be 0 }); });