--- a/BinaryTree.st Tue Sep 07 01:27:31 2004 +0200
+++ b/BinaryTree.st Fri Sep 10 13:16:43 2004 +0200
@@ -480,9 +480,32 @@
removeIdenticalValue:oldValue sortBlock:sortBlock
"remove a value - returns a new treeNode, or nil if the value is not in the tree"
- |newTop|
+ ^ self removeValue:oldValue using:#== sortBlock:sortBlock
+!
+
+removeValue:oldValue sortBlock:sortBlock
+ "remove a value - returns a new treeNode, or nil if the value is not in the tree"
+
+ ^ self removeValue:oldValue using:#= sortBlock:sortBlock
+!
+
+removeValue:oldValue using:compareOp sortBlock:sortBlock
+ "remove a value - returns a new treeNode, or nil if the value is not in the tree"
- data == oldValue ifTrue:[
+ |thisIsMyNode newTop|
+
+ "/ speed hack - avoids message sends
+ compareOp == #== ifTrue:[
+ thisIsMyNode := (data == oldValue).
+ ] ifFalse:[
+ compareOp == #= ifTrue:[
+ thisIsMyNode := (data = oldValue).
+ ] ifFalse:[
+ thisIsMyNode := data perform:compareOp with:oldValue.
+ ].
+ ].
+
+ thisIsMyNode ifTrue:[
leftSubtree isNil ifTrue:[
^ rightSubtree
].
@@ -496,48 +519,18 @@
].
(sortBlock value:oldValue value:data) ifTrue:[
+ "/ the value should be in the left half.
leftSubtree isNil ifTrue:[
^ nil
].
- leftSubtree := leftSubtree removeIdenticalValue:oldValue sortBlock:sortBlock.
- ^ self.
- ].
- rightSubtree isNil ifTrue:[
- ^ nil
- ].
- rightSubtree := rightSubtree removeIdenticalValue:oldValue sortBlock:sortBlock.
- ^ self.
-!
-
-removeValue:oldValue sortBlock:sortBlock
- "remove a value - returns a new treeNode, or nil if the value is not in the tree"
-
- |newTop|
-
- data = oldValue ifTrue:[
- leftSubtree isNil ifTrue:[
- ^ rightSubtree
- ].
+ leftSubtree := leftSubtree removeValue:oldValue using:compareOp sortBlock:sortBlock.
+ ] ifFalse:[
+ "/ the value should be in the right half.
rightSubtree isNil ifTrue:[
- ^ leftSubtree
- ].
- newTop := self removeLeftRightMostNode.
- newTop leftSubtree:leftSubtree.
- newTop rightSubtree:rightSubtree.
- ^ newTop.
- ].
-
- (sortBlock value:oldValue value:data) ifTrue:[
- leftSubtree isNil ifTrue:[
^ nil
].
- leftSubtree := leftSubtree removeValue:oldValue sortBlock:sortBlock.
- ^ self.
+ rightSubtree := rightSubtree removeValue:oldValue using:compareOp sortBlock:sortBlock.
].
- rightSubtree isNil ifTrue:[
- ^ nil
- ].
- rightSubtree := rightSubtree removeValue:oldValue sortBlock:sortBlock.
^ self.
! !
@@ -554,6 +547,26 @@
leftSubtree printOn: aStream. aStream nextPut: $ .
rightSubtree printOn: aStream.
aStream nextPut: $)]
+!
+
+printOn:aStream indent:i
+ "Append the graphical ascii representation to aStream"
+
+ data isNil
+ ifTrue: [aStream spaces:i. aStream nextPutAll: '--']
+ ifFalse: [
+ aStream spaces:i. aStream nextPut: $(.
+ data printOn: aStream.
+ aStream cr.
+ leftSubtree isNil
+ ifTrue:[ aStream spaces:i+2. '--' printOn: aStream]
+ ifFalse:[ leftSubtree printOn: aStream indent:i+2 ].
+ aStream cr.
+ rightSubtree isNil
+ ifTrue:[ aStream spaces:i+2. '--' printOn: aStream]
+ ifFalse:[ rightSubtree printOn: aStream indent:i+2 ].
+ aStream nextPut: $)
+ ]
! !
!BinaryTree::BinaryTreeNode methodsFor:'private helpers'!
@@ -577,8 +590,10 @@
do:[:word |
tree insert:(BinaryTreeNode data:word).
].
- Transcript showCR:tree.
+ tree printOn:Transcript indent:0. Transcript cr.
+ '---------------------------' printOn:Transcript. Transcript cr.
tree removeLeftRightMostNode.
+ tree printOn:Transcript indent:0. Transcript cr.
"
!
@@ -721,5 +736,5 @@
!BinaryTree class methodsFor:'documentation'!
version
- ^ '$Header: /cvs/stx/stx/libbasic2/BinaryTree.st,v 1.2 2003-12-10 09:54:24 cg Exp $'
+ ^ '$Header: /cvs/stx/stx/libbasic2/BinaryTree.st,v 1.3 2004-09-10 11:16:43 cg Exp $'
! !