Tyro
Tyro

Reputation: 150

Depth vs Level of a Tree

Are they the same thing?

In this article Height,Depth and Level of a Tree

Depth is defined as the number of edges from a node to the tree's root node while Level is defined as

1 + the number of connections between the node and the root."

or basically depth + 1

and in this link

What is level of root node in a tree?

It is said that level can either start with 1 or 0 which makes it the same with depth if it starts with 0

So which is which? If it is 1 + depth then what is the use of adding 1?

Upvotes: 2

Views: 3063

Answers (2)

Tyro
Tyro

Reputation: 150

The question was asked because there were already a lot about height vs depth of a tree but not a clear distinction in level and depth of a tree and is often times used interchangeably.

So as I have read here, in different articles and in a book;

The level is depth + 1. It is not the same with depth although some choose to start the level with 0.

Depth is mostly used in relation to the root as

Depth is the number of edges from the root to a node

So it is mostly treated as a property of a node while the level is mostly used as a whole e.g.

Width is the number of nodes in a level

Or in

A perfect binary tree is where all internal nodes have two children and all leaves are at the same level

So level is like steps in a tree wherein the root node is the first step and it just so happen that it shared the same pattern with the depth of a node.

Although there is no single definition, to distinguish the two the level is mostly taken as depth + 1.

Upvotes: 2

reyad
reyad

Reputation: 1432

well, it will be best explained by a image, just see below:

// I've used 1 for roots level
// though some people consider roots level as 0, so you can use either 0 or 1
// I would prefer to use 1
// but its your choice

                                   o(depth=0, height=3, lev=1)
                                 /   \
      (depth=1, height=2, lev=2)o     o(depth=1, height=1, lev=2)
                               /     / \
    (depth=2, height=1, lev=3)o     o   o(depth=2, height=0, lev=3)
                             /
  (depth=3, height=0, lev=4)o

I hope its clear to you now...

Upvotes: 3

Related Questions