"This method first removes aTree from whatever tree it is already in (if any) and then it adds it as the left child of the receiver."
(leftChild notNil) ifTrue: [
leftChild remove
].
(aTree notNil) ifTrue: [
aTree remove.
aTree parent: self
].
leftChild _ aTree! !
!Tree2 methodsFor: 'tree functions'!
addRightChild: aTree
"This method first removes aTree from whatever tree it is already in (if any) and then it adds it as the right child of the receiver."
(rightChild notNil) ifTrue: [
rightChild remove
].
(aTree notNil) ifTrue: [
aTree remove.
aTree parent: self
].
rightChild _ aTree! !
!Tree2 methodsFor: 'tree functions'!
remove
"This method removes the receiver from whatever tree it is in (if any)."
(parent notNil) ifTrue: [
(parent leftChild = self) ifTrue: [
parent leftChild: nil
].
(parent rightChild = self) ifTrue: [
parent rightChild: nil
]
].
parent _ nil! !
!Tree2 methodsFor: 'tree functions'!
remove: aLabel
"Search the tree whose root is the receiver for a node labelled with aLabel and remove that node. Return the modified tree's new root."
^ self "Unimplemented..."! !
!Tree2 methodsFor: 'tree functions'!
removeNode: aNode
"This method is passed aNode. It searches the sub-tree self for the node and removes it. (It does not remove the children of aNode.) If the tree contains aNode, it returns the modified sub-tree, otherwise it returns 'false'."
| gotIt |
(self = aNode) ifTrue: [
^ self removeRoot
].
leftChild notNil ifTrue: [
gotIt _ leftChild removeNode: aNode.
(gotIt ~~ false) ifTrue: [^ self]
].
rightChild notNil ifTrue: [
gotIt _ rightChild removeNode: aNode.
(gotIt ~~ false) ifTrue: [^ self]
].
^false! !
!Tree2 methodsFor: 'tree functions'!
removeRoot
"This method returns a sub-tree containing all the nodes in the tree whose root is the receiver, except the root node. If the left sub-tree is nil, it just returns the right sub-tree. Otherwise, it adds the right sub-tree as the right child of the left sub-tree's right-most descendent and returns the modified left sub-tree."
drawSubtree: aTree on: aCanvas at: aPoint xIncr: xIncr yIncr: yIncr
"Draw this sub-tree on the given canvas. It displays edges leading down to its sub-trees and then it calls itself recursively to obtain a display of its sub-trees."
| bottomPoint leftPoint rightPoint |
"DEBUGGING: DISPLAY A BLUE RECTANGLE WHOSE UPPER CORNER IS AT aPoint."
"aCanvas fillRectangle: (aPoint extent: 50@50) color: Color blue."
"DISPLAY THE OVAL AND THE LABEL TEXT."
aCanvas fillOval: (aPoint-(20@0) extent: 40@25) color: Color yellow borderWidth: 1 borderColor: Color black.
aCanvas text: aTree label at: (aPoint x - 13) @ (aPoint y + 5) font: nil color: Color black.
"DISPLAY THE LEFT SUB-TREE AND A LINE DOWN TO IT."
bottomPoint _ (aPoint x ) @ (aPoint y + 24).
leftPoint _ (aPoint x - xIncr) @ (aPoint y + yIncr).
(aTree leftChild) notNil ifTrue: [
aCanvas line: bottomPoint to: leftPoint color: Color black.
self
drawSubtree: aTree leftChild
on: aCanvas at: leftPoint
xIncr: xIncr//2
yIncr: yIncr
].
"DISPLAY THE RIGHT SUB-TREE AND A LINE DOWN TO IT."
rightPoint _ (aPoint x + xIncr) @ (aPoint y + yIncr).
(aTree rightChild) notNil ifTrue: [
aCanvas line: bottomPoint to: rightPoint color: Color black.
"Imagine that we are displaying this tree, but do not display anything. Instead, ask which node would contain mousePoint within its oval and return that node. Return nil if no node contains mousePoint."
| whereWouldBeDisplayed leftPoint ans rightPoint |
self error: 'This message is not tested'.
"SEE WHERE WE WOULD HAVE DISPLAYED THE OVAL AND SEE IF IT CONTAINS mousePoint."