4 Persistable = 0 'NotPersistable
5 DataBindingBehavior = 0 'vbNone
6 DataSourceBehavior = 0 'vbNone
7 MTSTransactionMode = 0 'NotAnMTSObject
9 Attribute VB_Name = "CBin"
10 Attribute VB_GlobalNameSpace = False
11 Attribute VB_Creatable = True
12 Attribute VB_PredeclaredId = False
13 Attribute VB_Exposed = False
16 ' binary tree, array Method
18 Private GrowthFactor As Single
19 Private InitialAlloc As Long
21 ' fields associated with each node
22 Private Left() As Long ' left child
23 Private Right() As Long ' right child
24 Private Parent() As Long ' parent
25 Private key() As Variant ' user's key
26 Private rec() As Variant ' user's data associated with key
28 Private Root As Long ' root of binary tree
29 Private Node As CNode ' class for allocating nodes
31 Private Function FindNode(ByVal KeyVal As Variant) As Long
33 ' KeyVal key of node to find
37 ' Finds node with key KeyVal.
42 ' find node specified by key
45 If key(x) = KeyVal Then
49 If KeyVal < key(x) Then
56 Raise errKeyNotFound, "CBin.FindNode"
59 Public Sub Insert(ByVal KeyVal, ByRef RecVal As Variant)
61 ' KeyVal key of node to insert
62 ' RecVal record associated with key
64 ' Inserts record RecVal with key KeyVal.
72 ' allocate node for data and insert in tree
78 If key(current) = KeyVal Then Raise errDuplicateKey, "CBin.Insert"
80 If KeyVal < key(current) Then
81 current = Left(current)
83 current = Right(current)
89 If x > UBound(key) Then
90 ReDim Preserve Left(1 To UBound(Left) * GrowthFactor)
91 ReDim Preserve Right(1 To UBound(Right) * GrowthFactor)
92 ReDim Preserve Parent(1 To UBound(Parent) * GrowthFactor)
93 ReDim Preserve key(1 To UBound(key) * GrowthFactor)
94 ReDim Preserve rec(1 To UBound(rec) * GrowthFactor)
100 ' copy fields to node
106 If key(x) < key(p) Then
116 Public Sub Delete(ByVal KeyVal As Variant)
118 ' KeyVal key of node to delete
120 ' Deletes record with key KeyVal.
130 ' delete node z from tree
132 ' find tree successor
133 If Left(z) = 0 Or Right(z) = 0 Then
137 Do While Left(y) <> 0
142 ' x is y's only child
149 ' remove y from the parent chain
150 If x <> 0 Then Parent(x) = Parent(y)
151 If Parent(y) <> 0 Then
152 If y = Left(Parent(y)) Then
161 ' if z and y are not the same, replace z with y.
164 If Left(y) <> 0 Then Parent(Left(y)) = y
166 If Right(y) <> 0 Then Parent(Right(y)) = y
167 Parent(y) = Parent(z)
168 If Parent(z) <> 0 Then
169 If z = Left(Parent(z)) Then
185 Public Function Find(ByVal KeyVal) As Variant
187 ' KeyVal key of node to delete
189 ' record associated with key
191 ' Finds record with key KeyVal
195 Find = rec(FindNode(KeyVal))
198 Public Sub Init(ByVal InitialAllocVal As Long, ByVal GrowthFactorVal As Single)
200 ' InitialAlloc initial allocation of nodes
201 ' GrowthFactor amount for reallocation of nodes (.GT. 1)
203 ' Initializes Bin class. Call once after allocating class.
206 GrowthFactor = GrowthFactorVal
207 ReDim Left(1 To InitialAllocVal)
208 ReDim Right(1 To InitialAllocVal)
209 ReDim Parent(1 To InitialAllocVal)
210 ReDim key(1 To InitialAllocVal)
211 ReDim rec(1 To InitialAllocVal)
213 Node.Init InitialAllocVal, GrowthFactorVal
216 Public Sub Class_Terminate()