mirror of
https://github.com/zrwusa/data-structure-typed.git
synced 2024-11-23 12:54:04 +00:00
Merge branch 'main' into trie
This commit is contained in:
commit
9cf303edd4
|
@ -47,10 +47,8 @@ export class TreeNode<V = any> {
|
|||
}
|
||||
|
||||
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<V>, level: number) => {
|
||||
if (level > maxDepth) {
|
||||
maxDepth = level;
|
||||
|
@ -62,7 +60,7 @@ export class TreeNode<V = any> {
|
|||
}
|
||||
}
|
||||
};
|
||||
bfs(beginRoot, 1);
|
||||
bfs(this, 0);
|
||||
}
|
||||
return maxDepth;
|
||||
}
|
||||
|
|
|
@ -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<string>('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
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue