-
-
Notifications
You must be signed in to change notification settings - Fork 360
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. | ||
|
||
| ]. | ||
| children collect: [ :each | each dfsRecursive]. | ||
|
||
|
|
||
| Node>>dfsRecursivePostorder | ||
|
||
| "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!'. | ||
|
||
| 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 | | ||
|
||
| 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. | ||
| 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.