@@ -188,7 +188,7 @@ def binary_tree(point_count, left=0, right=0, **kwargs):
188188 float right = 0 -> random arg. should be in [0,1]
189189 NOTICE:left+right mustn't be greater than 1
190190 **kwargs(Keyword args):
191- bool directed = False -> whether the chain is directed(true:directed,false:not directed)
191+ bool directed = False -> whether the binary tree is directed(true:directed,false:not directed)
192192 (int,int) weight_limit = (1,1) -> the limit of weight. index 0 is the min limit, and index 1 is the max limit(both included)
193193 int weight_limit -> If you use a int for this arg, it means the max limit of the weight(included)
194194 int/float weight_gen()
@@ -207,24 +207,33 @@ def binary_tree(point_count, left=0, right=0, **kwargs):
207207 raise Exception ("left and right must be between 0 and 1" )
208208 if left + right > 1 :
209209 raise Exception ("left plus right must be smaller than 1" )
210-
211- can_left = set ( [1 ])
212- can_right = set ( [1 ])
210+
211+ can_left = [1 ]
212+ can_right = [1 ]
213213 graph = Graph (point_count , directed )
214214 for i in range (2 , point_count + 1 ):
215215 edge_pos = random .random ()
216216 node = 0
217217 # Left
218218 if edge_pos < left or left + right < edge_pos <= (1.0 - left - right ) / 2 :
219- node = random .choice (tuple (can_left ))
220- can_left .remove (node )
219+ point_index = random .randint (0 ,len (can_left )- 1 )
220+ node = can_left [point_index ]
221+ del_last_node = can_left .pop () # Save a copy of the last element
222+ if point_index < len (can_left ):
223+ # If the chosen element isn't the last one,
224+ # Copy the last one to the position of the chosen one
225+ can_left [point_index ] = del_last_node
221226 # Right
222- elif left <= edge_pos <= left + right or (1.0 - left - right ) / 2 < edge_pos < 1 :
223- node = random .choice (tuple (can_right ))
224- can_right .remove (node )
227+ else :
228+ # elif left <= edge_pos <= left + right or (1.0 - left - right) / 2 < edge_pos < 1:
229+ point_index = random .randint (0 ,len (can_right )- 1 )
230+ node = can_right [point_index ]
231+ del_last_node = can_right .pop ()
232+ if point_index < len (can_right ):
233+ can_right [point_index ] = del_last_node
225234 graph .add_edge (node , i , weight = weight_gen ())
226- can_left .add (i )
227- can_right .add (i )
235+ can_left .append (i )
236+ can_right .append (i )
228237
229238 return graph
230239
0 commit comments