fork download
  1. data LinkedTree a
  2. = Root a (LinkedTree a) (LinkedTree a)
  3. | Leaf a (LinkedTree a)
  4. | Branch a (LinkedTree a) (LinkedTree a) (LinkedTree a)
  5.  
  6. root = Root 1 l r
  7. where
  8. l = Branch 2 ll lr root
  9. r = Leaf 3 root
  10. ll = Leaf 21 l
  11. lr = Leaf 22 l
  12.  
  13. instance Show a => Show (LinkedTree a) where
  14. show (Root x l r) = "{" ++ show x ++ " " ++ show l ++ " " ++ show r ++ "}"
  15. show (Leaf x _) = "{" ++ show x ++ "}"
  16. show (Branch x l r _) = "{" ++ show x ++ " " ++ show l ++ " " ++ show r ++ "}"
  17.  
  18. left (Root _ l _) = l
  19. left (Branch _ l _ _) = l
  20. right (Root _ _ r) = r
  21. right (Branch _ _ r _) = r
  22. parent (Leaf _ p) = p
  23. parent (Branch _ _ _ p) = p
  24.  
  25. main = do
  26. print root
  27. let rootLeft = left root
  28. print rootLeft
  29. print $ parent rootLeft
Success #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
{1 {2 {21} {22}} {3}}
{2 {21} {22}}
{1 {2 {21} {22}} {3}}