Skip to content

Commit 0652a18

Browse files
authored
Tree traversal in smalltalk (#453)
1 parent 84e9d5d commit 0652a18

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
Object subclass: #Node
2+
instanceVariableNames: 'children data'
3+
classVariableNames: ''
4+
package: ''
5+
6+
Node>>children
7+
"Children getter."
8+
^ children
9+
10+
Node>>children: newChildren
11+
"Children setter."
12+
children := newChildren.
13+
14+
Node>>data
15+
"Data getter"
16+
^ data
17+
18+
Node>>data: newData
19+
"Data setter"
20+
data := newData.
21+
22+
Node>>dfsRecursive
23+
"Recursive depth first search."
24+
Transcript show: data; cr.
25+
children collect: [ :child | child dfsRecursive ]
26+
27+
Node>>dfsRecursivePostOrder
28+
"Recursive depth first search (post-order)."
29+
children collect: [ :child | (child dfsRecursivePostOrder)].
30+
Transcript show: data; cr.
31+
32+
Node>>dfsInOrderBinaryTree
33+
"Recursive depth first search on a binary tree in order."
34+
children size > 2 ifTrue: [
35+
Transcript show: 'This is not a binary tree!'; cr.
36+
^self.
37+
].
38+
children size = 2 ifTrue: [
39+
(children at: 1) dfsInOrderBinaryTree: value.
40+
].
41+
Transcript show: data; cr.
42+
children size >= 1 ifTrue: [
43+
(children at: 0) dfsInOrderBinaryTree: value.
44+
].
45+
^self.
46+
47+
Node>>dfsStack
48+
"Depth-first search with a stack."
49+
| stack top |
50+
stack := Stack new.
51+
stack push: self.
52+
[stack size > 0] whileTrue: [
53+
top := stack pop.
54+
Transcript show: (top data); cr.
55+
top children reverseDo: [ :child |
56+
stack push: child.
57+
].
58+
].
59+
60+
Node>>bfs
61+
"A breadth-first tree search using queues."
62+
| queue current |
63+
queue := LinkedList with: self.
64+
[ queue size > 0 ] whileTrue: [
65+
current := queue first.
66+
queue removeFirst.
67+
Transcript show: (current data); cr.
68+
current children collect: [ :child |
69+
queue addLast: child
70+
].
71+
].
72+
73+
| test |
74+
test := Node new: 1 children: { Node new: 2.
75+
Node new: 3 children: { Node new: 4.
76+
Node new: 5. } }.
77+
test dfsRecursive.
78+
test dfsRecursivePostorder.
79+
test dfsInOrderBinaryTree.
80+
test dfsStack.
81+
test bfs.

contents/tree_traversal/tree_traversal.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ As a note, a `node` struct is not necessary in javascript, so this is an example
3232
[import:4-37, lang:"php"](code/php/tree_traversal.php)
3333
{% sample lang="crystal" %}
3434
[import:1-5, lang:"crystal"](code/crystal/tree-traversal.cr)
35+
{% sample lang="st" %}
36+
[import:1-20, lang:"smalltalk"](code/smalltalk/tree_traversal.st)
3537
{% sample lang="go" %}
3638
[import:5-8, lang:"go"](code/golang/treetraversal.go)
3739
{% sample lang="asm-x64" %}
@@ -77,6 +79,8 @@ Because of this, the most straightforward way to traverse the tree might be recu
7779
[import:41-49, lang:"php"](code/php/tree_traversal.php)
7880
{% sample lang="crystal" %}
7981
[import:7-10, lang:"crystal"](code/crystal/tree-traversal.cr)
82+
{% sample lang="st" %}
83+
[import:22-27, lang:"smalltalk"](code/smalltalk/tree_traversal.st)
8084
{% sample lang="go" %}
8185
[import:10-15, lang:"go"](code/golang/treetraversal.go)
8286
{% sample lang="asm-x64" %}
@@ -131,6 +135,8 @@ Now, in this case the first element searched through is still the root of the tr
131135
[import:51-57, lang:"php"](code/php/tree_traversal.php)
132136
{% sample lang="crystal" %}
133137
[import:12-15, lang:"crystal"](code/crystal/tree-traversal.cr)
138+
{% sample lang="st" %}
139+
[import:29-34, lang:"smalltalk"](code/smalltalk/tree_traversal.st)
134140
{% sample lang="go" %}
135141
[import:17-22, lang:"go"](code/golang/treetraversal.go)
136142
{% sample lang="asm-x64" %}
@@ -180,6 +186,8 @@ In this case, the first node visited is at the bottom of the tree and moves up t
180186
[import:59-78, lang:"php"](code/php/tree_traversal.php)
181187
{% sample lang="crystal" %}
182188
[import:17-31, lang:"crystal"](code/crystal/tree-traversal.cr)
189+
{% sample lang="st" %}
190+
[import:36-49, lang:"smalltalk"](code/smalltalk/tree_traversal.st)
183191
{% sample lang="go" %}
184192
[import:24-38, lang:"go"](code/golang/treetraversal.go)
185193
{% sample lang="asm-x64" %}
@@ -238,6 +246,8 @@ In code, it looks like this:
238246
[import:80-91, lang:"php"](code/php/tree_traversal.php)
239247
{% sample lang="crystal" %}
240248
[import:33-41, lang:"crystal"](code/crystal/tree-traversal.cr)
249+
{% sample lang="st" %}
250+
[import:47-58, lang:"smalltalk"](code/smalltalk/tree_traversal.st)
241251
{% sample lang="go" %}
242252
[import:40-49, lang:"go"](code/golang/treetraversal.go)
243253
{% sample lang="asm-x64" %}
@@ -289,6 +299,8 @@ And this is exactly what Breadth-First Search (BFS) does! On top of that, it can
289299
[import:93-104, lang:"php"](code/php/tree_traversal.php)
290300
{% sample lang="crystal" %}
291301
[import:43-51, lang:"crystal"](code/crystal/tree-traversal.cr)
302+
{% sample lang="st" %}
303+
[import:60-71, lang:"smalltalk"](code/smalltalk/tree_traversal.st)
292304
{% sample lang="go" %}
293305
[import:51-60, lang:"go"](code/golang/treetraversal.go)
294306
{% sample lang="asm-x64" %}
@@ -351,6 +363,8 @@ The code snippets were taken from this [Scratch project](https://scratch.mit.edu
351363
[import, lang:"php"](code/php/tree_traversal.php)
352364
{% sample lang="crystal" %}
353365
[import, lang:"crystal"](code/crystal/tree-traversal.cr)
366+
{% sample lang="st" %}
367+
[import, lang:"smalltalk"](code/smalltalk/tree_traversal.st)
354368
{% sample lang="go" %}
355369
[import, lang:"go"](code/golang/treetraversal.go)
356370
{% sample lang="asm-x64" %}

0 commit comments

Comments
 (0)