Skip to content

Tree traversal in smalltalk #453

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Oct 23, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 101 additions & 0 deletions contents/tree_traversal/code/smalltalk/tree_traversal.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
Object subclass: #Node
instanceVariableNames: 'children data'
classVariableNames: ''
package: ''

Node>>children
"Children getter."
^ children

Node>>children: newChildren
"Children setter."
children := newChildren.

Node>>data
"Data getter"
^ data

Node>>data: newData
"Data setter"
data := newData.

Node>>dfsRecursive
"Recursive depth first search."
data isNil ifFalse: [
Transcript show: data.
Transcript cr.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You do this a lot:

Transcript show: something.
Transcript cr.

That can, and should, be simplified to this:

Transcript show: something; cr.

I'm not gonna leave a comment everywhere you do it, because that'd be a lot.

].
children collect: [ :each | each dfsRecursive].
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What exactly is an each, and why does children contain those, instead of... children? (I'm trying to tell you to rename each to child, because the parameter isn't representing an "each", it's representing one of the several children)


Node>>dfsRecursivePostorder
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not take a visitor and return an array instead of just printing all the elements?

Node>>dfsPostorder: ary into: visitor
  children inject: ary into: [ :child | child dfsPostorder: visitor ]
  data isNil ifFalse: [ ary add: visitor value: data. ]  

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did this taking reference from the Python example.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably printing will be more clear (I guess).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Neverik Fair enough! This isn't a necessary change, just an idea.

"Recursive depth first search (post-order)."
children collect: [ :each | each dfsRecursivePostorder ].
data isNil ifTrue: [ ^ self ].
Transcript show: data.
Transcript cr.

Node>>dfsInOrderBinaryTree
"Recursive depth first search on a binary tree in order."
children size = 2 ifTrue: [
(children at: 1) dfsInOrderBinaryTree.
Transcript show: data.
Transcript cr.
(children at: 2) dfsInOrderBinaryTree.
^self.
].
children size = 1 ifTrue: [
(children at: 1) dfsInOrderBinaryTree.
Transcript show: data.
Transcript cr.
^self.
].
children size = 0 ifTrue: [
Transcript show: data.
Transcript cr.
^self.
].
Transcript show: 'This is not a binary tree!'.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This block of ifs can be simplified a bit:

children size > 2 ifTrue: [
  Transcript show: 'This is not a binary tree!'; cr.
  ^self.
]
children size = 2 ifTrue: [
  (children at: 1) dfsInOrderBinaryTree.
]
Transcript show: data; cr.
children size >= 1 ifTrue: [
  (children at: 0) dfsInOrderBinaryTree.
]
^self.

Transcript cr.
children length

Node>>dfsStack
"Depth-first search with a stack."
| stack top |
stack := Stack new.
stack push: self.
[stack size > 0] whileTrue: [
top := stack pop.
Transcript show: top data.
Transcript cr.
top children reverseDo: [ :child |
stack push: child
].
].

Node>>bfs
"A breadth-first tree search using queues."
| queue current |
queue := LinkedList with: self.
[ queue size > 0 ] whileTrue: [
current := queue first.
queue removeFirst.
Transcript show: current.
Transcript cr.
current children collect: [ :child |
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Messy indenting here, too; again, please switch to all spaces.

queue addLast: child
].
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that period is unnecessary.

]

| test |
test := Node new: 1 children: { Node new: 2.
Node new: 3 children: { Node new: 4.
Node new: 5. } }.
test dfsRecursive.
Transcript cr.
test dfsRecursivePostorder.
Transcript cr.
test dfsInOrderBinaryTree.
Transcript cr.
test dfsStack.
Transcript cr.
test bfs.
14 changes: 14 additions & 0 deletions contents/tree_traversal/tree_traversal.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ This has not been implemented in your chosen language, so here is the Julia code
[import:3-27, lang:"php"](code/php/tree_traversal.php)
{% sample lang="crystal" %}
[import:1-5, lang:"crystal"](code/crystal/tree-traversal.cr)
{% sample lang="st" %}
[import:1-20, lang:"st"](code/smalltalk/tree_traversal.st)
{% endmethod %}

Because of this, the most straightforward way to traverse the tree might be recursive. This naturally leads us to the Depth-First Search (DFS) method:
Expand Down Expand Up @@ -66,6 +68,8 @@ Because of this, the most straightforward way to traverse the tree might be recu
[import:31-35, lang:"php"](code/php/tree_traversal.php)
{% sample lang="crystal" %}
[import:7-10, lang:"crystal"](code/crystal/tree-traversal.cr)
{% sample lang="st" %}
[import:22-28, lang:"st"](code/smalltalk/tree_traversal.st)
{% endmethod %}

At least to me, this makes a lot of sense. We fight recursion with recursion! First, we first output the node we are on and then we call `DFS_recursive(...)` on each of its children nodes. This method of tree traversal does what its name implies: it goes to the depths of the tree first before going through the rest of the branches. In this case, the ordering looks like:
Expand Down Expand Up @@ -108,6 +112,8 @@ Now, in this case the first element searched through is still the root of the tr
[import:37-41, lang:"php"](code/php/tree_traversal.php)
{% sample lang="crystal" %}
[import:12-15, lang:"crystal"](code/crystal/tree-traversal.cr)
{% sample lang="st" %}
[import:30-35, lang:"st"](code/smalltalk/tree_traversal.st)
{% endmethod %}

<p>
Expand Down Expand Up @@ -145,6 +151,8 @@ In this case, the first node visited is at the bottom of the tree and moves up t
[import:43-62, lang:"php"](code/php/tree_traversal.php)
{% sample lang="crystal" %}
[import:17-31, lang:"crystal"](code/crystal/tree-traversal.cr)
{% sample lang="st" %}
[import:37-59, lang:"st"](code/smalltalk/tree_traversal.st)
{% endmethod %}

<p>
Expand Down Expand Up @@ -192,6 +200,8 @@ In code, it looks like this:
[import:64-73, lang:"php"](code/php/tree_traversal.php)
{% sample lang="crystal" %}
[import:33-41, lang:"crystal"](code/crystal/tree-traversal.cr)
{% sample lang="st" %}
[import:61-73, lang:"st"](code/smalltalk/tree_traversal.st)
{% endmethod %}

All this said, there are a few details about DFS that might not be idea, depending on the situation. For example, if we use DFS on an incredibly long tree, we will spend a lot of time going further and further down a single branch without searching the rest of the data structure. In addition, it is not the natural way humans would order a tree if asked to number all the nodes from top to bottom. I would argue a more natural traversal order would look something like this:
Expand Down Expand Up @@ -231,6 +241,8 @@ And this is exactly what Breadth-First Search (BFS) does! On top of that, it can
[import:65-74, lang:"php"](code/php/tree_traversal.php)
{% sample lang="crystal" %}
[import:43-51, lang:"crystal"](code/crystal/tree-traversal.cr)
{% sample lang="st" %}
[import:75-87, lang:"st"](code/smalltalk/tree_traversal.st)
{% endmethod %}

## Example Code
Expand Down Expand Up @@ -272,6 +284,8 @@ The code snippets were taken from this [Scratch project](https://scratch.mit.edu
[import, lang:"php"](code/php/tree_traversal.php)
{% sample lang="crystal" %}
[import, lang:"crystal"](code/crystal/tree-traversal.cr)
{% sample lang="st" %}
[import, lang:"st"](code/smalltalk/tree_traversal.st)
{% endmethod %}


Expand Down