2024-08-17

anond:20240816235943

高さ3の有向木で根からAという節点が生えAからBという葉が生える

class TreeNode:
def __init__(self, name, attributes=None):
self.name = name
self.attributes = attributes or {}
self.children = []

def add_child(self, child_node):
self.children.append(child_node)

def display(self, level=0):
indent = " " * level
print(f"{indent}{self.name} {self.attributes}")
for child in self.children:
child.display(level + 1)

def has_dependency(self):
# ルートノード属性を持たないノード依存関係を判定しない
if not self.children or "start" not in self.attributes or "end" not in self.attributes:
return False

# Aノードのstartとendを取得
start = self.attributes["start"]
end = self.attributes["end"]

# すべての子ノード(Bノード)に対して依存関係をチェック
for child in self.children:
if "position" in child.attributes:
position = child.attributes["position"]
if start <= position <= end:
print(f"{child.name} (position: {position}) is dependent on {self.name} (start: {start}, end: {end})")
return True
else:
print(f"{child.name} (position: {position}) is NOT dependent on {self.name} (start: {start}, end: {end})")
return False

# ノード作成
root = TreeNode("Root")
a_node = TreeNode("A", {"start": 10, "end": 20})
b1_node = TreeNode("B1", {"position": 15})
b2_node = TreeNode("B2", {"position": 25})

# 木構造の構築
root.add_child(a_node)
a_node.add_child(b1_node)
a_node.add_child(b2_node)

# 木構造の表示
root.display()

# 依存関係のチェック
a_node.has_dependency()

こうかー

記事への反応 -
  • A ー>BみたいなDAGは分かるけど Aがこういう属性持って number start number end Bがこういう属性持ってるとき number position Bのpositionが、Aのstartとendの範囲内にある場合は依存関係あり...

    • この分野は素人だけど、有向非巡回グラフのことなのであれば高さ3の有向木で根からAという節点が生えAからBという葉が生えるようにするという話?それであれば何が問題なのかがよく...

      • 例えば貸しオフィスの例で表現すると ER図がこんな感じ https://mermaid.live/edit#pako:eNqNUsFuwyAM_RXkc_sDHNemaqVtkRrlhoRYMC1agYmC1irJv5c0UbJqqTb7AH7P2A9MDZWTCBSY7Rz9WouDF4ZZkizfbHarjDTNctnUZJ_nb4SSywNX91FnNpoP9MQppSvkWvZM2y_3w...

        • 高さ3の有向木で根からAという節点が生えAからBという葉が生える class TreeNode: def __init__(self, name, attributes=None): self.name = name self.attributes = attributes or {} self.children = [] d...

          • うーむ?やっぱ木じゃOFFICE、ROOM、SCHEDULE、BUSINESS_HOURの4つは表現できないかー

            • で・・・できたけど木の組み立てがしんどすぎるー class TreeNode: def __init__(self, name, attributes=None): self.name = name self.attributes = attributes or {} self.children = [] def add_child(self, chi...

              • あー依存関係は木で表現して、条件はattributesとして持たせればいいのか あしたやるぞー!

                • 依存関係は木で表現 ノードにロック持たせる ロックに条件持たせる やりたいことはできてるように見えるが、うーんしんどい # Entity Relation Diagram# ```mermaid# ---# title: Rental Office example# -...

                  • そんでこのロックのデータをこんなかんじでRedisにもてばネットワーク越しに依存関係のあるロックできる? Type キー名 値 String "Office1" true String "Office2" false Stri...

記事への反応(ブックマークコメント)

ログイン ユーザー登録
ようこそ ゲスト さん