Skip to content

Commit fe18c4a

Browse files
committed
Working on notes on trees.
1 parent 7ef3573 commit fe18c4a

8 files changed

Lines changed: 246 additions & 110 deletions

File tree

source/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ $(FLO_DIR)%.png: $(FLO_DIR)%.txt
378378
# Graph diagram are only used for
379379
# binary trees.
380380
$(GRA_DIR)%.md: $(GRA_DIR)%.txt | $(GRA_DIR)%.svg $(GRA_DIR)%.png
381-
echo "![A binary tree diagram ([text version](./$(basename $@).txt), [image version](./$(basename $@).png), [svg version](./$(basename $@).svg))]($(notdir $(basename $@)).png)" > $@
381+
echo "![$$(sed -n '1p' $< | sed -r 's/^.{3}//') ([text version](./$(basename $@).txt), [image version](./$(basename $@).png), [svg version](./$(basename $@).svg))]($(notdir $(basename $@)).png)" > $@
382382

383383
$(GRA_DIR)%.svg: $(GRA_DIR)%.txt
384384
@echo "Creating $@"

source/code/projects/Tree/Tree/BSTree.cs

Lines changed: 68 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
public class BSTree<T> : BTree<T>
55
where T : IComparable<T>
66
{
7+
// Inserting into a BSTree
78
public override void Insert(T dataP)
89
{
910
root = Insert(dataP, root);
@@ -31,8 +32,72 @@ private Node Insert(T dataP, Node nodeP)
3132
}
3233
return nodeP;
3334
}
35+
public override bool Find(T dataP)
36+
{
37+
bool found = false;
38+
if (root != null)
39+
{
40+
found = Find(root, dataP);
41+
}
42+
return found;
43+
}
44+
45+
// Finding into a BSTree
3446

35-
public override bool Delete(T dataP)
47+
private bool Find(Node nodeP, T dataP)
48+
{
49+
bool found = false;
50+
if (nodeP != null)
51+
{
52+
if (nodeP.Data.Equals(dataP))
53+
{
54+
found = true;
55+
}
56+
else
57+
{
58+
if (dataP.CompareTo(nodeP.Data) < 0) // dataP < nodeP.Data
59+
{
60+
found = Find(nodeP.left, dataP);
61+
}
62+
else if (dataP.CompareTo(nodeP.Data) > 0) // dataP > nodeP.Data
63+
{
64+
found = Find(nodeP.right, dataP);
65+
}
66+
}
67+
}
68+
return found;
69+
}
70+
71+
public T FindMin()
72+
{
73+
if (root == null)
74+
{
75+
throw new ApplicationException(
76+
"Cannot find a value in an empty tree!"
77+
);
78+
}
79+
else
80+
{
81+
return FindMin(root);
82+
}
83+
}
84+
85+
private T FindMin(Node nodeP)
86+
{
87+
T minValue;
88+
if (nodeP.left == null)
89+
{
90+
minValue = nodeP.Data;
91+
}
92+
else
93+
{
94+
minValue = FindMin(nodeP.left);
95+
}
96+
return minValue;
97+
}
98+
// Deleting from a BSTree
99+
100+
public override bool Delete(T dataP)
36101
{
37102
return Delete(dataP, ref root);
38103
}
@@ -76,65 +141,7 @@ private bool Delete(T dataP, ref Node nodeP)
76141
return found;
77142
}
78143

79-
public override bool Find(T dataP)
80-
{
81-
bool found = false;
82-
if (root != null)
83-
{
84-
found = Find(root, dataP);
85-
}
86-
return found;
87-
}
88-
89-
private bool Find(Node nodeP, T dataP)
90-
{
91-
bool found = false;
92-
if (nodeP != null)
93-
{
94-
if (nodeP.Data.Equals(dataP))
95-
{
96-
found = true;
97-
}
98-
else
99-
{
100-
if (dataP.CompareTo(nodeP.Data) < 0) // dataP < nodeP.Data
101-
{
102-
found = Find(nodeP.left, dataP);
103-
}
104-
else if (dataP.CompareTo(nodeP.Data) > 0) // dataP > nodeP.Data
105-
{
106-
found = Find(nodeP.right, dataP);
107-
}
108-
}
109-
}
110-
return found;
111-
}
112-
113-
public T FindMin()
114-
{
115-
if (root == null)
116-
{
117-
throw new ApplicationException(
118-
"Cannot find a value in an empty tree!"
119-
);
120-
}
121-
else
122-
{
123-
return FindMin(root);
124-
}
125-
}
144+
// Done with deletion.
126145

