Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Depth vs Height of a tree. Refreshing the fundamentals

I am doing a refresher an algorithms and data structures.

I am confused on the concept of depth vs height of a tree. In many cases, especially on sites focusing on interview quizes, it seems to me that these terms are used interchangeably.

It seems to me that the basic literature defines them as applicable to a node and not to a tree.

So the depth of the root (which is a node) is 0. The height of root (or any subnode) is the max height of its children.

But when you apply these terms on a tree i.e. find the max depth of a tree, it seems that these terms now are "meaningless" and can be used interchangeably i.e. to find the max depth just calculate max height.

For example in this post Check if tree is balanced the answers focus on the height of the tree while the definition of balance could be on the depth of the tree

Is my understanding correct or am I messing up on these fundamentals?

like image 253
Cratylus Avatar asked Dec 11 '11 15:12

Cratylus


People also ask

What is the difference between depth and height of the tree?

The depth(or level) of a node is its distance(i.e. no of edges) from tree's root node. The height is number of edges between root node and furthest leaf.

What do you mean by depth of a tree?

Depth. In a tree, many edges from the root node to the particular node are called the depth of the tree. In the tree, the total number of edges from the root node to the leaf node in the longest path is known as "Depth of Tree". In the tree data structures, the depth of the root node is 0.

Is level and depth same?

Level – The level of a node is defined by 1 + the number of connections between the node and the root. Simply, level is depth plus 1. The important thing to remember is when talking about level, it starts from 1 and the level of the root is 1.


2 Answers

When talking about a tree they mean the same thing: the length of the longest path from the root to a leaf node.

like image 114
Tudor Avatar answered Sep 30 '22 16:09

Tudor


The depth is usually used to describe a property of a tree node, while the height is used to describe the property of the entire tree, as in the following examples:

  • Root node has a depth of zero
  • Node X has a depth of N
  • The height of the tree is M

The height of a tree is defined as the depth of its deepest node.

like image 28
Sergey Kalinichenko Avatar answered Sep 30 '22 15:09

Sergey Kalinichenko