You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: source/lectures/data/trees.md
+19-6Lines changed: 19 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -14,7 +14,8 @@ As an abstract data type, it generally uses the following definitions:
14
14
- 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.
15
15
- A node without children is called *a leaf*.
16
16
- 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.
18
19
- 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.
19
20
20
21
From there, operations generally include, as usual
@@ -33,8 +34,8 @@ In the following example:
33
34
- The root has two children, holding the values 7 and 11,
34
35
- The node holding the value 5 has $0$ children, hence it is a leaf,
35
36
- 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$.
38
39
39
40
## Possible Implementation
40
41
@@ -56,6 +57,7 @@ As we can see, a `Node` has three elements:
56
57
-`Data` is the value it is holding,
57
58
-`left` denotes its "left child" (which is a `Node`, possibly `null`),
58
59
-`right` denotes its "right child" (which is a `Node`, possibly `null`),
60
+
- (plus `Height`, implemented as a read-only property)
59
61
60
62
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:
61
63
@@ -71,8 +73,19 @@ A `BTree` object is simply the `root` `Node`, and we can construct, empty, and t
71
73
72
74
#### Computing the Depth of the tree
73
75
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.
76
89
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.
77
90
78
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,7 +105,7 @@ For the `root`'s children to determine what the deepest node under them is, they
92
105
Etc., etc. Hence, we get:
93
106
94
107
```{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
96
109
```
97
110
98
111
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