-
-
Notifications
You must be signed in to change notification settings - Fork 359
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
Amaras
merged 8 commits into
algorithm-archivists:master
from
Neverik:tree_traversal_smalltalk
Oct 23, 2021
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f512f11
Tree traversal in smalltalk
Neverik 0970e19
Fixed indent
Neverik 69d5118
Kinda some changes
Neverik c0a07b2
Shortening text
Neverik 3395e0a
Shortening code
Neverik 8e3300b
typo fix
Neverik d56619e
Merge branch 'master' into tree_traversal_smalltalk
Neverik f051340
Merge branch 'master' into tree_traversal_smalltalk
Neverik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
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." | ||
Transcript show: data; cr. | ||
children collect: [ :child | child dfsRecursive ] | ||
|
||
Node>>dfsRecursivePostOrder | ||
"Recursive depth first search (post-order)." | ||
children collect: [ :child | (child dfsRecursivePostOrder)]. | ||
Transcript show: data; cr. | ||
|
||
Node>>dfsInOrderBinaryTree | ||
"Recursive depth first search on a binary tree in order." | ||
children size > 2 ifTrue: [ | ||
Transcript show: 'This is not a binary tree!'; cr. | ||
^self. | ||
]. | ||
children size = 2 ifTrue: [ | ||
(children at: 1) dfsInOrderBinaryTree: value. | ||
]. | ||
Transcript show: data; cr. | ||
children size >= 1 ifTrue: [ | ||
(children at: 0) dfsInOrderBinaryTree: value. | ||
]. | ||
^self. | ||
|
||
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); 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 data); cr. | ||
current children collect: [ :child | | ||
queue addLast: child | ||
]. | ||
]. | ||
|
||
| test | | ||
test := Node new: 1 children: { Node new: 2. | ||
Neverik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Node new: 3 children: { Node new: 4. | ||
Node new: 5. } }. | ||
test dfsRecursive. | ||
test dfsRecursivePostorder. | ||
test dfsInOrderBinaryTree. | ||
test dfsStack. | ||
test bfs. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.