127-
private T FindMin(Node nodeP)
128-
{
129-
T minValue;
130-
if (nodeP.left == null)
131-
{
132-
minValue = nodeP.Data;
133-
}
134-
else
135-
{
136-
minValue = FindMin(nodeP.left);
137-
}
138-
return minValue;
139-
}
146+
140147
}

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

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,17 +62,34 @@ public int Depth()
6262

6363
private int Depth(Node nodeP, int depth)
6464
{
65+
// "Unless proven otherwise",
66+
// we assume that the depth of the
67+
// node is the depth it received
68+
// as argument.
6569
int result = depth;
70+
// We assume the depth of
71+
// its right sub-tree
72+
// is 0.
6673
int depthL = 0;
6774
if (nodeP.left != null)
6875
{
76+
// If its left sub-tree is not null,
77+
// we inquire about its depth,
78+
// knowing that it will be 1 more
79+
// than the depth of the current node.
6980
depthL = Depth(nodeP.left, result + 1);
7081
}
71-
int depthR = 0;
82+
// We proceed similarly for the
83+
// left sub-tree.
84+
int depthR = 0;
7285
if (nodeP.right != null)
7386
{
7487
depthR = Depth(nodeP.right, result + 1);
7588
}
89+
// Finally, if at least one sub-tree
90+
// is not null, we take their max to
91+
// be the depth of the tree starting
92+
// with our current node.
7693
if (nodeP.left != null || nodeP.right != null)
7794
{
7895
result = Math.Max(depthL, depthR);
@@ -114,7 +131,7 @@ private bool Find(Node nodeP, T dataP)
114131

115132
/* Traversal methods. */
116133
// Inorder traversal
117-
// "Left, root, right"
134+
// "Left, data, right"
118135
public string TrasverseI()
119136
{
120137
string returned = "";
@@ -138,7 +155,7 @@ private string TraverseI(Node nodeP)
138155
}
139156

140157
// Preorder traversal
141-
// "Root, left, right"
158+
// "Data, left, right"
142159
public string TrasversePr()
143160
{
144161
string returned = "";
@@ -162,7 +179,7 @@ private string TraversePr(Node nodeP)
162179
}
163180

164181
// Postorder traversal
165-
// "Left, right, root"
182+
// "Left, right, data"
166183
public string TrasversePo()
167184
{
168185
string returned = "";
@@ -185,12 +202,11 @@ private string TraversePo(Node nodeP)
185202
return returned;
186203
}
187204

205+
/* Done with traversal methods. */
206+
188207
public abstract void Insert(T dataP);
189208
public abstract bool Delete(T dataP);
190209

191-
192-
193-
194210
// The ToString method is simply here to help debug.
195211

196212
public override string ToString()

source/diag/gra/bstree_example_1.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
%%% A simple binary tree
2+
13
A((10))-->B((7))
24
B-->C((5))
35
B-->D((8))
Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,42 @@
1+
%%% A binary tree with depth illustrated
2+
3+
A((10))-->B((7))
4+
B-->C((5))
5+
B-->D((8))
6+
A-->E((11))
7+
E~~~H0:::hidden
8+
E-->F((30))
9+
F-->G((12))
10+
G~~~H1:::hidden
11+
G-->I((15))
12+
F~~~H2:::hidden
13+
114
d1:::depthClass
215
d2:::depthClass
316
d3:::depthClass
417
d4:::depthClass
518
d5:::depthClass
619

720
subgraph d1 [Depth 0]
8-
A((10))
21+
A
922
end
1023
subgraph d2 [Depth 1]
11-
B((7))
12-
E((11))
24+
B
25+
E
1326
end
1427
subgraph d3 [Depth 2]
15-
C((5))
16-
D((8))
17-
F((30))
28+
C
29+
D
30+
F
31+
H0
1832
end
1933
subgraph d4 [Depth 3]
20-
G((12))
34+
H2
35+
G
2136
end
2237
subgraph d5 [Depth 4]
23-
H((15))
38+
H1
39+
I
2440
end
2541

26-
A-->B
27-
B-->C
28-
B-->D
29-
A-->E
30-
E~~~H0:::hidden
31-
E-->F
32-
F-->G
33-
G~~~H2:::hidden
34-
G-->H
35-
F~~~H3:::hidden
36-
37-
classDef depthClass width:30em, height:4em, display:flex, justify-content: flex-start, align-items:flex-end;
42+
classDef depthClass width:23em, height:4em, display:flex, justify-content: flex-start, align-items:flex-end;
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
%%% A Binary tree with explicit `null`
2+
13
A((10))-->B((7))
24
B-->C((5))
35
B-->D((8))
46
A-->E((11))
57
E-->N1((null))
68
E-->F((30))
79
F-->G((12))
8-
G--->N2((null))
10+
G-->N2((null))
911
G-->H1((15))
10-
F--->N3((null))
12+
F-->N3((null))

source/docs/about/dev_guide.md

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,8 @@ The resulting images are added to the repository so that there is no need to re-
227227
The diagrams are created using [Mermaid](https://mermaid.js.org/) and located in `source/diag`.
228228
Note that because of [an annoying bug](https://github.com/mermaid-js/mermaid-cli/issues/730) present on github's server, mermaid-cli [must call pupeeter with the `--no-sandbox`](https://github.com/mermaid-js/mermaid-cli/blob/340561040b6b0621a486e3fc96723139e5718268/docs/linux-sandbox-issue.md) option, which constitutes a potential safety issue.
229229

230+
To auto-reload the images when they are re-built, we recommend using sxiv.
231+
230232
##### Class Diagrams
231233

232234
The *class* UML diagrams are created using [Mermaid](https://mermaid.js.org/) and located in `source/diag/cla`.
@@ -241,12 +243,25 @@ To create a new class diagram, say for a `Documentation` class, follow those ste
241243

242244
The *flowchart* diagrams are created using [Mermaid](https://mermaid.js.org/) and located in `source/diag/flo`.
243245

244-
To create a new class diagram, say for a `Documentation` class, follow those steps:
246+
To create a new flowchart diagram, follow those steps:
247+
248+
1. Create a `Cell.txt` file in `source/diag/flo` that follows [the syntax for flowchart diagrams](https://mermaid.js.org/syntax/flowchart.html) (note that there is no need to add `flowchart` at the beginning, it will be done automatically),
249+
2. Add on the first line a comment `%% title` with "title" the title of the diagram,
250+
3. Run (from the `source/` folder) `make diag/flo/Cell.md`,
251+
4. Integrate the resulting drawing, properly captioned and with links to `Cell.txt`, `Cell.svg` and `Cell.png` files using `!include diag/flo/Cell.md`.
245252

246-
1. Create a `Documentation.txt` file in `source/diag/flo` that follows [the syntax for flowchart diagrams](https://mermaid.js.org/syntax/flowchart.html) (note that there is no need to add `flowchart` at the beginning, it will be done automatically),
253+
##### Graph Diagrams
254+
255+
The *graph* diagrams are created using [Mermaid](https://mermaid.js.org/) and located in `source/diag/gra`: note that while `graph` and `flowchart` are [synonyms in mermaid](https://mermaid.js.org/syntax/flowchart.html#a-node-default), we separate them to apply different styles to them.
256+
Graphs are used to represent binary trees.
257+
258+
To create a new class diagram, say for a `BTree_Example`, follow those steps:
259+
260+
1. Create a `BTree_Example.txt` file in `source/diag/gra` that follows [the syntax for flowchart diagrams](https://mermaid.js.org/syntax/flowchart.html) (note that there is no need to add `flowchart` at the beginning, it will be done automatically),
247261
2. Add on the first line a comment `%% title` with "title" the title of the diagram,
248-
3. Run (from the `source/` folder) `make diag/flo/Documentation.md`,
249-
4. Integrate the resulting drawing, properly captioned and with links to `Documentation.txt`, `Documentation.svg` and `Documentation.png` files using `!include diag/flo/Documentation.md`.
262+
3. Run (from the `source/` folder) `make diag/gra/BTree_Example.md`,
263+
4. Integrate the resulting drawing, properly captioned and with links to `BTree_Example.txt`, `BTree_Example.svg` and `BTree_Example.png` files using `!include diag/flo/BTree_Example.md`.
264+
250265

251266
#### Source code
252267

0 commit comments

Comments
 (0)