Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ CTBinarySearchTreeTest >> testAsArray [
self assert: result equals: #(20 30 40 50 70)
]

{ #category : 'tests' }
CTBinarySearchTreeTest >> testAtIfAbsent [

tree addAll: #(50 30 70).
self assert: (tree at: 30 ifAbsent: [ #notFound ]) equals: 30.
self assert: (tree at: 99 ifAbsent: [ #notFound ]) equals: #notFound
]

{ #category : 'tests' }
CTBinarySearchTreeTest >> testBSTValidation [

Expand Down Expand Up @@ -186,12 +194,13 @@ CTBinarySearchTreeTest >> testEmptyTreeOperations [
]

{ #category : 'tests' }
CTBinarySearchTreeTest >> testFindMinMax [
CTBinarySearchTreeTest >> testFirstLast [

tree addAll: #(50 30 70 20 80).

self assert: tree findMin equals: 20.
self assert: tree findMax equals: 80
self assert: tree first equals: 20.
self assert: tree last equals: 80.
self assert: CTBinarySearchTree new first isNil.
self assert: CTBinarySearchTree new last isNil
]

{ #category : 'tests' }
Expand Down Expand Up @@ -362,16 +371,20 @@ CTBinarySearchTreeTest >> testRemoveNonExistentElement [
{ #category : 'tests' }
CTBinarySearchTreeTest >> testRemoveRoot [
"Root with no children"

tree add: 50.
tree remove: 50.
self assert: tree isEmpty.

"Root with two children"
tree clear.
tree addAll: #(50 30 70 20 40 60 80).
tree addAll: #( 50 30 70 20 40 60 80 ).
tree remove: 50.
self assert: tree size equals: 6.
self deny: (tree includes: 50).

self assert: (tree includes: 60).
self assert: tree root contents equals: 60
]

{ #category : 'tests' }
Expand Down
12 changes: 12 additions & 0 deletions src/Containers-BinarySearchTree/CTBSTAbstractNode.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,24 @@ CTBSTAbstractNode >> findMax [
^ self subclassResponsibility
]

{ #category : 'searching' }
CTBSTAbstractNode >> findMaxNode [

^ self subclassResponsibility
]

{ #category : 'searching' }
CTBSTAbstractNode >> findMin [

^ self subclassResponsibility
]

{ #category : 'searching' }
CTBSTAbstractNode >> findMinNode [

^ self subclassResponsibility
]

{ #category : 'accessing' }
CTBSTAbstractNode >> height [

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ I provide default 'do nothing' behavior for all tree operations, eliminating the
When elements are added to me, I create and return a new CTBSTNode containing the element, effectively growing the tree.
"
Class {
#name : 'CTBSTNillNode',
#name : 'CTBSTNilNode',
#superclass : 'CTBSTAbstractNode',
#category : 'Containers-BinarySearchTree',
#package : 'Containers-BinarySearchTree'
}

{ #category : 'adding' }
CTBSTNillNode >> addChild: anObject [
CTBSTNilNode >> addChild: anObject [

^ CTBSTNode new
contents: anObject;
Expand All @@ -22,122 +22,134 @@ CTBSTNillNode >> addChild: anObject [
]

{ #category : 'accessing' }
CTBSTNillNode >> contents [
CTBSTNilNode >> contents [

^ nil
]

{ #category : 'accessing' }
CTBSTNillNode >> contents: anObject [
CTBSTNilNode >> contents: anObject [

"Do nothing for nil node"
]

{ #category : 'enumerating' }
CTBSTNillNode >> elementsFrom: min to: max into: aCollection [
CTBSTNilNode >> elementsFrom: min to: max into: aCollection [

"Do nothing for nill node"
]

{ #category : 'enumerating' }
CTBSTNillNode >> elementsGreaterThan: anObject into: aCollection [
CTBSTNilNode >> elementsGreaterThan: anObject into: aCollection [

"Do nothing for nill node"
]

{ #category : 'enumerating' }
CTBSTNillNode >> elementsLessThan: anObject into: aCollection [
CTBSTNilNode >> elementsLessThan: anObject into: aCollection [

"Do nothing for nill node"
]

{ #category : 'searching' }
CTBSTNillNode >> findMax [
CTBSTNilNode >> findMax [

^ nil
]

{ #category : 'searching' }
CTBSTNillNode >> findMin [
CTBSTNilNode >> findMaxNode [

^ self
]

{ #category : 'searching' }
CTBSTNilNode >> findMin [

^ nil
]

{ #category : 'searching' }
CTBSTNilNode >> findMinNode [

^ self
]

{ #category : 'accessing' }
CTBSTNillNode >> height [
CTBSTNilNode >> height [

^ 0
]

{ #category : 'enumerating' }
CTBSTNillNode >> inOrderDo: aBlock [
CTBSTNilNode >> inOrderDo: aBlock [

"Do nothing for nil node"
]

{ #category : 'testing' }
CTBSTNillNode >> isEmpty [
CTBSTNilNode >> isEmpty [

^ true
]

{ #category : 'testing' }
CTBSTNillNode >> isLeaf [
CTBSTNilNode >> isLeaf [

^ false
]

{ #category : 'enumerating' }
CTBSTNillNode >> postOrderDo: aBlock [
CTBSTNilNode >> postOrderDo: aBlock [

"Do nothing for nill node"
]

{ #category : 'enumerating' }
CTBSTNillNode >> preOrderDo: aBlock [
CTBSTNilNode >> preOrderDo: aBlock [

"Do nothing for nill node"
]

{ #category : 'searching' }
CTBSTNillNode >> predecessorOf: anObject [
CTBSTNilNode >> predecessorOf: anObject [

^ nil
]

{ #category : 'removing' }
CTBSTNillNode >> removeValue: anObject [
CTBSTNilNode >> removeValue: anObject [

"Element not found - return self unchanged"
^ self
]

{ #category : 'accessing' }
CTBSTNillNode >> search: anObject [
CTBSTNilNode >> search: anObject [

^ nil
]

{ #category : 'accessing' }
CTBSTNillNode >> size [
CTBSTNilNode >> size [

^ 0
]

{ #category : 'searching' }
CTBSTNillNode >> successorOf: anObject [
CTBSTNilNode >> successorOf: anObject [

^ nil
]

{ #category : 'testing' }
CTBSTNillNode >> validateBSTProperty [
CTBSTNilNode >> validateBSTProperty [

^ true
]

{ #category : 'private' }
CTBSTNillNode >> validateBSTPropertyWithMin: min max: max [
CTBSTNilNode >> validateBSTPropertyWithMin: min max: max [

^ true
]
Loading
Loading