Student Originated Software 1997-1998
Fall Quarter

A Software Engineering Course at
The Evergreen State College

Smalltalk Code Examples - Binary Tree






Object subclass: #BinaryTree
instanceVariableNames: 'value leftTree rightTree '
classVariableNames: 'BinBox '
poolDictionaries: ''
category: 'Binary Tree'

BinaryTree methodsFor: 'accessing'
add: aValue
| right left |
(aValue > value)
ifTrue: [(rightTree=nil)
ifTrue: [right := BinaryTree new: aValue. self setRightTree: right.]
ifFalse: [rightTree add: aValue]. ]
ifFalse:[ (leftTree=nil)
ifTrue: [left := BinaryTree new: aValue. self setLeftTree: left.]
ifFalse: [leftTree add: aValue]. ].

BinaryTree methodsFor: 'private'

setLeftTree: aValue

leftTree:= aValue.

setRightTree: aValue

rightTree:= aValue.

setValue: aValue
value:= aValue.

BinaryTree methodsFor: 'printing'

ascendingPrint

(value==nil)
ifTrue: []
ifFalse: [

(leftTree ~= nil)
ifTrue: [ leftTree ascendingPrint ] .

Transcript show: value printString; cr.

(rightTree ~= nil)
ifTrue:[ rightTree ascendingPrint ] ].

ascendingPrint2

(value==nil)
ifTrue: []
ifFalse: [

(leftTree ~= nil)
ifTrue: [ leftTree ascendingPrint2 ] .

BinBox add: value.

(rightTree ~= nil)
ifTrue:[ rightTree ascendingPrint2 ] ].

ascendingPrint: aTree

(aTree value==nil)
ifTrue: []
ifFalse: [

(leftTree <> nil)
ifTrue: [ leftTree ascendingPrint ] .

Transcript show: value printString; cr.

(rightTree <> nil)
ifTrue:[ rightTree ascendingPrint ] ].

callPrint

BinBox := OrderedCollection new.

self ascendingPrint2.

Transcript show: BinBox printString; cr.

"-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "

BinaryTree class
instanceVariableNames: ''

BinaryTree class methodsFor: 'instance creation'

new
^ self basicNew.

new: aValue
^self new setValue: aValue.

Posted Nov 3, 1997
Pavan Auman

[ SOS Home Page | Case Study | Year Long Projects | OOP ]


For more information contact
[ Evergreen Home Page | Academic Programs ]


Created by: SoSwEbGrOuP
E-mail: ringert@evergreen.edu