Skip to content

Commit 470577d

Browse files
ClémentClément
authored andcommitted
Fixing layout for solutions to exam #1
1 parent 40583f3 commit 470577d

2 files changed

Lines changed: 118 additions & 62 deletions

File tree

source/solutions/data/queues.md

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,25 @@ tags:
88
## Multiple Choices
99

1010
#. A queue is generally endowed with operations called…
11-
12-
- [ ] Requeue
13-
- [x] Dequeue
14-
- [x] Enqueue
15-
- [ ] Unqueue
11+
12+
- [ ] Requeue
13+
- [x] Dequeue
14+
- [x] Enqueue
15+
- [ ] Unqueue
1616
1717
#. A queue implements which principle?
1818

19-
- [ ] LIFO
20-
- [x] FIFO
21-
- [ ] LILO
22-
- [ ] FOFI
19+
- [ ] LIFO
20+
- [x] FIFO
21+
- [ ] LILO
22+
- [ ] FOFI
2323

2424
#. LIFO stands for…
25-
26-
- [ ] Least Is First Out
27-
- [ ] Last Is First Outside
28-
- [x] Last In First Out
29-
- [ ] Low Input Fast Output
25+
26+
- [ ] Least Is First Out
27+
- [ ] Last Is First Outside
28+
- [x] Last In First Out
29+
- [ ] Low Input Fast Output
3030

3131
## Exercises
3232

source/solutions/data/trees.md

Lines changed: 104 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ tags:
5050
<summary>Solution</summary>
5151
One among the following:
5252
53-
- Inorder displays first the left subtree, then the data at the root, then the right subtree.
54-
- Preorder displays first the data at the root, then the left subtree, then the right subtree.
55-
- Postorder displays first the left subtree, then the right subtree, then the data at the root.
53+
- Inorder traversal processes (recursively) first the left subtree, then the data at the root, then the right subtree.
54+
- Preorder traversal processes (recursively) first the data at the root, then the left subtree, then the right subtree.
55+
- Postorder traversal processes (recursively) first the left subtree, then the right subtree, then the data at the root.
5656

5757
</details>
5858
@@ -68,25 +68,85 @@ tags:
6868
- Postorder gives 6, 14, 12, 13, 10
6969
</details>
7070

71-
#. Consider the implementation of "random" binary trees shared on page \pageref{code:rbtree}, and note that the `Insert(T dataP, Node nodeP)` method uses the `gen.NextDouble() > 0.5` test that will be randomly `true` half of the time, and `false` the other half.
71+
#. Consider the following implementation of "random" binary trees:
7272

73+
```
74+
public class RBTree<T>
75+
{
76+
77+
private class Node
78+
{
79+
public T Data { get; set; }
80+
public Node left;
81+
public Node right;
82+
public Node(
83+
T dataP = default(T),
84+
Node leftP = null,
85+
Node rightP = null
86+
)
87+
{
88+
Data = dataP;
89+
left = leftP;
90+
right = rightP;
91+
}
92+
}
93+
94+
private Node root;
95+
96+
public RBTree()
97+
{
98+
root = null;
99+
}
100+
101+
public void Insert(T dataP)
102+
{
103+
root = Insert(dataP, root);
104+
}
105+
106+
private Node Insert(T dataP, Node nodeP)
107+
{
108+
if (nodeP == null)
109+
{
110+
return new Node(dataP, null, null);
111+
}
112+
else
113+
{
114+
Random gen = new Random();
115+
if(gen.NextDouble() > 0.5)
116+
{
117+
nodeP.left = Insert(dataP, nodeP.left);
118+
}
119+
else
120+
{
121+
nodeP.right = Insert(dataP, nodeP.right);
122+
}
123+
}
124+
return nodeP;
125+
}
126+
}
127+
```
128+
129+
Note that the `Insert(T dataP, Node nodeP)` method uses the `gen.NextDouble() > 0.5` test that will be randomly `true` half of the time, and `false` the other half.
130+
131+
73132
#. Explain the `T dataP = default(T)` part of the `Node` constructor.
74133
<details>
75134
<summary>Solution</summary>
76-
This makes the first argument of the constructor optional: if no value is provided, then the default value for `T` is used.
77-
For example, for `int`, then 0 would be used.
135+
This makes the first argument of the constructor optional: if no value is provided, then the default value for `T` is used.
136+
For example, for `int`, then 0 would be used.
78137
</details>
79138
#. Write a `ToString` method for the `Node` class, remembering that only a node `Data` needs to be part of the `string` returned.
80-
<details>
81-
<summary>Solution</summary>
82-
```
83-
public override string ToString()
84-
{
85-
return Data.ToString();
86-
}
87-
```
88-
</details>
89-
139+
140+
<details>
141+
<summary>Solution</summary>
142+
```
143+
public override string ToString()
144+
{
145+
return Data.ToString();
146+
}
147+
```
148+
</details>
149+
90150
#. Write a series of statements that would
91151
#. create a `RBTree` object,
92152
#. insert the values 1, 2, 3, and 4 in it (in this order).
@@ -105,47 +165,43 @@ tags:
105165
#. Make a drawing of a possible `RBTree` obtained by executing your code.
106166
<details>
107167
<summary>Solution</summary>
108-
Any binary tree containing 1, 2, 3 and 4, with 1 at the root, is correct. One such example is:
168+
Any binary tree containing 1, 2, 3 and 4, with 1 at the root, 2 a child of 1, 3 a child of 1 or 2, and 4 a child of 1, 2 or 3, is correct. One such example is:
109169
110-
!include diag/gra/bstree_example_5.md
170+
!include diag/gra/bstree_example_5.md
111171
</details>
112172

113173
#. Write a `Find` method that takes one argument `dataP` of type `T` and returns `true` if `dataP` is in the `RBtree` calling object, `false` otherwise.
114174

115-
<details>
116-
<summary>Solution</summary>
117-
```
118-
public bool Find(T dataP)
175+
<details>
176+
<summary>Solution</summary>
177+
```
178+
public bool Find(T dataP)
179+
{
180+
bool found = false;
181+
if (root != null)
119182
{
120-
bool found = false;
121-
if (root != null)
122-
{
123-
found = Find(root, dataP);
124-
}
125-
return found;
183+
found = Find(root, dataP);
126184
}
185+
return found;
186+
}
127187

128-
private bool Find(Node nodeP, T dataP)
188+
private bool Find(Node nodeP, T dataP)
189+
{
190+
bool found = false;
191+
if (nodeP != null)
129192
{
130-
bool found = false;
131-
if (nodeP != null)
193+
if (nodeP.Data.Equals(dataP))
194+
{
195+
found = true;
196+
}
197+
else
132198
{
133-
if (nodeP.Data.Equals(dataP))
134-
{
135-
found = true;
136-
}
137-
else
138-
{
139-
found =
140-
Find(nodeP.left, dataP)
141-
|| Find(nodeP.right, dataP);
142-
}
199+
found =
200+
Find(nodeP.left, dataP)
201+
|| Find(nodeP.right, dataP);
143202
}
144-
return found;
145203
}
146-
```
147-
</details>
148-
149-
150-
151-
204+
return found;
205+
}
206+
```
207+
</details>

0 commit comments

Comments
 (0)