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
- 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.
56
56
57
57
</details>
58
58
@@ -68,25 +68,85 @@ tags:
68
68
- Postorder gives 6, 14, 12, 13, 10
69
69
</details>
70
70
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:
72
72
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
+
73
132
#. Explain the `T dataP = default(T)` part of the `Node` constructor.
74
133
<details>
75
134
<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.
78
137
</details>
79
138
#. 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
+
90
150
#. Write a series of statements that would
91
151
#. create a `RBTree` object,
92
152
#. insert the values 1, 2, 3, and 4 in it (in this order).
@@ -105,47 +165,43 @@ tags:
105
165
#. Make a drawing of a possible `RBTree` obtained by executing your code.
106
166
<details>
107
167
<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:
109
169
110
-
!include diag/gra/bstree_example_5.md
170
+
!include diag/gra/bstree_example_5.md
111
171
</details>
112
172
113
173
#. 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.
0 commit comments