【发布时间】:2020-02-12 13:09:23
【问题描述】:
我需要实现一个树结构,其中每个节点都有任意数量的子节点。当孩子的数量已知时(链表、二叉树等),这似乎很简单,但我还没有设法在更一般的情况下实现它。
例如,我尝试通过将next 参数更改为列表来扩展此linked list example。但是,似乎没有deferred_type 的列表。关于如何实现这一点的任何想法?
from collections import OrderedDict
from numba import njit, jitclass, types, int32, deferred_type, optional
import numpy as np
node_type = deferred_type()
spec = OrderedDict()
spec['data'] = int32
spec['next'] = optional(types.ListType(node_type))
@jitclass(spec)
class LinkedNode:
def __init__(self, data, next):
self.data = data
self.next = next
def prepend(self, data):
return LinkedNode(data, self)
@njit def make_linked_node(data):
return LinkedNode(data, None)
node_type.define(LinkedNode.class_type.instance_type)
【问题讨论】: