Haskellで木構造

中古ノートPCにubuntuを入れるとだいぶはかどりました。
Haskellでの木構造についてまとめ

data Tree a = EmptyTree | Node a (Tree a) (Tree a) deriving (Show)
singleton x = Node x EmptyTree EmptyTree
treeInsert :: (Ord a) => a -> Tree a -> Tree a
treeInsert x EmptyTree = singleton x
treeInsert x (Node a left right)
     | x == a = Node x left right
     | x  < a = Node a (treeInsert x left) right
     | x  > a = Node a left (treeInsert x right)
treeElem :: (Ord a) => a -> Tree a -> Bool
treeElem x EmptyTree = False
treeElem x (Node a left right)
     | x == a = True
     | x  < a = treeElem x left
     | x  > a = treeElem x right
flattenTree EmptyTree = []
flattenTree (Node a left right) = flattenTree left ++ [a] ++ flattenTree right

data Tree a = EmptyTree | Node a (Tree a) (Tree a)
によって再帰的なデータ構造を定義。