[英]Get parent node of the node in a rose tree
I am trying to get the parent node of the node in a rose tree using following code:我正在尝试使用以下代码获取玫瑰树中节点的父节点:
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE OverloadedStrings #-}
module Main where
import GHC.Generics (Generic)
data RoseTree a = RoseTree { value :: a, children :: [RoseTree a]}
deriving (Show, Generic, Functor)
type Timestamp = Integer
type Tree = RoseTree Timestamp
getParentNode :: Timestamp -> Tree -> Maybe Timestamp
getParentNode node tree@(RoseTree _ []) = Nothing
getParentNode node tree@(RoseTree rootNode (x:xs)) =
if node == value x then Just rootNode else case getParentNode node x of
Nothing -> case getParentNodeForList node (children x) of
Nothing -> getParentNodeForList node xs
Just parent -> Just parent
Just parent -> Just parent
getParentNodeForList :: Timestamp -> [Tree]-> Maybe Timestamp
getParentNodeForList node [] = Nothing
getParentNodeForList node (x:xs) = case getParentNode node x of
Nothing -> case getParentNodeForList node (children x) of
Nothing -> getParentNodeForList node xs
Just parent -> Just parent
Just parent -> Just parent
main :: IO ()
main = do
let tree = RoseTree 1623839394 [RoseTree 1623839395 [], RoseTree 1623839396 [], RoseTree 1623839397 []]
putStrLn $ show $ getParentNode 1623839397 tree
The output is Nothing
for this case.对于这种情况,
Nothing
是空的。 I do not understand why.我不懂为什么。 I covered all possible cases of traversing in
getParentNode
and getParentNodeForList
(at least it seems so, it doesn't work apparently).我在
getParentNode
和getParentNodeForList
中涵盖了所有可能的遍历情况(至少看起来是这样,它显然不起作用)。
Any help is appreciated.任何帮助表示赞赏。
I think the problem is that getParentNode
should recurse on getParentNode node (RoseTree rootNode xs)
, so the whole function would become:我认为问题是
getParentNode
应该在getParentNode node (RoseTree rootNode xs)
上递归,所以整个 function 会变成:
getParentNode :: Timestamp -> Tree -> Maybe Timestamp
getParentNode node tree@(RoseTree _ []) = Nothing
getParentNode node tree@(RoseTree rootNode (x:xs)) =
if node == value x then Just rootNode else case getParentNode node x of
Nothing -> case getParentNodeForList node (children x) of
Nothing -> getParentNode node (RoseTree rootNode xs)
Just parent -> Just parent
Just parent -> Just parent
Personally, I would make use of asum
to write it more succinctly:就个人而言,我会使用
asum
来更简洁地编写它:
getParentNode node (RoseTree x xs)
| node `elem` map value xs = pure x
| otherwise = asum (map (getParentNode node) xs)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.