Skip to content

Commit cfe7fb7

Browse files
ClémentClément
authored andcommitted
Clarifying depth and height.
1 parent 8c64230 commit cfe7fb7

2 files changed

Lines changed: 64 additions & 9 deletions

File tree

source/code/projects/Tree/Tree/BTree.cs

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,52 @@ public int Height()
9090
}
9191
return height;
9292
}
93+
// We illustrate another way of computing the
94+
// height of the tree, as the depth of the
95+
// deepest node.
9396

94-
// Finding is also recursive.
97+
public int HeightAlt()
98+
{
99+
int height = 0;
100+
if (root != null)
101+
{
102+
height = Depth(root);
103+
}
104+
return height;
105+
}
95106

96-
public virtual bool Find(T dataP)
107+
private int Depth(Node nodeP)
108+
{
109+
// We first assume that the Depth of
110+
// nodeP is 0.
111+
int result = 0;
112+
// Then, we compute the depth of
113+
// its left subtree, if it is not null.
114+
int depthL = 0;
115+
if (nodeP.left != null)
116+
{
117+
depthL = 1 + Depth(nodeP.left);
118+
}
119+
// We proceed similarly for the
120+
// left sub-tree.
121+
int depthR = 0;
122+
if (nodeP.right != null)
123+
{
124+
depthR = 1 + Depth(nodeP.right);
125+
}
126+
// Finally, if at least one sub-tree
127+
// is not null, we take the max of their
128+
// depths to be the depth of nodeP.
129+
if (nodeP.left != null || nodeP.right != null)
130+
{
131+
result = Math.Max(depthL, depthR);
132+
}
133+
return result;
134+
}
135+
136+
// Finding is also recursive.
137+
138+
public virtual bool Find(T dataP)
97139
{
98140
bool found = false;
99141
if (root != null)
@@ -209,7 +251,7 @@ private string TraversePo(Node nodeP)
209251

210252
public override string ToString()
211253
{
212-
string returned = "";
254+
string returned = "Height of the tree:" + Height() + " (computed alternatively: " + HeightAlt() + ")\n";
213255
if (root != null)
214256
{
215257
returned += Stringify(root, 0);

source/lectures/data/trees.md

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ As an abstract data type, it generally uses the following definitions:
1414
- A (rooted) binary tree has exactly one node with 0 parent (that is not the child of any other node), called *the root*. Except for the root, all the nodes have exactly one parent.
1515
- A node without children is called *a leaf*.
1616
- The *depth of a node* is the distance (i.e., the number of times we must go to its parent) from it to the root. This means in particular that the depth of the root is $0$.
17-
- The *depth of a tree* is the maximum depth of the nodes it contain.
17+
- The *height of a node* is the greater distance from it to a leaf (i.e., the number of edges on the *longest* path from the node to a leaf).
18+
- The *height of a tree* is the height of its root node.
1819
- A *subtree* is the tree obtain by considering a particular node in a tree as the root of the tree made of all the nodes "below" it, starting with its children.
1920

2021
From there, operations generally include, as usual
@@ -33,8 +34,8 @@ In the following example:
3334
- The root has two children, holding the values 7 and 11,
3435
- The node holding the value 5 has $0$ children, hence it is a leaf,
3536
- The node with value 11 has one child (holding the value 30): we call it "the right child", and observe that the node holding the value 11 has no "left child",
36-
- The depth of the tree is $4$,
37-
- The nodes holding the values 7, 5 and 8 taken together form a subtree, of depth $1$.
37+
- The height *of the tree* is $4$,
38+
- The nodes holding the values 7, 5 and 8 taken together form a subtree, of height $1$.
3839

3940
## Possible Implementation
4041

@@ -56,6 +57,7 @@ As we can see, a `Node` has three elements:
5657
- `Data` is the value it is holding,
5758
- `left` denotes its "left child" (which is a `Node`, possibly `null`),
5859
- `right` denotes its "right child" (which is a `Node`, possibly `null`),
60+
- (plus `Height`, implemented as a read-only property)
5961

6062
If we wanted to be more technically correct in our drawing, we would explicitly label some nodes as `null`, and the previous tree would actually be as follows:
6163

@@ -71,8 +73,19 @@ A `BTree` object is simply the `root` `Node`, and we can construct, empty, and t
7173

7274
#### Computing the Depth of the tree
7375

74-
Now, assume that we are given a `BTree` object. How can we compute its depth?
75-
Its depth is the maximum depth of the nodes it contain.
76+
Now, assume that we are given a `BTree` object. How can we compute its height?
77+
There are two ways:
78+
79+
- The height of a tree is the height of its root node,
80+
- The height of a tree is equivalently equal to the depth of its deepest node.
81+
82+
We illustrate both approaches below:
83+
84+
```{download="./code/projects/Tree.zip"}
85+
!include`snippetStart="// its root node.", snippetEnd="// Finding is also recursive."` code/projects/Tree/Tree/BTree.cs
86+
```
87+
88+
The second approach uses that the height of the tree is the maximum depth of the nodes it contain.
7689
However, only the root knows its depth (it is $0$), all the other nodes do not know their depth, only that they have 0, 1 or 2 children.
7790

7891
Of course, if the `BTree` is `null` or if its `root` has $0$ children, then deciding its depth is easy: it is $0$.
@@ -92,7 +105,7 @@ For the `root`'s children to determine what the deepest node under them is, they
92105
Etc., etc. Hence, we get:
93106

94107
```{download="./code/projects/Tree.zip"}
95-
!include`snippetStart="// compute the Depth of a tree.", snippetEnd="// Finding is also recursive."` code/projects/Tree/Tree/BTree.cs
108+
!include`snippetStart="// deepest node.", snippetEnd="// Finding is also recursive."` code/projects/Tree/Tree/BTree.cs
96109
```
97110

98111
Note that we have *two* recursive calls: one for the `left` `Node`, one for the `right` `Node` (if they are not `null`).

0 commit comments

Comments
 (0)