问题
I created a class in Pharo known as BinarySearchTreean i implemented a method called BinarySearchTree>>PreOrder and BinarySearchTree>>index
Preorder: myArray index: position
(myArray at: position) ~= -1
ifTrue: [
Transcript show: (myArray at: position).
self Preorder: myArray index: (position * 2).
self Preorder: myArray index: (position * 2) + 1.
].
I then provided this array #(90 60 95 50) with index 1 to make a PreOrder search in my binary tree which i implemented using arrays but it does not work. Please help...
回答1:
#at:
will signal SubscriptOutOfBounds
when the index is < 0 or greater than the size of the array (Smalltalk collections are 1-based, i.e. the first index is 1, not 0). 8 is clearly larger than 4 (the size of myArray
).
The check at the start will never evaluate to False
as your array has no entry -1
, and hence the conditional block will be evaluated every time.
I can't really say where your problem lies as you've excluded all the code that is actually of interest. If you add that I can tell you more.
来源:https://stackoverflow.com/questions/46619649/recursive-method-in-pharo-produces-subscriptoutofbounds8