Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Height of a tree with only one node

According to Wikipedia,

The height of a tree is the length of the path from the root to the deepest node in the tree. A (rooted) tree with only one node (the root) has a height of zero (or one).

I dont get it - is it zero or one (or both)?

like image 420
Snowman Avatar asked Oct 31 '10 22:10

Snowman


People also ask

Can a tree only have one node?

Root-tree: a tree with only one node. Binary tree: a tree in which each node has at most two children (parent, left, and right) Two tree: a binary tree that either is empty or each non-leaf has two children. Heap: a tree where parent node has bigger (smaller) value than children.

What is the depth of a tree with only a root node?

The depth of a node is the number of edges from that node to the tree's root node. As such, the depth of the whole tree would be the depth of its deepest leaf node. The root node has a depth of 0.


1 Answers

It just an assuption you make for the recursive description of the height of a binary tree. You can consider a tree composed by just a node either with 0 height or with 1 height.

If you really want to think about it somehow you can think that

  • it's 0 if you consider the height as a edge count (so that a single node doesn't have any edge, hence 0)
  • it's 1 if you consider the height as a node count (so that a single node counts as 1)

This is just to describe how much height the smallest tree has, then in any case whenever you add a descending node you will add also a related edge so it will increase accordingly.

In the example provided in wikipedia:

alt text

This tree can have height 4 (nodes) or 3 (edges). It depends if you are counting it by edges or by nodes.

like image 183
Jack Avatar answered Sep 18 '22 12:09

Jack