Compare commits

..

5 commits

Author SHA1 Message Date
Bruno BELANYI 1392b07894 Add k-d tree post
Some checks failed
ci/woodpecker/push/deploy/2 Pipeline failed
ci/woodpecker/cron/deploy/2 Pipeline failed
2024-08-10 16:52:17 +01:00
Bruno BELANYI 4796157b65 posts: kd-tree: add nearest neighbour 2024-08-10 16:51:35 +01:00
Bruno BELANYI 8ae274d5b2 posts: kd-tree: add search 2024-08-10 16:48:24 +01:00
Bruno BELANYI 95658484f3 posts: kd-tree: add insertion 2024-08-10 16:47:11 +01:00
Bruno BELANYI b821a20ba1 posts: kd-tree: add construction 2024-08-10 16:44:57 +01:00

View file

@ -72,6 +72,13 @@ class KdLeafNode[T]:
def __init__(self): def __init__(self):
self.points = {} self.points = {}
# Convenience constructor used when splitting a node
@classmethod
def from_items(cls, items: Iterable[tuple[Point, T]]) -> KdLeafNode[T]:
res = cls()
res.points.update(items)
return res
``` ```
#### Split node #### Split node
@ -110,13 +117,6 @@ class KdNode[T]:
def __init__(self): def __init__(self):
self.inner = KdLeafNode() self.inner = KdLeafNode()
# Convenience constructor used when splitting a node
@classmethod
def from_items(cls, items: Iterable[tuple[Point, T]]) -> KdNode[T]:
res = cls()
res.inner.points.update(items)
return res
class KdTree[T]: class KdTree[T]:
_root: KdNode[T] _root: KdNode[T]
@ -165,7 +165,7 @@ def split_leaf[T](node: KdLeafNode[T], axis: Axis) -> KdSplitNode[T]:
return KdSplitNode( return KdSplitNode(
split_axis, split_axis,
mid, mid,
(KdNode.from_items(left), KdNode.from_items(right)), (KdLeafNode.from_items(left), KdLeafNode.from_items(right)),
) )
class KdTree[T]: class KdTree[T]: