Skip to content

Commit

Permalink
Improve insert
Browse files Browse the repository at this point in the history
  • Loading branch information
travisjungroth committed Dec 19, 2022
1 parent c6a9ac4 commit 5c54ed9
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions mtree4.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,13 @@ def insert(self, value: Value) -> None:
if isinstance(self.children[0], ValueNode):
self.add_child(ValueNode(self.tree, value))
else:
self.children: list[ParentNode]
self.radius = max(self.radius, self.distance(value))
node = random.choice(self.children)
node.insert(value)
distances = [(child, child.distance(value)) for child in self.children]
child, distance = min(distances, key=itemgetter(1))
if distance >= child.radius:
child, _ = min(distances, key=lambda x: x[1] - x[0].radius)
child.insert(value)

def __len__(self):
return len(self.children)
Expand Down

0 comments on commit 5c54ed9

Please sign in to comment.