-2

Consider the following definition of a Rose Tree: The tree can contains unique nodes.

data NTree a = Nil | Node { root :: a, subtree :: [NTree a]} deriving Show
-- or just data NTree a = Nil | Node a [NTree a]

t1 :: NTree Int
t1 = Node 1 [Node 2 [ Node 5 [Nil], Node 6 [Nil], Node 7 [Nil]], Node 3 [Node 8 [Nil], Node 9 [Nil]], Node 4 [Nil]]
{-
t1:        1
        /  |  \
       /   |   \
     2     3     4
   / | \   | \
  5  6  7  8  9
-}

How to find the number of neighbours of a given node of a rose tree? Examples what I mean:

--Function that finds the number of the neighbours of a given node
neighboursOf :: (Eq a) => NTree a -> a -> Int
--Examples:
print (neighboursOf t1 3) -- -> 3 (The neighbours are 1,8 and 9)
print (neighboursOf t1 1) -- -> 3 (The neighbours are 2,3 and 4)
print (neighboursOf t1 8) -- -> 1 (The neighbour is 3)
print (neighboursOf t1 10) -- -> error "Not existing node"

I don't know how to implement the function

neighboursOf :: (Eq a) => NTree a -> a -> Int

Could you help me to implement the function?

Edit: I came up with this solution:

data NTree a = Nil | Node {root :: a, subtree :: [NTree a] } deriving (Show , Eq)

neighborsOf :: (Eq a) => NTree a -> a -> Int
neighborsOf Nil _ = error "Empty tree"
neighborsOf tree element
    | helper tree element True == 0 = error "No such element"
    | otherwise = helper tree element True

helper ::(Eq a) => NTree a -> a -> Bool -> Int
helper Nil _ _ = 0
helper tree el isSuperior
    | root tree == el && isSuperior && subtree tree == [Nil] = 0
    | root tree == el && isSuperior = length $ subtree tree
    | root tree == el && subtree tree == [Nil] = 1
    | root tree == el = length (subtree tree) + 1
    | otherwise = helper' (subtree tree) el
        where 
            helper' :: (Eq a) => [NTree a] -> a -> Int
            helper' [] _ = 0
            helper' (tree:subtrees) el = helper tree el False  + helper' subtrees el

And I think it is a very bad solution. Could you suggest to me some improvements?

  • https://stackoverflow.com/help/how-to-ask –  May 18 '21 at 21:37
  • this is self-contradictory. you now say 9 is a neighbor of 8. why then is not 4 a neighbor of 3?? for consistency, either 8 should have only one neighbor, 3, or something else should change with your examples. – Will Ness May 19 '21 at 08:57
  • @WillNess Yes, you are right! Sorry about my poor example. 8 should have only one neighbor, which is 3. – Martimbuka May 19 '21 at 09:35
  • Welcome to Stack Overflow! Please edit your question to include any code you’ve tried to write so far. If you’re having trouble starting, consider how you’d find the answer by hand, in *very small* steps, and try writing a function for each step & checking if it works. E.g. 1. Find the node in a tree—is the tree `Nil`? Is it `Node n ts`? Does `n` equal the desired label? How do you recursively search `ts` if not? 2. If you found the node, how many children does it have? Is it equal to the length of the `subtrees` field, or not? 3. How many parents does it have? Is it always one, or not? – Jon Purdy May 20 '21 at 00:24
  • it's best to make new post with the updated question. this way it will get the fresh amount of views and hopefully responses. left here it's practically guaranteed to go unnoticed. (I noticed your edit by pure chance). cheers. – Will Ness May 22 '21 at 12:29
  • when you post, please include sample calls and results, and indicate whether the results are always as intended or there sometimes are some errors, or some other issues like you think it's too slow for example. (if it works as intended and you just want your code reviewed, it's supposed to be posted on https://codereview.stackexchange.com instead). – Will Ness May 22 '21 at 12:58

1 Answers1

0

OK now we can start thinking about it. So your neighbors are either children, or a parent, of the node.

You can write two separate functions, one is childrenOf :: Node -> [Node], the other is parentOf :: Node -> [Node], then use the two functions' results and combine them into the one you need. simple. :)

There can only ever be one parent for a node in a tree, so why did I specify the list, [Node], as the result of parentOf? Can there be only one parent always?

Will Ness
  • 70,110
  • 9
  • 98
  • 181