-
-
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
Changes from 1 commit
f512f11
0970e19
69d5118
c0a07b2
3395e0a
8e3300b
d56619e
f051340
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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]. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What exactly is an |
||
|
||
Node>>dfsRecursivePostorder | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. ] There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I did this taking reference from the Python example. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably printing will be more clear (I guess). There was a problem hiding this comment. Choose a reason for hiding this commentThe 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!'. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This block of 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 | | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
]. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
Neverik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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. |
Uh oh!
There was an error while loading. Please reload this page.