3
3
//! expect to modify the tree (e.g. `insert` or `delete`) instead return
4
4
//! a new tree that reference many of the nodes of the original tree.
5
5
//!
6
- //! To avoid copious `Rc`ing, we do not implement a particularly efficient
7
- //! persistent structure - we only allow one tree at a time. Still, most
8
- //! of the algorithms are the same and there are useful lessons to learn!
6
+ //! # Examples
7
+ //!
8
+ //! ```
9
+ //! use bst::functional::Tree;
10
+ //!
11
+ //! let tree = Tree::new();
12
+ //!
13
+ //! // Nothing in here yet.
14
+ //! assert_eq!(tree.find(&1), None);
15
+ //!
16
+ //! // This `insert` returns a new tree!
17
+ //! let new_tree = tree.insert(1, 2);
18
+ //!
19
+ //! // The new tree has this new value but the old one doesn't.
20
+ //! assert_eq!(new_tree.find(&1), Some(&2));
21
+ //! assert_eq!(tree.find(&1), None);
22
+ //!
23
+ //! // Insert a new value for the same key gives yet another tree.
24
+ //! let newer_tree = new_tree.insert(1, 3);
25
+ //!
26
+ //! // And delete it for good measure.
27
+ //! let newest_tree = newer_tree.delete(&1);
28
+ //!
29
+ //! // All history is preserved.
30
+ //! assert_eq!(newest_tree.find(&1), None);
31
+ //! assert_eq!(newer_tree.find(&1), Some(&3));
32
+ //! assert_eq!(new_tree.find(&1), Some(&2));
33
+ //! assert_eq!(tree.find(&1), None);
34
+ //! ```
9
35
10
36
use std:: cmp;
37
+ use std:: rc:: Rc ;
11
38
12
39
/// A Binary Search Tree. This can be used for inserting, finding,
13
40
/// and deleting keys and values. Note that this data structure is
@@ -34,37 +61,46 @@ impl<K, V> Tree<K, V> {
34
61
}
35
62
36
63
/// Returns a new tree that includes a node
37
- /// containing the given key and value
64
+ /// containing the given key and value.
38
65
///
39
66
/// # Examples
40
67
///
41
68
/// ```
42
69
/// use bst::functional::Tree;
43
70
///
44
- /// let mut tree = Tree::new();
45
- /// tree = tree.insert(1, 2);
46
- ///
47
- /// assert_eq!(tree.find(&1), Some(&2));
48
- ///
49
- /// tree = tree.insert(1, 3);
71
+ /// let tree = Tree::new();
72
+ /// let new_tree = tree.insert(1, 2);
73
+ /// let newer_tree = new_tree.insert(1, 3);
50
74
///
51
- /// assert_eq!(tree.find(&1), Some(&3));
75
+ /// // All history is preserved.
76
+ /// assert_eq!(newer_tree.find(&1), Some(&3));
77
+ /// assert_eq!(new_tree.find(&1), Some(&2));
78
+ /// assert_eq!(tree.find(&1), None);
52
79
/// ```
53
- pub fn insert ( self , key : K , value : V ) -> Self
80
+ pub fn insert ( & self , key : K , value : V ) -> Self
54
81
where
55
82
K : cmp:: Ord ,
56
83
{
57
84
match self {
58
85
Tree :: Leaf => Tree :: Node ( Node :: new ( key, value) ) ,
59
86
Tree :: Node ( n) => match key. cmp ( & n. key ) {
60
87
cmp:: Ordering :: Less => Tree :: Node ( Node {
61
- left : Box :: new ( n. left . insert ( key, value) ) ,
62
- ..n
88
+ left : Rc :: new ( n. left . insert ( key, value) ) ,
89
+ key : Rc :: clone ( & n. key ) ,
90
+ right : Rc :: clone ( & n. right ) ,
91
+ value : Rc :: clone ( & n. value ) ,
92
+ } ) ,
93
+ cmp:: Ordering :: Equal => Tree :: Node ( Node {
94
+ value : Rc :: new ( value) ,
95
+ key : Rc :: clone ( & n. key ) ,
96
+ left : Rc :: clone ( & n. left ) ,
97
+ right : Rc :: clone ( & n. right ) ,
63
98
} ) ,
64
- cmp:: Ordering :: Equal => Tree :: Node ( Node { value, ..n } ) ,
65
99
cmp:: Ordering :: Greater => Tree :: Node ( Node {
66
- right : Box :: new ( n. right . insert ( key, value) ) ,
67
- ..n
100
+ right : Rc :: new ( n. right . insert ( key, value) ) ,
101
+ key : Rc :: clone ( & n. key ) ,
102
+ left : Rc :: clone ( & n. left ) ,
103
+ value : Rc :: clone ( & n. value ) ,
68
104
} ) ,
69
105
} ,
70
106
}
@@ -79,8 +115,8 @@ impl<K, V> Tree<K, V> {
79
115
/// ```
80
116
/// use bst::functional::Tree;
81
117
///
82
- /// let mut tree = Tree::new();
83
- /// tree = tree.insert(1, 2);
118
+ /// let tree = Tree::new();
119
+ /// let tree = tree.insert(1, 2);
84
120
///
85
121
/// assert_eq!(tree.find(&1), Some(&2));
86
122
/// assert_eq!(tree.find(&42), None);
@@ -109,86 +145,117 @@ impl<K, V> Tree<K, V> {
109
145
/// ```
110
146
/// use bst::functional::Tree;
111
147
///
112
- /// let mut tree = Tree::new();
113
- /// tree = tree.insert(1, 2);
114
- ///
115
- /// tree = tree.delete(&1);
148
+ /// let tree = Tree::new();
149
+ /// let tree = tree.insert(1, 2);
150
+ /// let newer_tree = tree.delete(&1);
116
151
///
117
- /// assert_eq!(tree.find(&1), None);
152
+ /// // All history is preserved.
153
+ /// assert_eq!(newer_tree.find(&1), None);
154
+ /// assert_eq!(tree.find(&1), Some(&2));
118
155
/// ```
119
- pub fn delete ( self , k : & K ) -> Self
156
+ pub fn delete ( & self , k : & K ) -> Self
120
157
where
121
158
K : cmp:: Ord ,
122
159
{
123
160
match self {
124
- Tree :: Leaf => self ,
161
+ Tree :: Leaf => Tree :: Leaf ,
125
162
Tree :: Node ( n) => match k. cmp ( & n. key ) {
126
163
cmp:: Ordering :: Less => Tree :: Node ( Node {
127
- left : Box :: new ( n. left . delete ( k) ) ,
128
- ..n
164
+ left : Rc :: new ( n. left . delete ( k) ) ,
165
+ key : Rc :: clone ( & n. key ) ,
166
+ right : Rc :: clone ( & n. right ) ,
167
+ value : Rc :: clone ( & n. value ) ,
129
168
} ) ,
130
- cmp:: Ordering :: Equal => match ( * n. left , * n. right ) {
131
- ( Tree :: Leaf , right_child) => right_child,
132
- ( left_child, Tree :: Leaf ) => left_child,
169
+ cmp:: Ordering :: Equal => match ( n. left . as_ref ( ) , n. right . as_ref ( ) ) {
170
+ ( Tree :: Leaf , Tree :: Leaf ) => Tree :: Leaf ,
171
+ ( Tree :: Leaf , Tree :: Node ( right) ) => Tree :: Node ( right. clone ( ) ) ,
172
+ ( Tree :: Node ( left) , Tree :: Leaf ) => Tree :: Node ( left. clone ( ) ) ,
133
173
134
174
// If we have two children we have to figure out
135
175
// which node to promote. We choose here this node's
136
176
// predecessor. That is, the largest node in this node's
137
177
// left subtree.
138
- ( Tree :: Node ( left_child) , right_child ) => {
178
+ ( Tree :: Node ( left_child) , _ ) => {
139
179
let ( pred_key, pred_val, new_left) = left_child. delete_largest ( ) ;
140
180
Tree :: Node ( Node {
141
181
left : new_left,
142
- right : Box :: new ( right_child ) , // I really don't want this allocation here
182
+ right : Rc :: clone ( & n . right ) ,
143
183
key : pred_key,
144
184
value : pred_val,
145
185
} )
146
186
}
147
187
} ,
148
188
cmp:: Ordering :: Greater => Tree :: Node ( Node {
149
- right : Box :: new ( n. right . delete ( k) ) ,
150
- ..n
189
+ right : Rc :: new ( n. right . delete ( k) ) ,
190
+ key : Rc :: clone ( & n. key ) ,
191
+ left : Rc :: clone ( & n. left ) ,
192
+ value : Rc :: clone ( & n. value ) ,
151
193
} ) ,
152
194
} ,
153
195
}
154
196
}
155
197
}
156
198
157
- /// A `Node` tree has a key that is used for searching/sorting and a value
199
+ /// A `Node` has a key that is used for searching/sorting and a value
158
200
/// that is associated with that key. It always has two children although
159
201
/// those children may be [`Leaf`][Tree::Leaf]s.
160
202
pub struct Node < K , V > {
161
- key : K ,
162
- value : V ,
163
- left : Box < Tree < K , V > > ,
164
- right : Box < Tree < K , V > > ,
203
+ key : Rc < K > ,
204
+ value : Rc < V > ,
205
+ left : Rc < Tree < K , V > > ,
206
+ right : Rc < Tree < K , V > > ,
207
+ }
208
+
209
+ /// Manual implementation of `Clone` so we don't clone references when the generic parameters
210
+ /// aren't `Clone` themselves.
211
+ ///
212
+ /// Note the comment on generic structs in
213
+ /// [the docs][<https://doc.rust-lang.org/std/clone/trait.Clone.html#derivable>].
214
+ impl < K , V > Clone for Node < K , V > {
215
+ fn clone ( & self ) -> Self {
216
+ Self {
217
+ key : Rc :: clone ( & self . key ) ,
218
+ left : Rc :: clone ( & self . left ) ,
219
+ right : Rc :: clone ( & self . right ) ,
220
+ value : Rc :: clone ( & self . value ) ,
221
+ }
222
+ }
165
223
}
166
224
167
225
impl < K , V > Node < K , V > {
226
+ /// Construct a new `Node` with the given `key` and `value.
168
227
fn new ( key : K , value : V ) -> Self {
169
228
Self {
170
- key,
171
- value,
172
- left : Box :: new ( Tree :: Leaf ) ,
173
- right : Box :: new ( Tree :: Leaf ) ,
229
+ key : Rc :: new ( key ) ,
230
+ value : Rc :: new ( value ) ,
231
+ left : Rc :: new ( Tree :: Leaf ) ,
232
+ right : Rc :: new ( Tree :: Leaf ) ,
174
233
}
175
234
}
176
235
177
- /// Returns the largest node and a new subtree
178
- /// without that largest node.
179
- fn delete_largest ( self ) -> ( K , V , Box < Tree < K , V > > )
236
+ /// Returns the key and value of the largest node and a new subtree without that largest node.
237
+ fn delete_largest ( & self ) -> ( Rc < K > , Rc < V > , Rc < Tree < K , V > > )
180
238
where
181
239
K : cmp:: Ord ,
182
240
{
183
- match * self . right {
184
- Tree :: Leaf => ( self . key , self . value , self . left ) ,
241
+ match self . right . as_ref ( ) {
242
+ Tree :: Leaf => (
243
+ Rc :: clone ( & self . key ) ,
244
+ Rc :: clone ( & self . value ) ,
245
+ Rc :: clone ( & self . left ) ,
246
+ ) ,
185
247
Tree :: Node ( r) => {
186
248
let ( key, value, sub) = r. delete_largest ( ) ;
187
249
188
250
(
189
251
key,
190
252
value,
191
- Box :: new ( Tree :: Node ( Node { right : sub, ..self } ) ) ,
253
+ Rc :: new ( Tree :: Node ( Node {
254
+ right : sub,
255
+ key : Rc :: clone ( & self . key ) ,
256
+ left : Rc :: clone ( & self . left ) ,
257
+ value : Rc :: clone ( & self . value ) ,
258
+ } ) ) ,
192
259
)
193
260
}
194
261
}
0 commit comments