So I'm working on a program which is supposed to 'heapify' integers that a user adds to a set, among other things. I'm having a problem right now, however, when I try to access the left child node of a Node which was passed as a parameter to a function. Visual Works runs out of memory...
The odd thing is that in the debugger, I can see that the Left child node of the passed node was created and assigned to the passed node correctly. But when I try to access it via the source code, I get a crazy memory error.
Here is the corresponding code. Hopefully it is readable
The function in which the error occurs, once it is called:
addLeftChildTo: aNode
"Determine Left child of passed node. Set that child's parent node as self. Imprint the index where that value was retrieved from the array."
| temp leftChildIndex |
2 * aNode indexOfNode <= arrayOfInput size
ifTrue:
[leftChildIndex := 2 * aNode indexOfNode.
aNode left: (arrayOfInput at: leftChildIndex).
aNode left parentNode: aNode. <---VW Runs out of memory and crashes here. This occurs once the debugger dives into the 'left' accessor method of the passed aNode object.
aNode left indexOfNode: leftChildIndex.
self bubbleUp: aNode left.
self bubbleUpArrayOfInput: aNode left]
ifFalse: [aNode left: nil]
Here are the accessor methods of 'left' for the BinaryHeapNode class
left
^self left
left: aValue
left := BinaryHeapNode new: aValue.
And in case it's helpful to see the code that originally calls the addLeftChildTo: method...
populateHeap
"from passed array of input"
"Add a node (typically a node with an element from the ArrayOfInput) to the heap. Bubble up as you go."
| temp |
rootNode isNil
ifTrue:
[rootNode := BinaryHeapNode new: (arrayOfInput at: 1).
rootNode indexOfNode: indexOfArray.
self addLeftChildTo: rootNode. <--- Call to function with problematic code.
...
I don't understand what I'm doing wrong and I've been stuck on this problem for a couple of hours. Any ideas?
You made an infinite recursion with accessor #left:
left
^self left
This method actually calls itself. Return just instance variable ^left
instead.