-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimpl-bin-tree.html
More file actions
423 lines (380 loc) · 36.8 KB
/
impl-bin-tree.html
File metadata and controls
423 lines (380 loc) · 36.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<!-- 2022-W11-4 02:12 -->
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Implementing Binary Tree Sets</title>
<meta name="author" content="Inanna" />
<meta name="description" content="Making sets with binary trees." />
<meta name="generator" content="Org Mode" />
<link href="/site.css" rel="stylesheet" type="text/css" /><link href="images/website-icon.png" rel="icon" />
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
displayAlign: "center",
displayIndent: "0em",
"HTML-CSS": { scale: 100,
linebreaks: { automatic: "false" },
webFont: "TeX"
},
SVG: {scale: 100,
linebreaks: { automatic: "false" },
font: "TeX"},
NativeMML: {scale: 100},
TeX: { equationNumbers: {autoNumber: "AMS"},
MultLineWidth: "85%",
TagSide: "right",
TagIndent: ".8em"
}
});
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js?config=TeX-AMS_HTML"></script>
</head>
<body>
<div id="preamble" class="status">
<div><a href="/index.html"><img alt="An abstract logo representing a series of three assembly line stamping machines with the words CONS, DEV, and embalzoned in white on each machine." id="site-logo" src="/images/website-logo.png" /></a></div>
</div>
<div id="content" class="content">
<h1 class="title">Implementing Binary Tree Sets</h1>
<div id="table-of-contents" role="doc-toc">
<h2>Table of Contents</h2>
<div id="text-table-of-contents" role="doc-toc">
<ul>
<li><a href="#orge3753df">Introduction</a></li>
<li><a href="#org2894206">Implementation</a>
<ul>
<li><a href="#orgb5fc350">Object and Constructor</a></li>
<li><a href="#orgf7f858c">Insert</a></li>
<li><a href="#org08f4a50">Contains</a></li>
<li><a href="#org4620877">Removing an Element</a></li>
<li><a href="#org5a01077">Traversals</a></li>
<li><a href="#org6c76d55">Set-theoretic</a>
<ul>
<li><a href="#org83f1931">Union</a></li>
<li><a href="#orgf0e806f">Intersection</a></li>
<li><a href="#orga6b8a07">Difference</a></li>
<li><a href="#orgedef36a">Subset</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#org769979e">Python Code</a></li>
</ul>
</div>
</div>
<div id="outline-container-orge3753df" class="outline-2">
<h2 id="orge3753df">Introduction</h2>
<div class="outline-text-2" id="text-orge3753df">
<p>
Binary trees are a classic datastructure, basically like a linked list but with two children nodes rather than one node. Each of the child nodes can then link to further child nodes and so on until they reach leaf nodes, which are traditionally <code>nil</code>.
</p>
<div id="org374bce2" class="figure">
<p><img src="./images/bintree_17a848ae-1c8a-4a7c-8219-df9f2a794fb3.png" alt="A binary tree." />
</p>
</div>
<p>
You can also use them for <a href="https://en.wikipedia.org/wiki/Set_(mathematics)">sets</a> which have a simple, linear space complexity and are fairly space efficient when compared to hash-sets. However, they do have longer access times with \(O(log(n))\) being the standard for most operations.
</p>
</div>
</div>
<div id="outline-container-org2894206" class="outline-2">
<h2 id="org2894206">Implementation</h2>
<div class="outline-text-2" id="text-org2894206">
</div>
<div id="outline-container-orgb5fc350" class="outline-3">
<h3 id="orgb5fc350">Object and Constructor</h3>
<div class="outline-text-3" id="text-orgb5fc350">
<p>
To start it all off we define an object and a constructor. The constructor just takes a seq-able collection and converts it into a binary tree. This only works if elements within the collection can be compared with other elements, otherwise it will return an error.
</p>
<div class="org-src-container">
<pre class="src src-clojure"><span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">defrecord</span> <span style="color: #E53935;">BintreeNode</span> <span style="color: #bbb;">[</span>val lesser greater<span style="color: #bbb;">]</span><span style="color: #494949;">)</span>
<span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">defn</span> <span style="font-weight: bold; font-style: italic;">bintree-set</span> <span style="color: #bbb;">[</span>coll<span style="color: #bbb;">]</span>
<span style="color: #bbb;">(</span>reduce bintree-insert <span style="color: #E53935;">nil</span> coll<span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
</pre>
</div>
</div>
</div>
<div id="outline-container-orgf7f858c" class="outline-3">
<h3 id="orgf7f858c">Insert</h3>
<div class="outline-text-3" id="text-orgf7f858c">
<p>
To insert an item we simply go down the binary tree until we find the item or nil.
</p>
<p>
So to illustrate, let's imagine we added <code>7</code> to the tree from above:
</p>
<div id="org5502ca4" class="figure">
<p><img src="./images/bintree-insert_17a848ae-1c8a-4a7c-8219-df9f2a794fb3.png" alt="A binary tree with the number 7 added to it." />
</p>
</div>
<p>
The algorithm is trivial. If a key is present nothing has to be done! It can just return. If the node is nil, then it is at a leaf node and it can insert a new node. Lastly, if it is at a node it checks itself against the value of the node and chooses the lesser (left) side if it is lesser, else the right side and applies itself.
</p>
<div class="org-src-container">
<pre class="src src-clojure"><span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">defn</span> <span style="font-weight: bold; font-style: italic;">bintree-insert</span> <span style="color: #bbb;">[</span><span style="color: #494949;">{</span><span style="color: #E53935;">:keys</span> <span style="color: #bbb;">[</span>val lesser greater<span style="color: #bbb;">]</span> <span style="color: #E53935;">:as</span> node<span style="color: #494949;">}</span> key<span style="color: #bbb;">]</span>
<span style="color: #bbb;">(</span><span style="color: #E53935; font-style: italic;">cond</span> <span style="color: #494949;">(</span>= val key<span style="color: #494949;">)</span> node
<span style="color: #494949;">(</span>nil? node<span style="color: #494949;">)</span> <span style="color: #494949;">(</span>->BintreeNode key <span style="color: #E53935;">nil</span> <span style="color: #E53935;">nil</span><span style="color: #494949;">)</span>
<span style="color: #494949;">(</span>< key val<span style="color: #494949;">)</span> <span style="color: #494949;">(</span>assoc node <span style="color: #E53935;">:lesser</span> <span style="color: #bbb;">(</span>bintree-insert lesser key<span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
<span style="color: #E53935;">:else</span> <span style="color: #494949;">(</span>assoc node <span style="color: #E53935;">:greater</span> <span style="color: #bbb;">(</span>bintree-insert greater key<span style="color: #bbb;">)</span><span style="color: #494949;">)</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
</pre>
</div>
</div>
</div>
<div id="outline-container-org08f4a50" class="outline-3">
<h3 id="org08f4a50">Contains</h3>
<div class="outline-text-3" id="text-org08f4a50">
<p>
To determine if the value 23 is contained within our tree we can simply compare each node, if it returns true, also return true. But if not we continue down the tree, choosing the lesser side if it is lesser, greater side if it is greater.
</p>
<div id="org883ef7b" class="figure">
<p><img src="./images/bintree-contains_17a848ae-1c8a-4a7c-8219-df9f2a794fb3.png" alt="A binary tree with the number 23 added to it." />
</p>
</div>
<p>
The implementation, therefore, is almost exactly the same as the way we inserted an element into the tree, traversing until it hits the key itself or nil.
</p>
<div class="org-src-container">
<pre class="src src-clojure"><span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">defn</span> <span style="font-weight: bold; font-style: italic;">bintree-contains?</span> <span style="color: #bbb;">[</span><span style="color: #494949;">{</span><span style="color: #E53935;">:keys</span> <span style="color: #bbb;">[</span>val lesser greater<span style="color: #bbb;">]</span><span style="color: #494949;">}</span> key<span style="color: #bbb;">]</span>
<span style="color: #bbb;">(</span><span style="color: #E53935; font-style: italic;">cond</span> <span style="color: #494949;">(</span>= val key<span style="color: #494949;">)</span> <span style="color: #E53935;">true</span>
<span style="color: #494949;">(</span>nil? val<span style="color: #494949;">)</span> <span style="color: #E53935;">false</span>
<span style="color: #494949;">(</span>< key val<span style="color: #494949;">)</span> <span style="color: #494949;">(</span>bintree-contains? lesser key<span style="color: #494949;">)</span>
<span style="color: #E53935;">:else</span> <span style="color: #494949;">(</span>bintree-contains? greater key<span style="color: #494949;">)</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
</pre>
</div>
</div>
</div>
<div id="outline-container-org4620877" class="outline-3">
<h3 id="org4620877">Removing an Element</h3>
<div class="outline-text-3" id="text-org4620877">
<p>
Now you may wonder, how do we remove an element from the tree? Well it's pretty simple. First you find the element in the tree (as usual) and then you make one of the children of the node the node and insert the contents of the other child into that node. A good way to do this <i>would be</i> to simply take the leftmost branch of then new node and then insert there when you find a nil.
</p>
<div class="org-src-container">
<pre class="src src-clojure">
<span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">ns</span> <span style="color: #E53935;">bintree-demo</span><span style="color: #494949;">)</span>
<span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">defn</span> <span style="font-weight: bold; font-style: italic;">bintree-remove</span> <span style="color: #bbb;">[</span><span style="color: #494949;">{</span><span style="color: #E53935;">:keys</span> <span style="color: #bbb;">[</span>val lesser greater<span style="color: #bbb;">]</span> <span style="color: #E53935;">:as</span> node<span style="color: #494949;">}</span> key<span style="color: #bbb;">]</span>
<span style="color: #bbb;">(</span><span style="color: #E53935; font-style: italic;">letfn</span> <span style="color: #494949;">[</span><span style="color: #bbb;">(</span>insert-least <span style="color: #494949;">[</span><span style="color: #bbb;">{</span><span style="color: #E53935;">:keys</span> <span style="color: #494949;">[</span>lesser<span style="color: #494949;">]</span> <span style="color: #E53935;">:as</span> node<span style="color: #bbb;">}</span> tree<span style="color: #494949;">]</span>
<span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">if</span> lesser <span style="color: #bbb;">(</span>assoc node <span style="color: #E53935;">:lesser</span> <span style="color: #494949;">(</span>insert-least lesser tree<span style="color: #494949;">)</span><span style="color: #bbb;">)</span> tree<span style="color: #494949;">)</span><span style="color: #bbb;">)</span><span style="color: #494949;">]</span>
<span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">cond</span> <span style="color: #bbb;">(</span>nil? val<span style="color: #bbb;">)</span> <span style="color: #E53935;">nil</span>
<span style="color: #bbb;">(</span>= val key<span style="color: #bbb;">)</span> <span style="color: #bbb;">(</span>insert-least greater lesser<span style="color: #bbb;">)</span>
<span style="color: #bbb;">(</span>< key val<span style="color: #bbb;">)</span> <span style="color: #bbb;">(</span>assoc node <span style="color: #E53935;">:lesser</span> <span style="color: #494949;">(</span>bintree-remove lesser key<span style="color: #494949;">)</span><span style="color: #bbb;">)</span>
<span style="color: #E53935;">:else</span> <span style="color: #bbb;">(</span>assoc node <span style="color: #E53935;">:greater</span> <span style="color: #494949;">(</span>bintree-remove greater key<span style="color: #494949;">)</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
</pre>
</div>
</div>
</div>
<div id="outline-container-org5a01077" class="outline-3">
<h3 id="org5a01077">Traversals</h3>
<div class="outline-text-3" id="text-org5a01077">
<p>
Here we define some traversals for our tree. Traversals are simply functions that recursively visit all the nodes in the tree once.
</p>
<p>
The pre-order traversal returns a list of all nodes with the visited node first, followed by it's lesser and then greater children. This in essence provides the order of insertions needed to generate another tree of the same structure.
</p>
<div class="org-src-container">
<pre class="src src-clojure"><span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">defn</span> <span style="font-weight: bold; font-style: italic;">pre-order</span> <span style="color: #bbb;">[</span><span style="color: #494949;">{</span><span style="color: #E53935;">:keys</span> <span style="color: #bbb;">[</span>val lesser greater<span style="color: #bbb;">]</span><span style="color: #494949;">}</span><span style="color: #bbb;">]</span>
<span style="color: #bbb;">(</span><span style="color: #E53935; font-style: italic;">when</span> val <span style="color: #494949;">(</span>concat <span style="color: #bbb;">[</span>val<span style="color: #bbb;">]</span> <span style="color: #bbb;">(</span>pre-order lesser<span style="color: #bbb;">)</span> <span style="color: #bbb;">(</span>pre-order greater<span style="color: #bbb;">)</span><span style="color: #494949;">)</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
</pre>
</div>
<p>
The post-order traversal does the same, but with the node last.
</p>
<div class="org-src-container">
<pre class="src src-clojure"><span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">defn</span> <span style="font-weight: bold; font-style: italic;">post-order</span> <span style="color: #bbb;">[</span><span style="color: #494949;">{</span><span style="color: #E53935;">:keys</span> <span style="color: #bbb;">[</span>val lesser greater<span style="color: #bbb;">]</span><span style="color: #494949;">}</span><span style="color: #bbb;">]</span>
<span style="color: #bbb;">(</span><span style="color: #E53935; font-style: italic;">when</span> val <span style="color: #494949;">(</span>concat <span style="color: #bbb;">(</span>pre-order lesser<span style="color: #bbb;">)</span> <span style="color: #bbb;">(</span>pre-order greater<span style="color: #bbb;">)</span> <span style="color: #bbb;">[</span>val<span style="color: #bbb;">]</span><span style="color: #494949;">)</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
</pre>
</div>
<p>
Finally, the in-order traversal returns a sorted list of all nodes by visiting the lesser nodes first, itself, and then the greater nodes.
</p>
<div class="org-src-container">
<pre class="src src-clojure"><span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">defn</span> <span style="font-weight: bold; font-style: italic;">in-order</span> <span style="color: #bbb;">[</span><span style="color: #494949;">{</span><span style="color: #E53935;">:keys</span> <span style="color: #bbb;">[</span>val lesser greater<span style="color: #bbb;">]</span><span style="color: #494949;">}</span><span style="color: #bbb;">]</span>
<span style="color: #bbb;">(</span><span style="color: #E53935; font-style: italic;">when</span> val <span style="color: #494949;">(</span>concat <span style="color: #bbb;">(</span>in-order lesser<span style="color: #bbb;">)</span> <span style="color: #bbb;">[</span>val<span style="color: #bbb;">]</span> <span style="color: #bbb;">(</span>in-order greater<span style="color: #bbb;">)</span><span style="color: #494949;">)</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
</pre>
</div>
</div>
</div>
<div id="outline-container-org6c76d55" class="outline-3">
<h3 id="org6c76d55">Set-theoretic</h3>
<div class="outline-text-3" id="text-org6c76d55">
<p>
Here we just implement the various set-theoretic functions.
</p>
</div>
<div id="outline-container-org83f1931" class="outline-4">
<h4 id="org83f1931">Union</h4>
<div class="outline-text-4" id="text-org83f1931">
<p>
The union function is fairly simple, obtaining the post-order traversal of the first binary tree and then inserting all the elements into the second. It's time complexity is \(O(n\ log(n))\).
</p>
<div class="org-src-container">
<pre class="src src-clojure"><span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">defn</span> <span style="font-weight: bold; font-style: italic;">bintree-union</span>
<span style="color: #bbb;">(</span><span style="color: #494949;">[</span>a b & rest<span style="color: #494949;">]</span> <span style="color: #494949;">(</span>reduce bintree-union a <span style="color: #bbb;">(</span>cons b rest<span style="color: #bbb;">)</span><span style="color: #494949;">)</span><span style="color: #bbb;">)</span>
<span style="color: #bbb;">(</span><span style="color: #494949;">[</span>a b<span style="color: #494949;">]</span>
<span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">->></span> a post-order <span style="color: #bbb;">(</span>reduce bintree-insert b<span style="color: #bbb;">)</span><span style="color: #494949;">)</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
</pre>
</div>
</div>
</div>
<div id="outline-container-orgf0e806f" class="outline-4">
<h4 id="orgf0e806f">Intersection</h4>
<div class="outline-text-4" id="text-orgf0e806f">
<p>
This just checks if a node is contained in the other tree, and if it is, adds it to the new set of nodes. It's time complexity is \(O(n\ log(n))\).
</p>
<div class="org-src-container">
<pre class="src src-clojure"><span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">defn</span> <span style="font-weight: bold; font-style: italic;">bintree-intersection</span>
<span style="color: #bbb;">(</span><span style="color: #494949;">[</span>a b & rest<span style="color: #494949;">]</span> <span style="color: #494949;">(</span>reduce bintree-difference a <span style="color: #bbb;">(</span>cons b rest<span style="color: #bbb;">)</span><span style="color: #494949;">)</span><span style="color: #bbb;">)</span>
<span style="color: #bbb;">(</span><span style="color: #494949;">[</span>a b<span style="color: #494949;">]</span>
<span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">->></span> a post-order <span style="color: #bbb;">(</span>filter <span style="color: #494949;">(</span>partial bintree-contains? b<span style="color: #494949;">)</span><span style="color: #bbb;">)</span> bintree-set<span style="color: #494949;">)</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
</pre>
</div>
</div>
</div>
<div id="outline-container-orga6b8a07" class="outline-4">
<h4 id="orga6b8a07">Difference</h4>
<div class="outline-text-4" id="text-orga6b8a07">
<p>
Lastly we find the difference between two binary trees by simply filtering out the set of nodes that are contained within the second binary tree. Like all set-theoretic functions it is variadic and accepts an arbitrary number of arguments. It's time complexity is \(O(n\ log(n))\).
</p>
<div class="org-src-container">
<pre class="src src-clojure"><span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">defn</span> <span style="font-weight: bold; font-style: italic;">bintree-difference</span>
<span style="color: #bbb;">(</span><span style="color: #494949;">[</span>a b & rest<span style="color: #494949;">]</span> <span style="color: #494949;">(</span>reduce bintree-difference a <span style="color: #bbb;">(</span>cons b rest<span style="color: #bbb;">)</span><span style="color: #494949;">)</span><span style="color: #bbb;">)</span>
<span style="color: #bbb;">(</span><span style="color: #494949;">[</span>a b<span style="color: #494949;">]</span>
<span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">->></span> a post-order <span style="color: #bbb;">(</span>filter #<span style="color: #494949;">(</span>not <span style="color: #bbb;">(</span>bintree-contains? b <span style="font-style: italic;">%</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span><span style="color: #bbb;">)</span> bintree-set<span style="color: #494949;">)</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
</pre>
</div>
</div>
</div>
<div id="outline-container-orgedef36a" class="outline-4">
<h4 id="orgedef36a">Subset</h4>
<div class="outline-text-4" id="text-orgedef36a">
<p>
Lastly we have a simple function that determines if one tree is a subset of another tree by checking if each item is contained in the other tree. It's time complexity is (I know you will be shocked) \(O(n\ log(n))\).
</p>
<div class="org-src-container">
<pre class="src src-clojure"><span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">defn</span> <span style="font-weight: bold; font-style: italic;">bintree-subset?</span>
<span style="color: #bbb;">(</span><span style="color: #494949;">[</span>a b & rest<span style="color: #494949;">]</span> <span style="color: #494949;">(</span>reduce bintree-difference a <span style="color: #bbb;">(</span>cons b rest<span style="color: #bbb;">)</span><span style="color: #494949;">)</span><span style="color: #bbb;">)</span>
<span style="color: #bbb;">(</span><span style="color: #494949;">[</span>a b<span style="color: #494949;">]</span>
<span style="color: #494949;">(</span>not <span style="color: #bbb;">(</span><span style="color: #E53935; font-style: italic;">->></span> a post-order <span style="color: #494949;">(</span>some #<span style="color: #bbb;">(</span>not <span style="color: #494949;">(</span>bintree-contains? b <span style="font-style: italic;">%</span><span style="color: #494949;">)</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span><span style="color: #bbb;">)</span><span style="color: #494949;">)</span>
</pre>
</div>
</div>
</div>
</div>
</div>
<div id="outline-container-org769979e" class="outline-2">
<h2 id="org769979e">Python Code</h2>
<div class="outline-text-2" id="text-org769979e">
<p>
Below is also the code in python.
</p>
<div class="org-src-container">
<pre class="src src-python"><span style="color: #E53935; font-style: italic;">class</span> <span style="color: #E53935;">BintreeNode</span>:
<span style="color: #E53935; font-style: italic;">def</span> <span style="font-weight: bold; font-style: italic;">__init__</span><span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">self</span>, val<span style="color: #494949;">)</span>:
<span style="color: #E53935; font-style: italic;">self</span>.gtr = <span style="color: #E53935;">None</span>
<span style="color: #E53935; font-style: italic;">self</span>.lsr = <span style="color: #E53935;">None</span>
<span style="color: #E53935; font-style: italic;">self</span>.val = val
<span style="color: #E53935; font-style: italic;">class</span> <span style="color: #E53935;">Bintree</span>:
<span style="color: #E53935; font-style: italic;">def</span> <span style="font-weight: bold; font-style: italic;">__init__</span><span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">self</span><span style="color: #494949;">)</span>:
<span style="color: #E53935; font-style: italic;">self</span>.head = <span style="color: #E53935;">None</span>
<span style="color: #E53935; font-style: italic;">def</span> <span style="font-weight: bold; font-style: italic;">add_node</span><span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">self</span>, val<span style="color: #494949;">)</span>:
<span style="color: #E53935; font-style: italic;">if</span> <span style="color: #E53935; font-style: italic;">not</span> <span style="color: #E53935; font-style: italic;">self</span>.head:
<span style="color: #E53935; font-style: italic;">self</span>.head = BintreeNode<span style="color: #494949;">(</span>val<span style="color: #494949;">)</span>
<span style="color: #E53935; font-style: italic;">else</span>:
<span style="font-style: italic;">current_node</span> = <span style="color: #E53935; font-style: italic;">self</span>.head
<span style="font-style: italic;">inserted</span> = <span style="color: #E53935;">False</span>
<span style="color: #E53935; font-style: italic;">while</span> <span style="color: #E53935; font-style: italic;">not</span> inserted:
<span style="color: #E53935; font-style: italic;">if</span> current_node.val > val:
<span style="color: #E53935; font-style: italic;">if</span> current_node.lsr:
<span style="font-style: italic;">current_node</span> = current_node.lsr
<span style="color: #E53935; font-style: italic;">else</span>:
<span style="font-style: italic;">current_node.lsr</span> = BintreeNode<span style="color: #494949;">(</span>val<span style="color: #494949;">)</span>
<span style="font-style: italic;">inserted</span> = <span style="color: #E53935;">True</span>
<span style="color: #E53935; font-style: italic;">elif</span> current_node.val < val:
<span style="color: #E53935; font-style: italic;">if</span> current_node.gtr:
<span style="font-style: italic;">current_node</span> = current_node.gtr
<span style="color: #E53935; font-style: italic;">else</span>:
<span style="font-style: italic;">current_node.gtr</span> = BintreeNode<span style="color: #494949;">(</span>val<span style="color: #494949;">)</span>
<span style="font-style: italic;">inserted</span> = <span style="color: #E53935;">True</span>
<span style="color: #E53935; font-style: italic;">else</span>:
<span style="color: #E53935; font-style: italic;">raise</span> <span style="color: #E53935;">Exception</span><span style="color: #494949;">(</span><span style="color: #494949;">"duplicate key"</span><span style="color: #494949;">)</span>
<span style="color: #E53935; font-style: italic;">def</span> <span style="font-weight: bold; font-style: italic;">node_exists</span><span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">self</span>, val<span style="color: #494949;">)</span>:
<span style="font-style: italic;">exists</span> = <span style="color: #E53935;">False</span>
<span style="color: #E53935; font-style: italic;">if</span> <span style="color: #E53935; font-style: italic;">self</span>.head:
<span style="font-style: italic;">current_node</span> = <span style="color: #E53935; font-style: italic;">self</span>.head
<span style="font-style: italic;">found</span> = <span style="color: #E53935;">False</span>
<span style="color: #E53935; font-style: italic;">while</span> <span style="color: #E53935; font-style: italic;">not</span> found:
<span style="color: #E53935; font-style: italic;">if</span> current_node.val > val:
<span style="color: #E53935; font-style: italic;">if</span> current_node.lsr:
<span style="font-style: italic;">current_node</span> = current_node.lsr
<span style="color: #E53935; font-style: italic;">else</span>:
<span style="font-style: italic;">exists</span> = <span style="color: #E53935;">False</span>
<span style="font-style: italic;">found</span> = <span style="color: #E53935;">True</span>
<span style="color: #E53935; font-style: italic;">elif</span> current_node.val < val:
<span style="color: #E53935; font-style: italic;">if</span> current_node.gtr:
<span style="font-style: italic;">current_node</span> = current_node.gtr
<span style="color: #E53935; font-style: italic;">else</span>:
<span style="font-style: italic;">exists</span> = <span style="color: #E53935;">False</span>
<span style="font-style: italic;">found</span> = <span style="color: #E53935;">True</span>
<span style="color: #E53935; font-style: italic;">else</span>:
<span style="font-style: italic;">exists</span> = <span style="color: #E53935;">True</span>
<span style="font-style: italic;">found</span> = <span style="color: #E53935;">True</span>
<span style="color: #E53935; font-style: italic;">return</span> exists
<span style="color: #E53935; font-style: italic;">def</span> <span style="font-weight: bold; font-style: italic;">inorder</span><span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">self</span><span style="color: #494949;">)</span>:
<span style="color: #E53935; font-style: italic;">def</span> <span style="font-weight: bold; font-style: italic;">traverse</span><span style="color: #494949;">(</span>node<span style="color: #494949;">)</span>:
<span style="font-style: italic;">node_list</span> = <span style="color: #494949;">[]</span>
<span style="color: #E53935; font-style: italic;">if</span> node.lsr:
<span style="font-style: italic;">node_list</span> += traverse<span style="color: #494949;">(</span>node.lsr<span style="color: #494949;">)</span>
node_list.append<span style="color: #494949;">(</span>node.val<span style="color: #494949;">)</span>
<span style="color: #E53935; font-style: italic;">if</span> node.gtr:
<span style="font-style: italic;">node_list</span> += traverse<span style="color: #494949;">(</span>node.gtr<span style="color: #494949;">)</span>
<span style="color: #E53935; font-style: italic;">return</span> node_list
<span style="color: #E53935; font-style: italic;">return</span> traverse<span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">self</span>.head<span style="color: #494949;">)</span>
<span style="color: #E53935; font-style: italic;">def</span> <span style="font-weight: bold; font-style: italic;">postorder</span><span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">self</span><span style="color: #494949;">)</span>:
<span style="color: #E53935; font-style: italic;">def</span> <span style="font-weight: bold; font-style: italic;">traverse</span><span style="color: #494949;">(</span>node<span style="color: #494949;">)</span>:
<span style="font-style: italic;">node_list</span> = <span style="color: #494949;">[]</span>
node_list.append<span style="color: #494949;">(</span>node.val<span style="color: #494949;">)</span>
<span style="color: #E53935; font-style: italic;">if</span> node.lsr:
<span style="font-style: italic;">node_list</span> += traverse<span style="color: #494949;">(</span>node.lsr<span style="color: #494949;">)</span>
<span style="color: #E53935; font-style: italic;">if</span> node.gtr:
<span style="font-style: italic;">node_list</span> += traverse<span style="color: #494949;">(</span>node.gtr<span style="color: #494949;">)</span>
<span style="color: #E53935; font-style: italic;">return</span> node_list
<span style="color: #E53935; font-style: italic;">return</span> traverse<span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">self</span>.head<span style="color: #494949;">)</span>
<span style="color: #E53935; font-style: italic;">def</span> <span style="font-weight: bold; font-style: italic;">preorder</span><span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">self</span><span style="color: #494949;">)</span>:
<span style="color: #E53935; font-style: italic;">def</span> <span style="font-weight: bold; font-style: italic;">traverse</span><span style="color: #494949;">(</span>node<span style="color: #494949;">)</span>:
<span style="font-style: italic;">node_list</span> = <span style="color: #494949;">[]</span>
<span style="color: #E53935; font-style: italic;">if</span> node.lsr:
<span style="font-style: italic;">node_list</span> += traverse<span style="color: #494949;">(</span>node.lsr<span style="color: #494949;">)</span>
<span style="color: #E53935; font-style: italic;">if</span> node.gtr:
<span style="font-style: italic;">node_list</span> += traverse<span style="color: #494949;">(</span>node.gtr<span style="color: #494949;">)</span>
node_list.append<span style="color: #494949;">(</span>node.val<span style="color: #494949;">)</span>
<span style="color: #E53935; font-style: italic;">return</span> node_list
<span style="color: #E53935; font-style: italic;">return</span> traverse<span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">self</span>.head<span style="color: #494949;">)</span>
<span style="color: #E53935; font-style: italic;">def</span> <span style="font-weight: bold; font-style: italic;">union</span><span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">self</span>, other<span style="color: #494949;">)</span>:
<span style="color: #E53935; font-style: italic;">for</span> node <span style="color: #E53935; font-style: italic;">in</span> other.preorder<span style="color: #494949;">()</span>:
<span style="color: #E53935; font-style: italic;">self</span>.add_node<span style="color: #494949;">(</span>node<span style="color: #494949;">)</span>
<span style="color: #E53935; font-style: italic;">def</span> <span style="font-weight: bold; font-style: italic;">intersection</span><span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">self</span>, other<span style="color: #494949;">)</span>:
<span style="font-style: italic;">isect_set</span> = Bintree<span style="color: #494949;">()</span>
<span style="color: #E53935; font-style: italic;">for</span> node <span style="color: #E53935; font-style: italic;">in</span> other.preorder<span style="color: #494949;">()</span>:
<span style="color: #E53935; font-style: italic;">if</span> <span style="color: #E53935; font-style: italic;">self</span>.node_exists<span style="color: #494949;">(</span>node<span style="color: #494949;">)</span>:
isect_set.add_node<span style="color: #494949;">(</span>node<span style="color: #494949;">)</span>
<span style="color: #E53935; font-style: italic;">def</span> <span style="font-weight: bold; font-style: italic;">difference</span><span style="color: #494949;">(</span><span style="color: #E53935; font-style: italic;">self</span>, other<span style="color: #494949;">)</span>:
<span style="font-style: italic;">diff_set</span> = Bintree<span style="color: #494949;">()</span>
<span style="color: #E53935; font-style: italic;">for</span> node <span style="color: #E53935; font-style: italic;">in</span> <span style="color: #E53935; font-style: italic;">self</span>.preorder<span style="color: #494949;">()</span>:
<span style="color: #E53935; font-style: italic;">if</span> <span style="color: #E53935; font-style: italic;">not</span> other.node_exists<span style="color: #494949;">(</span>node<span style="color: #494949;">)</span>:
diff_set.add_node<span style="color: #494949;">(</span>node<span style="color: #494949;">)</span>
</pre>
</div>
</div>
</div>
</div>
<div id="postamble" class="status">
<p class="date">Last Modified: 2021-W52-2 01:02</p><p class="creator">Generated Using: <a href="https://www.gnu.org/software/emacs/">Emacs</a> 27.2 (<a href="https://orgmode.org">Org</a> mode 9.4.6)</p><p class="license">Except where otherwise noted content on <a href="https://cons.dev">cons.dev</a> is licensed under a <a href="https://creativecommons.org/licenses/by-sa/4.0/" rel="license">Creative Commons Attribution-ShareAlike 4.0 International License</a>.</p>
</div>
</body>
</html>