javascript-algorithms icon indicating copy to clipboard operation
javascript-algorithms copied to clipboard

BinaryTreeNode.js height() gives one less than tree's height.

Open gyaneshwar-sunkara opened this issue 2 years ago • 2 comments

The height of the tree is the number of edges in the tree from the root to the deepest node.

height right now gives one less than the total number of the edges, small tweek made will fix it.

gyaneshwar-sunkara avatar Sep 08 '23 04:09 gyaneshwar-sunkara

You have the definition of “height” of a tree confused with the “depth” of a tree. Depth is from root to deepest node and height is from the deepest node to the root. For clarification, see:Introduction to Algorithms, 4th ed Cormen et al p.1173Thanks,JohnOn Sep 7, 2023, at 10:00 PM, Gyaneshwar Sunkara @.***> wrote:The height of the tree is the number of edges in the tree from the root to the deepest node. height right now gives one less than the total number of the edges, small tweek made will fix it.

You can view, comment on, or merge this pull request online at:   https://github.com/trekhleb/javascript-algorithms/pull/1072

Commit Summary

6800c9a BinaryTreeNode.js height() gives one less than tree's height.

File Changes (1 file)

M
src/data-structures/tree/BinaryTreeNode.js
(6)

Patch Links:

https://github.com/trekhleb/javascript-algorithms/pull/1072.patch https://github.com/trekhleb/javascript-algorithms/pull/1072.diff

—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you are subscribed to this thread.Message ID: @.***>

johndub27 avatar Sep 10 '23 19:09 johndub27

His fix has nothing to do with height vs depth, but with the way you define an empty tree: -1 or 0. Both definitions are fine; the only important thing is to be explicit on what it actually means.

lazarljubenovic avatar Sep 10 '23 20:09 lazarljubenovic