Skip to content

Commit db183c3

Browse files
authored
Merge pull request #3 from Alokzh/bst-initial-methods
Basic Binary Search Tree Implementation with Tests
2 parents 2da9648 + de66350 commit db183c3

File tree

5 files changed

+372
-4
lines changed

5 files changed

+372
-4
lines changed

src/Containers-BinarySearchTree-Tests/CTBinarySearchTreeTest.class.st

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,93 @@ A CTBinarySearchTreeTest is a test class for testing the behavior of CTBinarySea
44
Class {
55
#name : 'CTBinarySearchTreeTest',
66
#superclass : 'TestCase',
7+
#instVars : [
8+
'tree'
9+
],
710
#category : 'Containers-BinarySearchTree-Tests',
811
#package : 'Containers-BinarySearchTree-Tests'
912
}
13+
14+
{ #category : 'running' }
15+
CTBinarySearchTreeTest >> setUp [
16+
super setUp.
17+
tree := CTBinarySearchTree new
18+
]
19+
20+
{ #category : 'tests' }
21+
CTBinarySearchTreeTest >> testAddMultipleElements [
22+
23+
tree add: 50.
24+
tree add: 30.
25+
tree add: 70.
26+
self assert: tree size equals: 3
27+
]
28+
29+
{ #category : 'tests' }
30+
CTBinarySearchTreeTest >> testAddSingleElement [
31+
32+
tree add: 42.
33+
self deny: tree isEmpty.
34+
self assert: tree size equals: 1
35+
]
36+
37+
{ #category : 'tests' }
38+
CTBinarySearchTreeTest >> testEmpty [
39+
40+
self assert: tree isEmpty.
41+
self assert: tree size equals: 0
42+
]
43+
44+
{ #category : 'tests' }
45+
CTBinarySearchTreeTest >> testHeight [
46+
47+
self assert: tree height equals: 0.
48+
49+
tree add: 50.
50+
self assert: tree height equals: 1.
51+
52+
tree add: 30.
53+
self assert: tree height equals: 2.
54+
55+
tree add: 70.
56+
self assert: tree height equals: 2
57+
]
58+
59+
{ #category : 'tests' }
60+
CTBinarySearchTreeTest >> testInOrderTraversal [
61+
62+
| result |
63+
tree add: 50.
64+
tree add: 30.
65+
tree add: 70.
66+
tree add: 20.
67+
tree add: 40.
68+
69+
result := OrderedCollection new.
70+
tree inOrderDo: [ :each | result add: each ].
71+
self assert: result asArray equals: #( 20 30 40 50 70 )
72+
]
73+
74+
{ #category : 'tests' }
75+
CTBinarySearchTreeTest >> testIncludes [
76+
77+
tree add: 50.
78+
tree add: 30.
79+
tree add: 70.
80+
81+
self assert: (tree includes: 50).
82+
self assert: (tree includes: 30).
83+
self assert: (tree includes: 70).
84+
self deny: (tree includes: 99)
85+
]
86+
87+
{ #category : 'tests' }
88+
CTBinarySearchTreeTest >> testIsLeaf [
89+
90+
tree add: 50.
91+
self assert: tree root isLeaf.
92+
93+
tree add: 30.
94+
self deny: tree root isLeaf.
95+
self assert: tree root left isLeaf
96+
]
Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,78 @@
11
"
2-
I represent Abstract Node for BST
2+
I represent an abstract node for Binary Search Tree.
33
"
44
Class {
55
#name : 'CTBSTAbstractNode',
66
#superclass : 'Object',
7+
#instVars : [
8+
'parent'
9+
],
710
#category : 'Containers-BinarySearchTree',
811
#package : 'Containers-BinarySearchTree'
912
}
13+
14+
{ #category : 'adding' }
15+
CTBSTAbstractNode >> addChild: anObject [
16+
17+
^ self subclassResponsibility
18+
]
19+
20+
{ #category : 'accessing' }
21+
CTBSTAbstractNode >> contents [
22+
23+
^ self subclassResponsibility
24+
]
25+
26+
{ #category : 'accessing' }
27+
CTBSTAbstractNode >> contents: anObject [
28+
29+
self subclassResponsibility
30+
]
31+
32+
{ #category : 'accessing' }
33+
CTBSTAbstractNode >> height [
34+
35+
^ self subclassResponsibility
36+
]
37+
38+
{ #category : 'enumerating' }
39+
CTBSTAbstractNode >> inOrderDo: aBlock [
40+
41+
^ self subclassResponsibility
42+
]
43+
44+
{ #category : 'testing' }
45+
CTBSTAbstractNode >> isEmpty [
46+
47+
^ self subclassResponsibility
48+
]
49+
50+
{ #category : 'testing' }
51+
CTBSTAbstractNode >> isLeaf [
52+
53+
^ self subclassResponsibility
54+
]
55+
56+
{ #category : 'accessing' }
57+
CTBSTAbstractNode >> parent [
58+
59+
^ parent
60+
]
61+
62+
{ #category : 'accessing' }
63+
CTBSTAbstractNode >> parent: aNode [
64+
65+
parent := aNode
66+
]
67+
68+
{ #category : 'accessing' }
69+
CTBSTAbstractNode >> search: anObject [
70+
71+
^ self subclassResponsibility
72+
]
73+
74+
{ #category : 'accessing' }
75+
CTBSTAbstractNode >> size [
76+
77+
^ self subclassResponsibility
78+
]
Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,66 @@
11
"
2-
I represent Nill Node for BST
2+
I represent a nil node for Binary Search Tree.
33
"
44
Class {
55
#name : 'CTBSTNillNode',
66
#superclass : 'CTBSTAbstractNode',
77
#category : 'Containers-BinarySearchTree',
88
#package : 'Containers-BinarySearchTree'
99
}
10+
11+
{ #category : 'adding' }
12+
CTBSTNillNode >> addChild: anObject [
13+
14+
^ CTBSTNode new
15+
contents: anObject;
16+
parent: self parent;
17+
yourself
18+
]
19+
20+
{ #category : 'accessing' }
21+
CTBSTNillNode >> contents [
22+
23+
^ nil
24+
]
25+
26+
{ #category : 'accessing' }
27+
CTBSTNillNode >> contents: anObject [
28+
29+
"Do nothing for nil node"
30+
]
31+
32+
{ #category : 'accessing' }
33+
CTBSTNillNode >> height [
34+
35+
^ 0
36+
]
37+
38+
{ #category : 'enumerating' }
39+
CTBSTNillNode >> inOrderDo: aBlock [
40+
41+
"Do nothing for nil node"
42+
]
43+
44+
{ #category : 'testing' }
45+
CTBSTNillNode >> isEmpty [
46+
47+
^ true
48+
]
49+
50+
{ #category : 'testing' }
51+
CTBSTNillNode >> isLeaf [
52+
53+
^ false
54+
]
55+
56+
{ #category : 'accessing' }
57+
CTBSTNillNode >> search: anObject [
58+
59+
^ nil
60+
]
61+
62+
{ #category : 'accessing' }
63+
CTBSTNillNode >> size [
64+
65+
^ 0
66+
]
Lines changed: 103 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,111 @@
11
"
2-
I represent Node for BST
2+
I represent a node for Binary Search Tree.
33
"
44
Class {
55
#name : 'CTBSTNode',
66
#superclass : 'CTBSTAbstractNode',
7+
#instVars : [
8+
'contents',
9+
'left',
10+
'right'
11+
],
712
#category : 'Containers-BinarySearchTree',
813
#package : 'Containers-BinarySearchTree'
914
}
15+
16+
{ #category : 'adding' }
17+
CTBSTNode >> addChild: anObject [
18+
19+
anObject < contents
20+
ifTrue: [ left := left addChild: anObject ]
21+
ifFalse: [ anObject > contents
22+
ifTrue: [ right := right addChild: anObject ] ].
23+
^ self
24+
]
25+
26+
{ #category : 'accessing' }
27+
CTBSTNode >> contents [
28+
29+
^ contents
30+
]
31+
32+
{ #category : 'accessing' }
33+
CTBSTNode >> contents: anObject [
34+
35+
contents := anObject
36+
]
37+
38+
{ #category : 'accessing' }
39+
CTBSTNode >> height [
40+
41+
^ 1 + (left height max: right height)
42+
]
43+
44+
{ #category : 'enumerating' }
45+
CTBSTNode >> inOrderDo: aBlock [
46+
47+
left inOrderDo: aBlock.
48+
aBlock value: contents.
49+
right inOrderDo: aBlock
50+
]
51+
52+
{ #category : 'initialization' }
53+
CTBSTNode >> initialize [
54+
55+
super initialize.
56+
left := CTBSTNillNode new parent: self.
57+
right := CTBSTNillNode new parent: self
58+
]
59+
60+
{ #category : 'testing' }
61+
CTBSTNode >> isEmpty [
62+
63+
^ false
64+
]
65+
66+
{ #category : 'testing' }
67+
CTBSTNode >> isLeaf [
68+
69+
^ left isEmpty and: [ right isEmpty ]
70+
]
71+
72+
{ #category : 'accessing' }
73+
CTBSTNode >> left [
74+
75+
^ left
76+
]
77+
78+
{ #category : 'accessing' }
79+
CTBSTNode >> left: aNode [
80+
81+
left := aNode.
82+
aNode parent: self
83+
]
84+
85+
{ #category : 'accessing' }
86+
CTBSTNode >> right [
87+
88+
^ right
89+
]
90+
91+
{ #category : 'accessing' }
92+
CTBSTNode >> right: aNode [
93+
94+
right := aNode.
95+
aNode parent: self
96+
]
97+
98+
{ #category : 'accessing' }
99+
CTBSTNode >> search: anObject [
100+
101+
contents = anObject ifTrue: [ ^ contents ].
102+
^ anObject < contents
103+
ifTrue: [ left search: anObject ]
104+
ifFalse: [ right search: anObject ]
105+
]
106+
107+
{ #category : 'accessing' }
108+
CTBSTNode >> size [
109+
110+
^ 1 + left size + right size
111+
]

0 commit comments

Comments
 (0)