Skip to content

Commit 429cc18

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

3 files changed

Lines changed: 84 additions & 80 deletions

File tree

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public bool IsEmpty()
8383
// its root node.
8484
public int Height()
8585
{
86-
int height = 0;
86+
int height = -1;
8787
if (root != null)
8888
{
8989
height = root.Height;
@@ -96,7 +96,7 @@ public int Height()
9696

9797
public int HeightAlt()
9898
{
99-
int height = 0;
99+
int height = -1;
100100
if (root != null)
101101
{
102102
height = Depth(root);
@@ -114,21 +114,21 @@ private int Depth(Node nodeP)
114114
int depthL = 0;
115115
if (nodeP.left != null)
116116
{
117-
depthL = 1 + Depth(nodeP.left);
117+
depthL = Depth(nodeP.left);
118118
}
119119
// We proceed similarly for the
120120
// left sub-tree.
121121
int depthR = 0;
122122
if (nodeP.right != null)
123123
{
124-
depthR = 1 + Depth(nodeP.right);
124+
depthR = Depth(nodeP.right);
125125
}
126126
// Finally, if at least one sub-tree
127127
// is not null, we take the max of their
128128
// depths to be the depth of nodeP.
129129
if (nodeP.left != null || nodeP.right != null)
130130
{
131-
result = Math.Max(depthL, depthR);
131+
result = 1 + Math.Max(depthL, depthR);
132132
}
133133
return result;
134134
}

source/code/projects/Tree/Tree/Program.cs

Lines changed: 70 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -2,83 +2,84 @@
22

33
class Program
44
{
5-
static void Main()
6-
{
7-
BSTree<int> btree = new BSTree<int>();
8-
Console.WriteLine(btree);
5+
static void Main()
6+
{
7+
BSTree<int> btree = new BSTree<int>();
8+
Console.WriteLine(btree);
99

10-
/*
11-
* The purpose of this example is to illustrate that
12-
* the order in which values are inserted matter.
13-
*/
14-
btree.Clear();
15-
btree.Insert(10);
16-
btree.Insert(6);
17-
btree.Insert(13);
18-
btree.Insert(12);
19-
btree.Insert(14);
20-
Console.WriteLine(btree);
21-
btree.Clear();
22-
btree.Insert(14);
23-
btree.Insert(12);
24-
btree.Insert(13);
25-
btree.Insert(6);
26-
btree.Insert(10);
27-
Console.WriteLine(btree);
10+
/*
11+
* The purpose of this example is to illustrate that
12+
* the order in which values are inserted matter.
13+
*/
14+
btree.Insert(10);
15+
Console.WriteLine(btree);
2816

29-
/* Second example, we begin by clearing the tree. */
17+
btree.Insert(6);
18+
btree.Insert(13);
19+
btree.Insert(12);
20+
btree.Insert(14);
21+
Console.WriteLine(btree);
22+
btree.Clear();
23+
btree.Insert(14);
24+
btree.Insert(12);
25+
btree.Insert(13);
26+
btree.Insert(6);
27+
btree.Insert(10);
28+
Console.WriteLine(btree);
3029

31-
btree.Clear();
32-
btree.Insert(10);
33-
// Attempting to insert the same value
34-
// twice raises an exception:
35-
try
36-
{
37-
btree.Insert(10);
38-
}
39-
catch (Exception e)
40-
{
41-
Console.WriteLine(e.Message);
42-
}
43-
btree.Insert(11);
44-
btree.Insert(30);
45-
btree.Insert(7);
46-
Console.WriteLine(btree);
47-
btree.Insert(5);
48-
btree.Insert(12);
49-
btree.Insert(8);
50-
btree.Insert(15);
30+
/* Second example, we begin by clearing the tree. */
5131

52-
Console.WriteLine(btree);
53-
Console.WriteLine("Inorder traversal:");
54-
Console.WriteLine(btree.TrasverseI());
55-
Console.WriteLine("Preorder traversal:");
56-
Console.WriteLine(btree.TrasversePr());
57-
Console.WriteLine("Postorder traversal:");
58-
Console.WriteLine(btree.TrasversePo());
32+
btree.Clear();
33+
btree.Insert(10);
34+
// Attempting to insert the same value
35+
// twice raises an exception:
36+
try
37+
{
38+
btree.Insert(10);
39+
}
40+
catch (Exception e)
41+
{
42+
Console.WriteLine(e.Message);
43+
}
44+
btree.Insert(11);
45+
btree.Insert(30);
46+
btree.Insert(7);
47+
Console.WriteLine(btree);
48+
btree.Insert(5);
49+
btree.Insert(12);
50+
btree.Insert(8);
51+
btree.Insert(15);
5952

60-
Console.WriteLine("Smallest value: " + btree.FindMin());
53+
Console.WriteLine(btree);
54+
Console.WriteLine("Inorder traversal:");
55+
Console.WriteLine(btree.TrasverseI());
56+
Console.WriteLine("Preorder traversal:");
57+
Console.WriteLine(btree.TrasversePr());
58+
Console.WriteLine("Postorder traversal:");
59+
Console.WriteLine(btree.TrasversePo());
6160

62-
btree.Delete(btree.FindMin());
63-
Console.WriteLine(btree);
64-
Console.WriteLine("Smallest value: " + btree.FindMin());
61+
Console.WriteLine("Smallest value: " + btree.FindMin());
6562

66-
btree.Delete(btree.FindMin());
67-
Console.WriteLine(btree);
68-
Console.WriteLine("Smallest value: " + btree.FindMin());
63+
btree.Delete(btree.FindMin());
64+
Console.WriteLine(btree);
65+
Console.WriteLine("Smallest value: " + btree.FindMin());
6966

70-
btree.Insert(-1);
71-
btree.Insert(-8);
72-
btree.Insert(-7);
73-
btree.Insert(-12);
74-
btree.Insert(-15);
75-
btree.Insert(-3);
76-
Console.WriteLine(btree);
77-
Console.WriteLine("Smallest value: " + btree.FindMin());
67+
btree.Delete(btree.FindMin());
68+
Console.WriteLine(btree);
69+
Console.WriteLine("Smallest value: " + btree.FindMin());
7870

79-
for (int i = -10; i <= 10; i++)
80-
{
81-
Console.WriteLine(i + " occurs: " + btree.Find(i));
71+
btree.Insert(-1);
72+
btree.Insert(-8);
73+
btree.Insert(-7);
74+
btree.Insert(-12);
75+
btree.Insert(-15);
76+
btree.Insert(-3);
77+
Console.WriteLine(btree);
78+
Console.WriteLine("Smallest value: " + btree.FindMin());
79+
80+
for (int i = -10; i <= 10; i++)
81+
{
82+
Console.WriteLine(i + " occurs: " + btree.Find(i));
83+
}
8284
}
83-
}
8485
}

source/lectures/data/trees.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,28 +79,31 @@ There are two ways:
7979
- The height of a tree is the height of its root node,
8080
- The height of a tree is equivalently equal to the depth of its deepest node.
8181

82+
If the tree is empty, then we set the height to be -1.
83+
8284
We illustrate both approaches below:
8385

8486
```{download="./code/projects/Tree.zip"}
85-
!include`snippetStart="// its root node.", snippetEnd="// Finding is also recursive."` code/projects/Tree/Tree/BTree.cs
87+
!include`snippetStart="// its root node.", snippetEnd="// We illustrate another way of computing the"` code/projects/Tree/Tree/BTree.cs
8688
```
8789

8890
The second approach uses that the height of the tree is the maximum depth of the nodes it contain.
8991
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.
9092

91-
Of course, if the `BTree` is `null` or if its `root` has $0$ children, then deciding its depth is easy: it is $0$.
92-
Otherwise, the idea is that each tree knows that its depth is $1$ more than the depth of the its two sub-trees: remains to find which sub-tree has the biggest depth.
93+
Of course, if the `BTree` is `null`, then deciding its height is easy: it is -1.
94+
If its `root` has $0$ children, then it is 0.
95+
But otherwise, we need to ask the children their depths, take the maximum, and add 1 to it.
9396
For this, we will use *recursion*.
9497

95-
In short, for a tree to determine its node, it needs to
98+
In short, for a node to determine its depth, it needs to
9699

97100
- Have its `root` "ask its children" what their deepest node is,
98-
- Take the maximum value returned.
101+
- Take the maximum value returned, and add 1 to it.
99102

100103
For the `root`'s children to determine what the deepest node under them is, they need both to
101104

102105
- "Ask their children" what is their deepest node,
103-
- Take the maximum value returned.
106+
- Take the maximum value returned, and add 1 to it.
104107

105108
Etc., etc. Hence, we get:
106109

0 commit comments

Comments
 (0)